
org.mybatis.generator.api.plus.MybatisUtilDistinctController Maven / Gradle / Ivy
package org.mybatis.generator.api.plus;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
* @author wangfupeng
*/
public class MybatisUtilDistinctController {
public static void writeDistinctController(String basePackageName, String aspectPackage, String targetProject,
TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName,
String servicePackage, String controllerPackage, String responseType) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = controllerPackage.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/distinct");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/distinct/Distinct" + tableName + "Controller.java");
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + controllerPackage + ".distinct;\n");
if (responseType != null && responseType.equals("AlphaSimpleResponse")) {
bufferedWriter.write("" +
"\n" +
"import " + aspectPackage + ".AuthUser;\n" +
"import " + servicePackage + "." + tableName + "Service;\n" +
"import " + servicePackage + ".distinct.Distinct" + tableName + "Service;\n" +
"import com.gitee.fufu669.aspect.Logging;\n" +
"import com.gitee.fufu669.response.AlphaSimpleResponse;\n" +
"import io.swagger.annotations.Api;\n" +
"import io.swagger.annotations.ApiOperation;\n" +
"import io.swagger.annotations.ApiParam;\n" +
"import org.slf4j.Logger;\n" +
"import org.slf4j.LoggerFactory;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.http.MediaType;\n" +
"import org.springframework.web.bind.annotation.GetMapping;\n" +
"import org.springframework.web.bind.annotation.RequestHeader;\n" +
"import org.springframework.web.bind.annotation.RequestMapping;\n" +
"import org.springframework.web.bind.annotation.RequestParam;\n" +
"import org.springframework.web.bind.annotation.RestController;\n" +
"\n" +
"@SuppressWarnings({\"unchecked\",\"rawtypes\"})\n" +
"@Api(description = \"脚手架自动生成的Distinct查询" + tableName + "接口\")\n" +
"@RestController\n" +
"@RequestMapping(value = \"/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)\n" +
"public class Distinct" + tableName + "Controller implements Logging {\n" +
" public static final Logger logger = LoggerFactory.getLogger(Distinct" + tableName + "Controller.class);\n" +
"\n" +
" @Autowired\n" +
" private Distinct" + tableName + "Service distinct" + tableName + "Service;\n" +
"\n" +
" @AuthUser\n" +
" @ApiOperation(\"查询对应字段的唯一数据列表" + tableName + "\")\n" +
" @GetMapping(value = \"/distinct\")\n" +
" public AlphaSimpleResponse distinct(@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"查询字段\") @RequestParam(value = \"field\", required = true, defaultValue = \"id\") String field) {\n" +
" //返回结果\n" +
" return AlphaSimpleResponse.success(distinct" + tableName + "Service.getDistinctCacheFetchLater(field));\n" +
" }\n" +
"}" +
"");
} else {
bufferedWriter.write("" +
"\n" +
"import " + aspectPackage + ".AuthUser;\n" +
"import " + servicePackage + "." + tableName + "Service;\n" +
"import " + servicePackage + ".distinct.Distinct" + tableName + "Service;\n" +
"import com.gitee.fufu669.aspect.Logging;\n" +
"import com.gitee.fufu669.response.SimpleResponse;\n" +
"import io.swagger.annotations.Api;\n" +
"import io.swagger.annotations.ApiOperation;\n" +
"import io.swagger.annotations.ApiParam;\n" +
"import org.slf4j.Logger;\n" +
"import org.slf4j.LoggerFactory;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.http.MediaType;\n" +
"import org.springframework.web.bind.annotation.GetMapping;\n" +
"import org.springframework.web.bind.annotation.RequestHeader;\n" +
"import org.springframework.web.bind.annotation.RequestMapping;\n" +
"import org.springframework.web.bind.annotation.RequestParam;\n" +
"import org.springframework.web.bind.annotation.RestController;\n" +
"\n" +
"@SuppressWarnings({\"unchecked\",\"rawtypes\"})\n" +
"@Api(description = \"脚手架自动生成的Distinct查询" + tableName + "接口\")\n" +
"@RestController\n" +
"@RequestMapping(value = \"/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "\", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)\n" +
"public class Distinct" + tableName + "Controller implements Logging {\n" +
" public static final Logger logger = LoggerFactory.getLogger(Distinct" + tableName + "Controller.class);\n" +
"\n" +
" @Autowired\n" +
" private Distinct" + tableName + "Service distinct" + tableName + "Service;\n" +
"\n" +
" @AuthUser\n" +
" @ApiOperation(\"查询对应字段的唯一数据列表" + tableName + "\")\n" +
" @GetMapping(value = \"/distinct\")\n" +
" public SimpleResponse distinct(@ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"查询字段\") @RequestParam(value = \"field\", required = true, defaultValue = \"id\") String field) {\n" +
" //返回结果\n" +
" return SimpleResponse.success(distinct" + tableName + "Service.getDistinctCacheFetchLater(field));\n" +
" }\n" +
"}" +
"");
}
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy