- 引入jar包:pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 编写代码生成类:MyBatisPlusCode.java
public class MyBatisPlusCode {
public static void main(String[] args) {
AutoGenerator generator = new AutoGenerator();
generator.setTemplateEngine(new FreemarkerTemplateEngine());
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir") + "/mybatis-plus/src/main/java");
globalConfig.setAuthor("Steven");
globalConfig.setOpen(false);
globalConfig.setFileOverride(false);
globalConfig.setServiceName("%sService");
globalConfig.setIdType(IdType.AUTO);
globalConfig.setDateType(DateType.ONLY_DATE);
globalConfig.setSwagger2(true);
generator.setGlobalConfig(globalConfig);
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/myabtis?useUnicode=true&useSSL=false&characterEncoding=utf8");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("password");
dataSourceConfig.setDbType(DbType.MYSQL);
generator.setDataSource(dataSourceConfig);
PackageConfig packageConfig = new PackageConfig();
packageConfig.setModuleName("user");
packageConfig.setParent("com.macro.cloud");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setController("controller");
generator.setPackageInfo(packageConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user");
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
strategy.setLogicDeleteFieldName("delete_flag");
TableFill gmtCreate = new TableFill("crete_time", FieldFill.INSERT);
TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
strategy.setVersionFieldName("version");
generator.setStrategy(strategy);
generator.execute();
}
}
- 展示效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/007d52e082d312d7e4ee82dbc0791a11.png)