Mybatis-plus逆向工程
概念
MyBatis-Plus 逆向工程(Reverse Engineering)是 MyBatis-Plus 提供的一个功能,它允许根据数据库表自动生成对应的实体类、Mapper 接口和 XML 等文件,从而省去手动创建这些文件的步骤。逆向工程能够加速开发过程,减少手写代码的工作量。
实现
mybatis-plus版本3.5.4.1。
导入依赖
这里模板引擎使用velocity,除此之外还有freemaker等模板引擎
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.5.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
配置数据库:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
测试代码:
public class CodeGenerator {
private static final String URL = "jdbc:mysql://localhost:3306/leadnews_user?useSSL=false&useUnicode=true&characterEncoding=utf8";
public static void main(String[] args) {
FastAutoGenerator.create(URL, "root", "123456")
.globalConfig(builder -> builder.outputDir("D:\\1study"))
.strategyConfig(builder -> builder.addInclude("表名[可以一个也可以多个]"))
.execute();
}
}
测试结果:
文件夹里为对应的文件