PageUtils.java: 從請求地址中獲取分頁相關參數
RetCode.java: 返回數據中的狀態碼
RetJson.java: 返回數據包裝類
一 : pom依賴
<!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.0</version> </dependency> <!-- freemarker模板--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.29</version> </dependency>
二: 編輯模板(可以直接參照模板中的源碼): 拷貝mybatis plus自帶的模板文件到自己項目下: /resources/templates/freemarker/ (直接放在/resources/templates/ 下會自動覆蓋)
PS: 模板的編輯其實就是講自己寫好通用代碼, 然后復制到模板文件中,然后修改引入包和類就ok了
controller.java.ftl
package ${package.Controller}; import ${package.Entity}.${entity}; import ${package.Service}.${table.serviceName}; import com.xuhong.blogs.core.component.RetJson; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; <#if restControllerStyle> <#else> import org.springframework.stereotype.Controller; </#if> <#if superControllerClassPackage??> import ${superControllerClassPackage}; </#if> import javax.validation.Valid; import java.util.List; /** * <p> * ${table.comment} 前端控制器 * </p> * * @author ${author} * @since ${date} */ @Api(tags = "${table.comment}") <#if restControllerStyle> @RestController <#else> @Controller </#if> @RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>") <#if kotlin> class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if> <#else> <#if superControllerClass??> public class ${table.controllerName} extends ${superControllerClass} { <#else> public class ${table.controllerName} { </#if> @Autowired private ${table.serviceName} ${table.serviceName?uncap_first}; @ApiOperation(value = "${table.comment}分頁列表", response = ${entity}.class) @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "頁面", dataType = "Long"), @ApiImplicitParam(name = "size", value = "頁面數據量", dataType = "Long"), @ApiImplicitParam(name = "sort", value = "排序方式排序[true:正序; false:倒序]", dataType = "Boolean"), @ApiImplicitParam(name = "sortName", value = "排序字段,參照返回字段", dataType = "String")}) @PostMapping(value = "/page") public Object list(@Valid @RequestBody ${entity} param) { Object data = ${table.serviceName?uncap_first}.page(param); return RetJson.ok(data); } @ApiOperation(value = "${table.comment}詳情", response = ${entity}.class) @GetMapping(value = "/info/{id}") public Object info(@PathVariable Long id) { Object data = ${table.serviceName?uncap_first}.info(id); return RetJson.ok(data); } @ApiOperation(value = "${table.comment}新增") @PostMapping(value = "/add") public Object add(@Valid @RequestBody ${entity} param) { ${table.serviceName?uncap_first}.add(param); return RetJson.ok(); } @ApiOperation(value = "${table.comment}修改") @PostMapping(value = "/modify") public Object modify(@Valid @RequestBody ${entity} param) { ${table.serviceName?uncap_first}.modify(param); return RetJson.ok(); } @ApiOperation(value = "${table.comment}刪除(單個條目)") @GetMapping(value = "/remove/{id}") public Object remove(@PathVariable Long id) { ${table.serviceName?uncap_first}.remove(id); return RetJson.ok(); } @ApiOperation(value = "${table.comment}刪除(多個條目)") @PostMapping(value = "/removes") public Object removes(@Valid @RequestBody List<Long> ids) { ${table.serviceName?uncap_first}.removes(ids); return RetJson.ok(); } } </#if>
service.java.ftl
package ${package.Service}; import ${package.Entity}.${entity}; import ${superServiceClassPackage}; import com.baomidou.mybatisplus.core.metadata.IPage; import java.util.List; /** * <p> * ${table.comment!} 服務類 * </p> * * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.serviceName} : ${superServiceClass}<${entity}> <#else> public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { /** * ${table.comment!}分頁列表 * @param param 根據需要進行傳值 * @return */ IPage<${entity}> page(${entity} param); /** * ${table.comment!}詳情 * @param id * @return */ ${entity} info(Long id); /** * ${table.comment!}新增 * @param param 根據需要進行傳值 * @return */ void add(${entity} param); /** * ${table.comment!}修改 * @param param 根據需要進行傳值 * @return */ void modify(${entity} param); /** * ${table.comment!}刪除(單個條目) * @param id * @return */ void remove(Long id); /** * 刪除(多個條目) * @param ids * @return */ void removes(List<Long> ids); } </#if>
serviceImpl.java.ftl
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xuhong.blogs.core.component.GlobalException;
import com.xuhong.blogs.core.component.RetJson;
import com.xuhong.blogs.core.enums.RetCode;
import com.xuhong.blogs.core.utils.IDGeneratorUtils;
import com.xuhong.blogs.core.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* <p>
* ${table.comment!} 服務實現類
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
@Autowired
private PageUtils pageUtils;
/**
* ${table.comment!}分頁列表
* @param param 根據需要進行傳值
* @return
*/
@Override
public IPage<${entity}> page(${entity} param) {
QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
<#list table.fields as field>
// ${field.comment}
<#if !entityLombokModel>
<#if field.propertyType == "Boolean">
<#assign getprefix="is"/>
<#else>
<#assign getprefix="get"/>
</#if>
<#if field.propertyType == "String">
.eq(!StringUtils.isEmpty(param.${getprefix}${field.capitalName}()), ${entity}::${getprefix}${field.capitalName}, param.${getprefix}${field.capitalName}())
<#else>
.eq(param.${getprefix}${field.capitalName}() != null, ${entity}::${getprefix}${field.capitalName}, param.${getprefix}${field.capitalName}())
</#if>
<#else>
<#if field.propertyType == "String">
.eq(!StringUtils.isEmpty(param.get${field.capitalName}()), ${entity}::get${field.capitalName}, param.get${field.capitalName}())
<#else>
.eq(param.get${field.capitalName}() != null, ${entity}::get${field.capitalName}, param.get${field.capitalName}())
</#if>
</#if>
</#list>;
IPage<${entity}> page = page(pageUtils.page(), queryWrapper);
return page;
}
/**
* ${table.comment!}詳情
* @param id
* @return
*/
@Override
public ${entity} info(Long id) {
return getById(id);
}
/**
* ${table.comment!}新增
* @param param 根據需要進行傳值
* @return
*/
@Override
public void add(${entity} param) {
param.setId(IDGeneratorUtils.nextId());
if (!save(param)) {
throw new GlobalException(RetJson.err(RetCode.FAILD));
}
}
/**
* ${table.comment!}修改
* @param param 根據需要進行傳值
* @return
*/
@Override
public void modify(${entity} param) {
if (!updateById(param)) {
throw new GlobalException(RetJson.err(RetCode.FAILD));
}
}
/**
* ${table.comment!}刪除(單個條目)
* @param id
* @return
*/
@Override
public void remove(Long id) {
removeById(id);
}
/**
* ${table.comment!}刪除(多個條目)
* @param ids
* @return
*/
@Override
public void removes(List<Long> ids) {
removeByIds(ids);
}
}
</#if>
PageUtils.java
需要配置分頁插件,直接看官網
import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; /************************************************************** * 創建日期:2019/12/15 11:08 * 作 者:lixuhong * 功能描述:分頁參數獲取 **************************************************************/ @Component public class PageUtils { // 頁面 private static final String PAGE_STR = "page"; private static final Integer DEFAULT_PAGE = 1; // 頁面數據數量 private static final String SIZE_STR = "size"; private static final Integer DEFAULT_SIZE = 10; // 排序 是否正序排列,默認 true private static final String SORT_STR = "sort"; private static final Boolean DEFAULT_SORT = true; private static final String SORT_NAME_STR = "sortName"; @Autowired private HttpServletRequest request; /** * 獲取分頁數據 */ public Page page(){ Page page = new Page(); page.setCurrent(getPage()); page.setSize(getSize()); List<OrderItem> orderItems = getOrders(); if(orderItems != null && orderItems.size() > 0){ page.setOrders(orderItems); } return page; } /** * 排序 */ private List<OrderItem> getOrders(){ // 排序字段 String sortName = getSortName(); if(StringUtils.isBlank(sortName)){ return null; } List<OrderItem> orderItems = new ArrayList<>(); OrderItem orderItem = new OrderItem(); orderItem.setColumn(sortName); orderItem.setAsc(getSort()); orderItems.add(orderItem); return orderItems; } /** * 獲取page參數 */ private Integer getPage(){ String page = request.getParameter(PAGE_STR); return StringUtils.isBlank(page) ? DEFAULT_PAGE : Integer.valueOf(page); } /** * 獲取size參數 */ private Integer getSize(){ String page = request.getParameter(SIZE_STR); return StringUtils.isBlank(page) ? DEFAULT_SIZE : Integer.valueOf(page); } /** * 獲取sortName參數 */ private String getSortName(){ return request.getParameter(SORT_NAME_STR); } /** * 獲取sort參數 是否正序排列,默認 true */ private boolean getSort(){ String str = request.getParameter(SORT_STR); return StringUtils.isBlank(str) ? true : Boolean.valueOf(str); } }
RetCode.java
/************************************************************** * 創建日期: 2020/03/03 10:56 * 作 者: lixuhong * 功能描述: 返回碼 **************************************************************/ public enum RetCode { PARAM_ERROR(100, "參數錯誤"), SUCCESS(200, "執行成功"), NOT_LOGIN(300, "未登錄"), FAILD(400, "執行失敗"), EXCEL_ERROR(1050, "Excel錯誤"), EXCEL_HEAD_ERROR(1051, "Excel表頭錯誤"), UNKNOWN_ERROR(6000, "未知錯誤"); /** 狀態碼 */ private int code; /** 消息 */ private String msg; RetCode(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }
RetJson.java
import com.xuhong.blogs.core.enums.RetCode; import java.io.Serializable; /************************************************************** * 創建日期:2020/1/10 11:08 * 作 者:lixuhong * 功能描述:返回數據 **************************************************************/ public class RetJson implements Serializable { /** 狀態碼 */ private int code; /** 消息 */ private String msg; /** 數據 */ private Object data; /** * 執行成功 */ public static Object ok(){ return new RetJson(); } /** * 執行成功 * @param data 返回數據 */ public static Object ok(Object data){ return new RetJson(data); } /** * 執行失敗 * @param code 返回碼 * @param msg 消息 */ public static Object err(int code, String msg){ return new RetJson(code, msg); } /** * 執行失敗 * @param RetCode 返回碼(枚舉) */ public static Object err(RetCode RetCode){ return err(RetCode.getCode(), RetCode.getMsg()); } /** * 執行失敗 * @param code 返回碼 * @param msg 消息 * @param data 返回數據 */ public static Object err(int code, String msg, Object data){ return new RetJson(code, msg, data); } public RetJson() { this.code = RetCode.SUCCESS.getCode(); this.msg = RetCode.SUCCESS.getMsg(); } public RetJson(Object data) { this.code = RetCode.SUCCESS.getCode(); this.msg = RetCode.SUCCESS.getMsg(); this.data = data; } public RetJson(int code, String msg) { this.code = code; this.msg = msg; } public RetJson(int code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
三: 生成器代碼:
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.FileType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.io.File; import java.util.ArrayList; import java.util.List; /************************************************************** * 創建日期: 2020/03/05 09:55 * 作 者: lixuhong * 功能描述: mybatis plus代碼生成器 **************************************************************/ public class CodeAutoGenerator { /** 作者 */ private static String AUTHOR = "lixuhong"; /** 生成的實體類忽略表前綴: 不需要則置空 */ private static String ENTITY_IGNORE_PREFIX = "blogs"; /** 表名 */ private static String[] TABLES = { "blogs_account", }; // 各層文件輸出到模塊, 沒有則置空 /** Entity.java, Mapper.java, Mapper.xml輸出模塊路徑 */ private static String DAO_OUTPUT_MODULE = "/common"; /** mapper.xml輸出模塊路徑(需要注意放置的位置:默認從模塊/src/main下開始) */ private static String XML_OUTPUT_MODULE = ""; /** IService.java, serviceImpl.java輸出模塊路徑 */ private static String SERVICE_OUTPUT_MODULE = "/common"; /** Controller.java輸出模塊路徑 */ private static String Controller_OUTPUT_MODULE = "/common"; /** 父包名路徑(文件輸出路徑,也是導包的路徑) */ private static String PARENT_PACKAGE_PATH = "/com/xuhong/blogs"; // 各層包名 private static String ENTITY_PATH = "/entity/"; private static String MAPPER_PATH = "/mapper/"; private static String XML_PATH = "/resources/mapper/"; private static String SERVICE_PATH = "/service/"; private static String SERVICE_IMPL_PATH = "/service/impl/"; private static String CONTROLLER_PATH = "/controller/"; // 數據庫 private static String username = "root"; private static String password = "mysqlpwd"; private static String url = "jdbc:mysql://localhost:3306/my_db?serverTimezone=GMT%2B8&characterEncoding=UTF-8&allowMultiQueries=true"; private static DbType DB_TYPE = DbType.MYSQL; private static String driverClassName = "com.mysql.cj.jdbc.Driver"; // 自定義輸出模板和位置 // 文件位置輸出模式: file output path = projectPath + XX_OUTPUT_PATH + File // XX_OUTPUT_PATH = modulePath + packagePath /** entity輸出模板 */ private static String ENTITY_TEMPLATE = "templates/freemarker/entity.java.ftl"; private static String ENTITY_OUTPUT_PATH = DAO_OUTPUT_MODULE + "/src/main/java" + PARENT_PACKAGE_PATH + DAO_OUTPUT_MODULE + ENTITY_PATH; /** mapper.xml輸出模板 */ private static String XML_TEMPLATE = "templates/freemarker/mapper.xml.ftl"; private static String XML_OUTPUT_PATH = DAO_OUTPUT_MODULE + "/src/main" + XML_OUTPUT_MODULE + XML_PATH; /** mapper.java輸出模板 */ private static String MAPPER_TEMPLATE = "templates/freemarker/mapper.java.ftl"; private static String MAPPER_OUTPUT_PATH = DAO_OUTPUT_MODULE + "/src/main/java" + PARENT_PACKAGE_PATH + DAO_OUTPUT_MODULE + MAPPER_PATH; /** service輸出模板 */ private static String SERVICE_TEMPLATE = "templates/freemarker/service.java.ftl"; private static String SERVICE_OUTPUT_PATH = SERVICE_OUTPUT_MODULE + "/src/main/java" + PARENT_PACKAGE_PATH + SERVICE_OUTPUT_MODULE + SERVICE_PATH; /** serviceImpl輸出模板 */ private static String SERVICE_IMPL_TEMPLATE = "templates/freemarker/serviceImpl.java.ftl"; private static String SERVICE_IMPL_OUTPUT_PATH = SERVICE_OUTPUT_MODULE + "/src/main/java" + PARENT_PACKAGE_PATH + SERVICE_OUTPUT_MODULE + SERVICE_IMPL_PATH; /** controller輸出模板 */ private static String CONTROLLER_TEMPLATE = "templates/freemarker/controller.java.ftl"; private static String CONTROLLER_OUTPUT_PATH = Controller_OUTPUT_MODULE + "/src/main/java" + PARENT_PACKAGE_PATH + Controller_OUTPUT_MODULE + CONTROLLER_PATH; public static void main(String[] args) { // 全局配置 GlobalConfig globalConfig = globalConfig(); // 數據源配置 DataSourceConfig dataSourceConfig = dataSourceConfig(); // 策略配置 StrategyConfig strategyConfig = strategyConfig(); // 包配置 PackageConfig packageConfig = packageConfig(); // 模板配置 TemplateConfig templateConfig = templateConfig(); // 自定義配置 InjectionConfig injectionConfig = injectionConfig(); // 執行 new AutoGenerator().setGlobalConfig(globalConfig) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo(packageConfig) // 因為使用了自定義模板,所以需要把各項置空否則會多生成一次 .setTemplate(templateConfig) // 使用的模板引擎,如果不是默認模板引擎則需要添加模板依賴到pom .setTemplateEngine(new FreemarkerTemplateEngine()) .setCfg(injectionConfig) .execute(); } /** * 全局配置 */ private static GlobalConfig globalConfig() { return new GlobalConfig() // 打開文件 .setOpen(false) // 文件覆蓋 .setFileOverride(true) // 開啟activeRecord模式 .setActiveRecord(true) // XML ResultMap: mapper.xml生成查詢映射結果 .setBaseResultMap(true) // XML ColumnList: mapper.xml生成查詢結果列 .setBaseColumnList(true) // swagger注解; 須添加swagger依賴 .setSwagger2(true) // 作者 .setAuthor(AUTHOR) // 設置實體類名稱 .setEntityName("%sDao"); } /** * 數據源配置 */ private static DataSourceConfig dataSourceConfig() { return new DataSourceConfig() // 數據庫類型 .setDbType(DB_TYPE) // 連接驅動 .setDriverName(driverClassName) // 地址 .setUrl(url) // 用戶名 .setUsername(username) // 密碼 .setPassword(password); } /** * 策略配置 */ private static StrategyConfig strategyConfig() { return new StrategyConfig() // 表名生成策略:下划線連轉駝峰 .setNaming(NamingStrategy.underline_to_camel) // 表字段生成策略:下划線連轉駝峰 .setColumnNaming(NamingStrategy.underline_to_camel) // 需要生成的表 .setInclude(TABLES) // 生成controller .setRestControllerStyle(true) // 去除表前綴 .setTablePrefix(ENTITY_IGNORE_PREFIX) // controller映射地址:駝峰轉連字符 .setControllerMappingHyphenStyle(true) // 是否啟用builder 模式 .setEntityBuilderModel(true) // 是否為lombok模型; 需要lombok依賴 .setEntityLombokModel(true) // 生成實體類字段注解 .setEntityTableFieldAnnotationEnable(true); } /** * 包配置 * 設置包路徑用於導包時使用,路徑示例:com.path */ private static PackageConfig packageConfig() { String entity = DAO_OUTPUT_MODULE + ENTITY_PATH; String mapper = DAO_OUTPUT_MODULE + MAPPER_PATH; String xml = DAO_OUTPUT_MODULE + XML_PATH; String service = SERVICE_OUTPUT_MODULE + SERVICE_PATH; String serviceImpl = SERVICE_OUTPUT_MODULE + SERVICE_IMPL_PATH; String controller = Controller_OUTPUT_MODULE + CONTROLLER_PATH; return new PackageConfig() // 父包名 .setParent(PARENT_PACKAGE_PATH.replace('/', '.').substring(1)) .setEntity(entity.replace('/', '.').substring(1, entity.length()-1)) .setMapper(mapper.replace('/', '.').substring(1, mapper.length()-1)) .setXml(xml.replace('/', '.').substring(1, xml.length()-1)) .setService(service.replace('/', '.').substring(1, service.length()-1)) .setServiceImpl(serviceImpl.replace('/', '.').substring(1, serviceImpl.length()-1)) .setController(controller.replace('/', '.').substring(1, controller.length()-1)); } /** * 模板配置 */ private static TemplateConfig templateConfig() { return new TemplateConfig() // 置空后方便使用自定義輸出位置 .setEntity(null) .setXml(null) .setMapper(null) .setService(null) .setServiceImpl(null) .setController(null); } /** * 自定義配置 */ private static InjectionConfig injectionConfig() { return new InjectionConfig() { @Override public void initMap() { // 注入配置 } } // 判斷是否創建文件 .setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 檢查文件目錄,不存在自動遞歸創建 checkDir(filePath); // 指定需要覆蓋的文件 // 文件結尾名字參照 全局配置 中對各層文件的命名,未修改為默認值 if (isExists(filePath) && (!filePath.endsWith("Mapper.xml") && !filePath.endsWith("Dao.java") && !filePath.endsWith("Mapper.java"))) { return false; } return true; } }) // 自定義輸出文件 .setFileOutConfigList(fileOutConfigList()); } /** * 自定義輸出文件配置 */ private static List<FileOutConfig> fileOutConfigList() { List<FileOutConfig> list = new ArrayList<>(); // 當前項目路徑 String projectPath = System.getProperty("user.dir"); // 實體類文件輸出 list.add(new FileOutConfig(ENTITY_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + ENTITY_OUTPUT_PATH + tableInfo.getEntityName() + StringPool.DOT_JAVA; } }); // mapper xml文件輸出 list.add(new FileOutConfig(XML_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + XML_OUTPUT_PATH + tableInfo.getMapperName() + StringPool.DOT_XML; } }); // mapper文件輸出 list.add(new FileOutConfig(MAPPER_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + MAPPER_OUTPUT_PATH + tableInfo.getMapperName() + StringPool.DOT_JAVA; } }); // service文件輸出 list.add(new FileOutConfig(SERVICE_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + SERVICE_OUTPUT_PATH + tableInfo.getServiceName() + StringPool.DOT_JAVA; } }); // service impl文件輸出 list.add(new FileOutConfig(SERVICE_IMPL_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + SERVICE_IMPL_OUTPUT_PATH + tableInfo.getServiceImplName() + StringPool.DOT_JAVA; } }); // controller文件輸出 list.add(new FileOutConfig(CONTROLLER_TEMPLATE) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + CONTROLLER_OUTPUT_PATH + tableInfo.getControllerName() + StringPool.DOT_JAVA; } }); return list; } /** * 判斷文件是否存在 * @param path 路徑 * @return */ private static boolean isExists(String path) { File file = new File(path); return file.exists(); } }
生成目錄結構: