org.mybatis.generator.api.plus.MybatisUtilFeignClient Maven / Gradle / Ivy
The newest version!
package org.mybatis.generator.api.plus;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.io.*;
/**
* @author wangfupeng
*/
public class MybatisUtilFeignClient {
public static void writeFeignClient(String eurekaServiceName, String feignClientUrl, String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = basePackageName.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/feign/common/client");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/feign/common/client/" + tableName + "Client.java");
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + basePackageName + ".feign.common.client;\n" +
"\n" +
"import org.springframework.cloud.netflix.feign.FeignClient;\n" +
"import org.springframework.web.bind.annotation.RequestHeader;\n" +
"import org.springframework.web.bind.annotation.RequestMapping;\n" +
"import org.springframework.web.bind.annotation.RequestMethod;\n" +
"import org.springframework.web.bind.annotation.RequestParam;\n" +
"import org.springframework.web.bind.annotation.RequestBody;\n" +
"import org.springframework.web.bind.annotation.PathVariable;\n" +
"import com.gitee.fufu669.response.ListResponse;\n" +
"import com.gitee.fufu669.response.SimpleResponse;\n" +
"import io.swagger.annotations.ApiParam;\n" +
"import org.springframework.http.MediaType;\n" +
"import " + basePackageName + ".bean.model." + tableName + ";\n" +
"import java.util.Date;\n" +
"\n" +
"/*" + introspectedTable.getRemarks() + "的feign远程调用的客户端*/\n" +
"@FeignClient(name = \"" + eurekaServiceName + "\"" + ((null == feignClientUrl || "".equals(feignClientUrl)) ? "" : ", url = \"" + feignClientUrl + "\"") + ")\n" +
"public interface " + tableName + "Client {\n" +
"\n" +
" /*创建" + introspectedTable.getRemarks() + "*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/create\", method = RequestMethod.POST)\n" +
" public SimpleResponse create(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token,\n" +
" @RequestBody " + tableName + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "\n" +
" );\n" +
"\n");
String pathForPrimaryKeyColumns = MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable);
if (!"".equals(pathForPrimaryKeyColumns)) {
bufferedWriter.write("" +
" /*删除" + introspectedTable.getRemarks() + "*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/delete" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\", method = RequestMethod.GET)\n" +
" public SimpleResponse delete(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token,\n" +
" ");
bufferedWriter.write(MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable));
bufferedWriter.write("\n" +
" );\n" +
"\n");
if (MybatisUtilCommon.getExistNonePrimaryKeyColumns(introspectedTable)) {
bufferedWriter.write("" +
" /*更改" + introspectedTable.getRemarks() + "*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/update\", method = RequestMethod.POST)\n" +
" public SimpleResponse update(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @RequestBody " + tableName + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "\n" +
" );\n" +
"\n" +
" /*自动缓存获取" + introspectedTable.getRemarks() + "实例*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/get" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\", method = RequestMethod.GET)\n" +
" public SimpleResponse get(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token,\n" +
" " + MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable) + "\n" +
" );\n" +
"\n" +
" /*只读缓存获取" + introspectedTable.getRemarks() + "实例*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/mock" + MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable) + "\", method = RequestMethod.GET)\n" +
" public SimpleResponse mock(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token,\n" +
" " + MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable) + "\n" +
" );\n" +
"\n");
}
}
bufferedWriter.write("" +
" /*带缓存获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/list\", method = RequestMethod.GET)\n" +
" public ListResponse list(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @ApiParam(value = \"页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page, \n" +
" @ApiParam(value = \"一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\") Integer limit, \n" +
" @ApiParam(value = \"排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\") String orderBy\n" +
" );\n" +
"\n" +
" /*只读缓存无验证获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/list/mock/without/auth/user\", method = RequestMethod.GET)\n" +
" public ListResponse listMockWithoutAuthUser(\n" +
" @ApiParam(value = \"页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page, \n" +
" @ApiParam(value = \"一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\") Integer limit, \n" +
" @ApiParam(value = \"排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\") String orderBy\n" +
" );\n" +
"\n" +
" /*无缓存有验证获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/list/without/cache\", method = RequestMethod.GET)\n" +
" public ListResponse listWithoutCache(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @ApiParam(value = \"页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page, \n" +
" @ApiParam(value = \"一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\") Integer limit, \n" +
" @ApiParam(value = \"排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\") String orderBy\n" +
" );\n" +
"\n" +
" /*只读缓存有验证获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/list/with/mock\", method = RequestMethod.GET)\n" +
" public ListResponse listWithMock(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @ApiParam(value = \"页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page, \n" +
" @ApiParam(value = \"一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\") Integer limit, \n" +
" @ApiParam(value = \"排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\") String orderBy\n" +
" );\n" +
"\n" +
" /*按条件查询" + introspectedTable.getRemarks() + "实例列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/query\", method = RequestMethod.GET)\n" +
" public ListResponse query(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @ApiParam(value = \"页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page, \n" +
" @ApiParam(value = \"一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"36\") Integer limit, \n" +
" @ApiParam(value = \"排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id desc\") String orderBy, \n" +
" " + MybatisUtilCommon.getParameterWithAnnotationForColumnsWithMaxMinPrefixSuffixLike(introspectedTable) + "\n" +
" );\n" +
"\n" +
" /*获取" + introspectedTable.getRemarks() + "的不同字段的不重复值列表*/\n" +
" @RequestMapping(value = \"/feign/v1/common/" + MybatisUtilCommon.getTableNameWithSlash(introspectedTable) + "/distinct\", method = RequestMethod.GET)\n" +
" public SimpleResponse distinct(\n" +
" @ApiParam(value = \"token\") @RequestHeader(value=\"token\", required = false) String token, \n" +
" @ApiParam(value = \"查询字段\") @RequestParam(value = \"field\", required = true, defaultValue = \"id\") String field\n" +
" );\n" +
"\n}" +
"\n"
);
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
}