MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
MyBatis-Plus 的目的是增强 MyBatis 的功能和操作,内置代码生成器可以为我们减少不少的工作量了,这里主要介绍 MyBatis-Plus 代码生成器的使用
代码生成器也是称作 MyBatis 的逆向工程,主要是用来生成 model、mapper 等层的代码,MyBatis 官方也有一个 MyBatis-Generator 的逆向工程,功能非常强大,但生成的代码比较臃肿,生成的 Example 类用于构造复杂的筛选条件,使用起来不友好。相比起来 MyBatis-Plus 生成的代码简洁优雅,配合 CRUD 接口和条件构造器,使用起来也方便
代码生成器
添加依赖
代码生成器的依赖1
2
3
4
5<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>最新版本</version>
</dependency>
MyBatis-Plus 3.0.3 之后移除了自动模板引擎依赖,需要手动添加对应引擎的依赖坐标1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<!-- velocity 模板引擎, 默认 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>最新版本</version>
</dependency>
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>最新版本</version>
</dependency>
<!-- beetl 模板引擎 -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>最新版本</version>
</dependency>
生成代码
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率
1 | public class Generator { |