mybatis-plus-generator 代码生成
import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; public class CodeGenerator { private static final Logger log=Logger.getLogger(CodeGenerator.class.getName()); public static void main(String[] args) { //创建代码生成器 AutoGenerator mpg=new AutoGenerator(); //全局配置 GlobalConfig gc =new GlobalConfig(); final String projectPath = System.getProperty("user.dir"); gc.setAuthor(GeneratorConfig.AUTHOR); gc.setOpen(false); gc.setFileOverride(true); gc.setDateType(DateType.ONLY_DATE); gc.setServiceName("I%sRepository"); gc.setServiceImplName("%sRepository"); // gc.setSwagger2(true); 实体属性 Swagger2 注解 mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(GeneratorConfig.DATASOURCE_URL); dsc.setDriverName(GeneratorConfig.DATASOURCE_DRIVER_NAME); dsc.setUsername(GeneratorConfig.DATASOURCE_USER_NAME); dsc.setPassword(GeneratorConfig.DATASOURCE_PASSWORD); mpg.setDataSource(dsc); // 包配置 final PackageConfig pc = new PackageConfig(); pc.setService("domain.repository"); pc.setEntity("domain.entity"); pc.setServiceImpl("infrastructure.repository"); pc.setMapper("infrastructure.db.mapper"); pc.setModuleName(GeneratorConfig.MODULE); Map<String, String> pathInfo = new HashMap<String, String>(); //设置实体路径 String packagePath = GeneratorConfig.PACKAGE.replace(".", "/"); String entityPath = String.format("%s/%s/src/main/java/%s/domain/entity", projectPath, GeneratorConfig.DOMAIN_LOCATION, packagePath); pathInfo.put(ConstVal.ENTITY_PATH, entityPath); //设置mapper生成路径 String mapperPath = String.format("%s/%s/src/main/java/%s/infrastructure/db/mapper", projectPath, GeneratorConfig.INFR_LOCATION, packagePath); pathInfo.put(ConstVal.MAPPER_PATH, mapperPath); //设置service生成路径 String servicePath = String.format("%s/%s/src/main/java/%s/domain/repository", projectPath, GeneratorConfig.DOMAIN_LOCATION, packagePath); pathInfo.put(ConstVal.SERVICE_PATH, servicePath); //设置service实现类生成路径 String serviceImplPath = String.format("%s/%s/src/main/java/%s/infrastructure/repository", projectPath, GeneratorConfig.INFR_LOCATION, packagePath); pathInfo.put(ConstVal.SERVICE_IMPL_PATH, serviceImplPath); //设置包名 pc.setPathInfo(pathInfo); pc.setParent(GeneratorConfig.PACKAGE); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templPath = ""; // 自定义输出配置 List<FileOutConfig> focList = new ArrayList<FileOutConfig>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templPath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! return projectPath + "/" + GeneratorConfig.INFR_LOCATION + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templConfig = new TemplateConfig(); // 配置自定义输出模板 //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 templConfig.setEntity("/templ/entity.java"); templConfig.setMapper("/templ/mapper.java"); templConfig.setService("/templ/repository.java"); templConfig.setServiceImpl("/templ/repositoryImpl.java"); templConfig.setXml(null); mpg.setTemplate(templConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(GeneratorConfig.TABLE_PREFIX); if (!StringUtils.isBlank(GeneratorConfig.INCLUDE_TABLE)) { strategy.setInclude(GeneratorConfig.INCLUDE_TABLE.split(",")); } mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
配置
package com.leo.li.datagenerator; public class GeneratorConfig { private GeneratorConfig() { } /** * 数据库链接地址 */ public static final String DATASOURCE_URL = ""; public static final String DATASOURCE_DRIVER_NAME = "com.mysql.cj.jdbc.Driver"; /** * 数据库用户名 */ public static final String DATASOURCE_USER_NAME = ""; /** * 数据库密码 */ public static final String DATASOURCE_PASSWORD = ""; /** * 包名,重要 */ public static final String PACKAGE = "com.test.management.service"; /** * artifactId,重要 */ public static final String ARTIFACT_ID = "test-management-service"; /** * infrastructure模块名称 */ public static final String INFR_LOCATION = ARTIFACT_ID + "-infrastructure"; /** * domain模块名称 */ public static final String DOMAIN_LOCATION = ARTIFACT_ID + "-domain"; public static final String MODULE = ""; /** * 表前缀 */ public static final String TABLE_PREFIX = ""; /** * 作者 */ public static final String AUTHOR = ""; /** * 表名,多个英文逗号分割 */ public static final String INCLUDE_TABLE = ""; }