需求来源/背景:
每次都根据表去新建表的各个类,存在重复的工作,所以使用代码生成生成相关基础类。
具体实现如下:
- 添加相关依赖
XML
<!-- 1-mybatis plus generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
<!-- 2-模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!--3-freemarker模板-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- 添加 mybatis-plus 的依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 4-测试模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.1.0</version>
</dependency>
- 添加对应的数据库依赖
XML
<!-- 1 Postgresql包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 2-sqlite相关包-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.45.1.0</version>
</dependency>
<!-- 2-sqlite 方言-->
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<!-- Oracle驱动 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<!-- DMDB 相关包-->
<!-- https://mvnrepository.com/artifact/com.dameng/DmJdbcDriver18 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.2.192</version>
</dependency>
<!-- mysql包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
- 添加代码生成测试类
java
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@SpringBootTest
public class GenCodeApplicationTests {
/**
* 数据库连接字符
*/
private static final String URL = "jdbc:postgresql://localhost:5432/postgres";
/**
* 生成代码的包名
*/
private static final String PARENT_PACKAGE_NAME = "com.app.business";
/**
* 数据库用户名
*/
private static final String USERNAME = "postgres";
/**
* 数据库密码
*/
private static final String PASSWORD = "postgres";
/**
* 项目根路径
*/
private static final String PROJECT_ROOT_PATH = System.getProperty("user.dir");
public static void main(String[] args) throws ClassNotFoundException {
// 也可以使用绝对地址 D:\\soft\\code\\demo\\src\\main\\java\\com\\ruoyi\\system\\domain
// 项目根路径
String entityPath = PROJECT_ROOT_PATH + "/com/app/business/domain";
// mapper路径
String mapperPath = PROJECT_ROOT_PATH + "/com/app/business/mapper";
// mapper xml路径
String mapperXmlPath = PROJECT_ROOT_PATH + "/src/main/resources/mapper";
// service路径
String servicePath = PROJECT_ROOT_PATH + "/com/app/business/service";
// service实现路径
String serviceImplPath = PROJECT_ROOT_PATH + "/com/app/business/service/impl";
// controller路径
String controllerPath = PROJECT_ROOT_PATH + "/com/app/business/controller";
FastAutoGenerator.create(URL, USERNAME, PASSWORD)
// 1. 全局配置
.globalConfig(builder -> builder
.author("author")
// 开启覆盖已生成的文件。注释掉则关闭覆盖。请谨慎开启此选项!
// .fileOverride()
.disableOpenDir()
// 开启swagger2。注释掉则默认关闭。
.enableSwagger()
.dateType(DateType.TIME_PACK)
// 注释时间策略。
.commentDate("yyyy-MM-dd")
)
.packageConfig((scanner, builder) -> builder
.parent(PARENT_PACKAGE_NAME)
.entity("domain")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
.other("other")
.pathInfo(
new HashMap<OutputFile, String>() {{
put(OutputFile.entity, entityPath);
put(OutputFile.mapper, mapperPath);
put(OutputFile.mapperXml, mapperXmlPath);
put(OutputFile.service, servicePath);
put(OutputFile.serviceImpl, serviceImplPath);
put(OutputFile.controller, controllerPath);
}}
)
)
//todo 加入表名,多个在字符串中用,隔开
.strategyConfig((scanner, builder) -> builder.addInclude(getTables("table_name"))
.entityBuilder()
.enableLombok()
.enableTableFieldAnnotation()
// 逻辑删除字段名(数据库)。
.logicDeleteColumnName("is_delete")
// 逻辑删除属性名(实体)。
.logicDeletePropertyName("isDelete")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
.idType(IdType.ASSIGN_ID)
.mapperBuilder()
.enableMapperAnnotation()
.enableBaseResultMap()
.enableBaseColumnList()
.controllerBuilder()
.enableRestStyle()
.enableHyphenStyle()
.build()
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
@Test
void contextLoads() {
}
}
最后运行这个测试类即可生成对应的代码类了!