Previously, Maven generator was used to generate code. There was a problem in the configuration file from single module to multi module,
Use mybatis plus to generate code automatically instead.
Code cloud address: https://gitee.com/baomidou/mybatis-plus
github Address: https://github.com/baomidou/mybatis-plus
rely on
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>
Single module generation
public class Generator { public static void main(String[] args) { String [] tableNames = new String[]{"Table name 1","Table name 2"}; String location = "Code generation path location";//For example: com/cn/jzedy generator(location,tableNames); } private static void generator(String location,String [] tableNames){ GlobalConfig globalConfig = new GlobalConfig();// Global configuration globalConfig.setOpen(false)//Open output directory default true .setOutputDir(location)//Output directory of the build file .setFileOverride(true)//Whether to overwrite the existing file default false .setBaseResultMap(true)//Enable BaseResultMap default false .setBaseColumnList(true)//Enable baseColumnList default false .setActiveRecord(false)//Enable ActiveRecord mode default false .setAuthor("Jzedy")//Developer .setServiceName("%sService");//service naming method for example:% sBusiness generates UserBusiness DataSourceConfig dataSourceConfig = new DataSourceConfig();// Data source configuration dataSourceConfig.setDbType(DbType.MYSQL) .setDriverName(Driver.class.getName()) .setUsername("Database connection name") .setPassword("Database connection password") .setUrl("url address"); PackageConfig packageConfig = new PackageConfig();// Packet configuration packageConfig.setParent(location) .setEntity("entity")//Entity package name .setMapper("mapper")//mapper package name .setService("service") .setController("controller"); StrategyConfig strategyConfig = new StrategyConfig();// Policy configuration strategyConfig .setCapitalMode(true)//Hump naming .setEntityLombokModel(false)//Whether [entity] is a lombok model (default false) .setRestControllerStyle(false)//Generate @ RestController controller controller .setNaming(NamingStrategy.underline_to_camel)//The naming strategy of database table mapping to entity, where underline is transferred to hump naming .setInclude(tableNames);//The name of the table to be included. Regular expressions are allowed (either with exclude configuration) new AutoGenerator()////Code generator .setGlobalConfig(globalConfig) .setDataSource(dataSourceConfig) .setPackageInfo(packageConfig) .setStrategy(strategyConfig) .execute(); } }
Refer to specific configuration https://mp.baomidou.com/
Multi module configuration
For example, the following code will generate entity mapper service code in the corresponding location when the project is divided into modules
In the service module, the controller is generated in the web module. The module can be further subdivided by itself, only the code generation path is on the module
On the basis of the adjustment. At the same time, the code generation template of the following code is mybatis plus's own template. If you need to customize the template, it will not be discussed,
public class Generator { public static void main(String[] args) { String [] tableNames = new String[]{"users","roles"}; String [] modules = new String[]{"service","web"};//Project module name, to be customized for (String module : modules) { moduleGenerator(module,tableNames); } } private static void moduleGenerator(String module,String [] tableNames){ GlobalConfig globalConfig = getGlobalConfig(module);// Global configuration DataSourceConfig dataSourceConfig = getDataSourceConfig();// Data source configuration PackageConfig packageConfig = getPackageConfig(module);// Packet configuration StrategyConfig strategyConfig = getStrategyConfig(tableNames);// Policy configuration TemplateConfig templateConfig = getTemplateConfig(module);// Configuration template new AutoGenerator() .setGlobalConfig(globalConfig) .setDataSource(dataSourceConfig) .setPackageInfo(packageConfig) .setStrategy(strategyConfig) .setTemplate(templateConfig) .execute(); } private static TemplateConfig getTemplateConfig(String module) { TemplateConfig templateConfig = new TemplateConfig(); if ("service".equals(module)){ templateConfig.setEntity(new TemplateConfig().getEntity(false)) .setMapper(new TemplateConfig().getMapper())//mapper template uses mybatis plus template .setXml(new TemplateConfig().getXml()) .setService(new TemplateConfig().getService()) .setServiceImpl(new TemplateConfig().getServiceImpl()) .setController(null);//The service module does not generate controller code }else if ("web".equals(module)){//web module only generates controller code templateConfig.setEntity(null) .setMapper(null) .setXml(null) .setService(null) .setServiceImpl(null) .setController(new TemplateConfig().getController()); }else throw new IllegalArgumentException("Parameter matching error, please check"); return templateConfig; } private static StrategyConfig getStrategyConfig(String[] tableNames) { StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig .setCapitalMode(true)//Hump naming .setEntityLombokModel(false) .setRestControllerStyle(false) .setNaming(NamingStrategy.underline_to_camel) .setInclude(tableNames); return strategyConfig; } private static PackageConfig getPackageConfig(String module) { PackageConfig packageConfig = new PackageConfig(); String packageName = "com.cn.jzedy";//User defined path for code generation of different modules if ("service".equals(module)){ packageName+=".web"; }else if ("web".equals(module)){ } packageConfig.setParent(packageName) .setEntity("entity") .setMapper("mapper") .setService("service") .setController("controller"); return packageConfig; } private static DataSourceConfig getDataSourceConfig() { String dbUrl = "jdbc:mysql://localhost:3306/z-blogs"; DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL) .setDriverName(Driver.class.getName()) .setUsername("root") .setPassword("root") .setUrl(dbUrl); return dataSourceConfig; } private static GlobalConfig getGlobalConfig(String module) { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOpen(false)//new File(module).getAbsolutePath() gets the root directory path of the module. For Maven project, the code specifies the path for custom adjustment .setOutputDir(new File(module).getAbsolutePath()+"/src/main/java")//Output directory of the build file .setFileOverride(true)//Overwrite existing file or not .setBaseResultMap(true) .setBaseColumnList(true) .setActiveRecord(false) .setAuthor("Jzedy") .setServiceName("%sService"); return globalConfig; } }