Hello大家好,本章我們通過freemarker自動生成service,serviceImpl,controller功能 。另求各路大神指點,感謝
一:添加freemarker依賴
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency>

二:創建service,serviceImpl,controller模板
在src\test\java\resources\template\generator
下創建
service.ftl
package ${basePackageService}; import ${basePackageModel}.${modelNameUpperCamel}; import ${basePackage}.core.universal.Service; /** * @Description: ${modelNameUpperCamel}Service接口 * @author ${author} * @date ${date} */ public interface ${modelNameUpperCamel}Service extends Service<${modelNameUpperCamel}> { }

service-impl.ftl
package ${basePackageServiceImpl}; import ${basePackageDao}.${modelNameUpperCamel}Mapper; import ${basePackageModel}.${modelNameUpperCamel}; import ${basePackageService}.${modelNameUpperCamel}Service; import ${basePackage}.core.universal.AbstractService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /** * @Description: ${modelNameUpperCamel}Service接口實現類 * @author ${author} * @date ${date} */ @Service public class ${modelNameUpperCamel}ServiceImpl extends AbstractService<${modelNameUpperCamel}> implements ${modelNameUpperCamel}Service { @Resource private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper; }

controller.ftl
package ${basePackageController}; import ${basePackage}.core.ret.RetResult; import ${basePackage}.core.ret.RetResponse; import ${basePackage}.core.utils.ApplicationUtils; import ${basePackageModel}.${modelNameUpperCamel}; import ${basePackageService}.${modelNameUpperCamel}Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * @Description: ${modelNameUpperCamel}Controller類 * @author ${author} * @date ${date} */ @RestController @RequestMapping("/${baseRequestMapping}") public class ${modelNameUpperCamel}Controller { @Resource private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service; @PostMapping("/insert") public RetResult<Integer> insert(${modelNameUpperCamel} ${modelNameLowerCamel}) throws Exception{ ${modelNameLowerCamel}.setId(ApplicationUtils.getUUID()); Integer state = ${modelNameLowerCamel}Service.insert(${modelNameLowerCamel}); return RetResponse.makeOKRsp(state); } @PostMapping("/deleteById") public RetResult<Integer> deleteById(@RequestParam String id) throws Exception { Integer state = ${modelNameLowerCamel}Service.deleteById(id); return RetResponse.makeOKRsp(state); } @PostMapping("/update") public RetResult<Integer> update(${modelNameUpperCamel} ${modelNameLowerCamel}) throws Exception { Integer state = ${modelNameLowerCamel}Service.update(${modelNameLowerCamel}); return RetResponse.makeOKRsp(state); } @PostMapping("/selectById") public RetResult<${modelNameUpperCamel}> selectById(@RequestParam String id) throws Exception { ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.selectById(id); return RetResponse.makeOKRsp(${modelNameLowerCamel}); } /** * @Description: 分頁查詢 * @param page 頁碼 * @param size 每頁條數 * @Reutrn RetResult<PageInfo<${modelNameUpperCamel}>> */ @PostMapping("/list") public RetResult<PageInfo<${modelNameUpperCamel}>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) throws Exception { PageHelper.startPage(page, size); List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.selectAll(); PageInfo<${modelNameUpperCamel}> pageInfo = new PageInfo<${modelNameUpperCamel}>(list); return RetResponse.makeOKRsp(pageInfo); } }

三:修改上篇文章中提到的CodeGenerator為如下
package com.example.demo; import com.example.demo.core.constant.ProjectConstant; import com.google.common.base.CaseFormat; import freemarker.template.TemplateExceptionHandler; import org.apache.commons.lang3.StringUtils; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.*; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; /** * @author 張瑤 * @Description: 代碼生成器,根據數據表名稱生成對應的Model、Mapper、Service、Controller簡化開發。 * @date 2018/4/23 20:28 */ public class CodeGenerator { // JDBC配置,請修改為你項目的實際配置 private static final String JDBC_URL = "jdbc:mysql://localhost:3333/demo"; private static final String JDBC_USERNAME = "root"; private static final String JDBC_PASSWORD = "123456"; private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; // 模板位置 private static final String TEMPLATE_FILE_PATH = "src/test/java/resources/template/generator"; private static final String JAVA_PATH = "src/main/java"; // java文件路徑 private static final String RESOURCES_PATH = "src/main/resources";// 資源文件路徑 // 生成的Service存放路徑 private static final String PACKAGE_PATH_SERVICE = packageConvertPath(ProjectConstant.SERVICE_PACKAGE); // 生成的Service實現存放路徑 private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(ProjectConstant.SERVICE_IMPL_PACKAGE); // 生成的Controller存放路徑 private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(ProjectConstant.CONTROLLER_PACKAGE); // @author private static final String AUTHOR = "張瑤"; // @date private static final String DATE = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date()); /** * genCode("輸入表名"); * @param args */ public static void main(String[] args) { genCode("system_log"); } /** * 通過數據表名稱生成代碼,Model 名稱通過解析數據表名稱獲得,下划線轉大駝峰的形式。 如輸入表名稱 "t_user_detail" 將生成 * TUserDetail、TUserDetailMapper、TUserDetailService ... * * @param tableNames 數據表名稱... */ public static void genCode(String... tableNames) { for (String tableName : tableNames) { genCode(tableName); } } /** * 通過數據表名稱生成代碼 如輸入表名稱 "user_info" * 將生成 UserInfo、UserInfoMapper、UserInfoService ... * * @param tableName 數據表名稱 */ public static void genCode(String tableName) { genModelAndMapper(tableName); genService(tableName); genController(tableName); } public static void genModelAndMapper(String tableName) { Context context = getContext(); JDBCConnectionConfiguration jdbcConnectionConfiguration = getJDBCConnectionConfiguration(); context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration); PluginConfiguration pluginConfiguration = getPluginConfiguration(); context.addPluginConfiguration(pluginConfiguration); JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = getJavaModelGeneratorConfiguration(); context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration); SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = getSqlMapGeneratorConfiguration(); context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration); JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = getJavaClientGeneratorConfiguration(); context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration); TableConfiguration tableConfiguration = new TableConfiguration(context); tableConfiguration.setTableName(tableName); tableConfiguration.setDomainObjectName(null); context.addTableConfiguration(tableConfiguration); List<String> warnings; MyBatisGenerator generator; try { Configuration config = new Configuration(); config.addContext(context); config.validate(); boolean overwrite = true; DefaultShellCallback callback = new DefaultShellCallback(overwrite); warnings = new ArrayList<>(); generator = new MyBatisGenerator(config, callback, warnings); generator.generate(null); } catch (Exception e) { throw new RuntimeException("生成Model和Mapper失敗", e); } if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) { throw new RuntimeException("生成Model和Mapper失敗:" + warnings); } String modelName = tableNameConvertUpperCamel(tableName); System.out.println(modelName + ".java 生成成功"); System.out.println(modelName + "Mapper.java 生成成功"); System.out.println(modelName + "Mapper.xml 生成成功"); } public static void genService(String tableName) { try { freemarker.template.Configuration cfg = getConfiguration(); //模板所需要的參數 Map<String, Object> data = new HashMap<>(); data.put("date", DATE); data.put("author", AUTHOR); String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); data.put("modelNameUpperCamel", modelNameUpperCamel); data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel)); data.put("basePackage", ProjectConstant.BASE_PACKAGE); data.put("basePackageService", ProjectConstant.SERVICE_PACKAGE); data.put("basePackageServiceImpl", ProjectConstant.SERVICE_IMPL_PACKAGE); data.put("basePackageModel", ProjectConstant.MODEL_PACKAGE); data.put("basePackageDao", ProjectConstant.MAPPER_PACKAGE); File file = new File(JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } cfg.getTemplate("service.ftl").process(data, new FileWriter(file)); System.out.println(modelNameUpperCamel + "Service.java 生成成功"); File file1 = new File(JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java"); if (!file1.getParentFile().exists()) { file1.getParentFile().mkdirs(); } cfg.getTemplate("service-impl.ftl").process(data, new FileWriter(file1)); System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功"); } catch (Exception e) { throw new RuntimeException("生成Service失敗", e); } } public static void genController(String tableName) { try { freemarker.template.Configuration cfg = getConfiguration(); Map<String, Object> data = new HashMap<>(); data.put("date", DATE); data.put("author", AUTHOR); String modelNameUpperCamel = tableNameConvertUpperCamel(tableName); data.put("baseRequestMapping", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel)); data.put("modelNameUpperCamel", modelNameUpperCamel); data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel)); data.put("basePackage", ProjectConstant.BASE_PACKAGE); data.put("basePackageController", ProjectConstant.CONTROLLER_PACKAGE); data.put("basePackageService", ProjectConstant.SERVICE_PACKAGE); data.put("basePackageModel", ProjectConstant.MODEL_PACKAGE); File file = new File(JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } cfg.getTemplate("controller.ftl").process(data, new FileWriter(file)); System.out.println(modelNameUpperCamel + "Controller.java 生成成功"); } catch (Exception e) { throw new RuntimeException("生成Controller失敗", e); } } private static Context getContext() { Context context = new Context(ModelType.FLAT); context.setId("Potato"); context.setTargetRuntime("MyBatis3Simple"); context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`"); context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`"); return context; } private static JDBCConnectionConfiguration getJDBCConnectionConfiguration() { JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration(); jdbcConnectionConfiguration.setConnectionURL(JDBC_URL); jdbcConnectionConfiguration.setUserId(JDBC_USERNAME); jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD); jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME); return jdbcConnectionConfiguration; } private static PluginConfiguration getPluginConfiguration() { PluginConfiguration pluginConfiguration = new PluginConfiguration(); pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin"); pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE); return pluginConfiguration; } private static JavaModelGeneratorConfiguration getJavaModelGeneratorConfiguration() { JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration(); javaModelGeneratorConfiguration.setTargetProject(JAVA_PATH); javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE); javaModelGeneratorConfiguration.addProperty("enableSubPackages", "true"); javaModelGeneratorConfiguration.addProperty("trimStrings", "true"); return javaModelGeneratorConfiguration; } private static SqlMapGeneratorConfiguration getSqlMapGeneratorConfiguration() { SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration(); sqlMapGeneratorConfiguration.setTargetProject(RESOURCES_PATH); sqlMapGeneratorConfiguration.setTargetPackage("mapper"); return sqlMapGeneratorConfiguration; } private static JavaClientGeneratorConfiguration getJavaClientGeneratorConfiguration() { JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration(); javaClientGeneratorConfiguration.setTargetProject(JAVA_PATH); javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE); javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER"); return javaClientGeneratorConfiguration; } private static freemarker.template.Configuration getConfiguration() throws IOException { freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23); cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH)); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); return cfg; } private static String tableNameConvertUpperCamel(String tableName) { return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase()); } private static String packageConvertPath(String packageName) { return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName); } }

四:功能測試
在CodeGenerator
中右鍵run
ok,創建成功
項目地址
碼雲地址: https://gitee.com/beany/mySpringBoot
GitHub地址: https://github.com/MyBeany/mySpringBoot
寫文章不易,如對您有幫助,請幫忙點下star
結尾
通過freemarker自動生成service,serviceImpl,controller功能已完成,后續功能接下來陸續更新,有問題可以聯系我mr_beany@163.com。另求各路大神指點,感謝大家。