Mybatis Plus代码生成器

HYF Lv3

在使用 Spring Boot开发项目时,需要手动创建Controller, Entity, Service, ServiceImpl, Mapper, Mapper XML 文件,效率实在太慢,本文介绍如何使用Mybatis Plus代码生成器去自动生成各个模块代码。
MyBatis Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Controller, Entity, Service, ServiceImpl, Mapper, Mapper XML 各个模块的代码,极大的提升了开发效率。

1 引入依赖

初始化一个Spring Boot 项目,在引入所需业务依赖下,额外引入以下Mybatis Plus相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--        Mybatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- 生成器所需模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>

2 数据库建表

2.1 user 表结构:

1
2
3
4
5
6
7
8
9
10
11
MariaDB [test]> desc user;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| userid | char(19) | NO | PRI | NULL | |
| username | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| createtime | datetime | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
5 rows in set (0.001 sec)

2.2 file 表结构

1
2
3
4
5
6
7
8
9
10
11
12
13
MariaDB [test]> desc file;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| fileid | char(19) | NO | PRI | NULL | |
| filename | varchar(255) | YES | | NULL | |
| createtime | datetime | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
| type | varchar(255) | YES | | NULL | |
| collection | varchar(255) | YES | | NULL | |
| logicdel | tinyint(1) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
7 rows in set (0.001 sec)

3 启动生成器代码

使用生成器之前代码结构:

1
2
3
4
---com.youfeng.CodeGenerator:
---utils:
---CodeGenerator.java
---Application.java

在 com/youfeng/CodeGenerator 下新建 utils/CodeGenerator.java 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @author -侑枫
* @date 2023/5/21 15:38:22
*/
public class CodeGenerator {
public static void main(String[] args) {
//1.获取代码生成器的对象
AutoGenerator autoGenerator = new AutoGenerator();

//设置数据库相关配置
DataSourceConfig dataSource = new DataSourceConfig();

dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://47.120.5.13:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai");
dataSource.setUsername("['you databases username']");
dataSource.setPassword("['you databases password']");
autoGenerator.setDataSource(dataSource);

//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
//设置代码生成位置
globalConfig.setOutputDir(System.getProperty("user.dir")+"\\src\\main\\java");
//设置生成完毕后是否打开生成代码所在的目录
globalConfig.setOpen(false);
//设置作者
globalConfig.setAuthor("-无心");
//设置是否覆盖原始生成的文件
globalConfig.setFileOverride(true);
//设置数据层接口名,%s为占位符,指代模块名称
//globalConfig.setMapperName("%sMapper");
//设置Id生成策略
globalConfig.setIdType(IdType.ASSIGN_ID);
autoGenerator.setGlobalConfig(globalConfig);

//设置包名相关配置
PackageConfig packageInfo = new PackageConfig();
//设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径
packageInfo.setParent("com.youfeng.CodeGenerator");
//设置实体类包名
packageInfo.setEntity("entity");
//设置数据层包名
packageInfo.setMapper("mapper");
autoGenerator.setPackageInfo(packageInfo);

//策略设置
StrategyConfig strategyConfig = new StrategyConfig();
//设置当前参与生成的表名,参数为可变参数
strategyConfig.setInclude("user","file");
//设置数据库表的前缀名称,模块名 = 数据库表名 - 前缀名 例如: User = tbl_user - tbl_
// strategyConfig.setTablePrefix("tbl_");
//设置是否启用Rest风格
strategyConfig.setRestControllerStyle(true);
//设置乐观锁字段名
// strategyConfig.setVersionFieldName("version");
//设置逻辑删除字段名
strategyConfig.setLogicDeleteFieldName("logicDel");
//设置是否启用lombok
strategyConfig.setEntityLombokModel(true);
autoGenerator.setStrategy(strategyConfig);
//2.执行生成操作
autoGenerator.execute();
}
}

生成后的代码机构为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
---com.youfeng.CodeGenerator:
---controller:
---FileController.java
---UserController.java
---mapper:
---xml:
---FileMapper.xml
---UserMapper.xml
---FileMapper.java
---UserMapper.java
---entity:
---File.java
---User.java
---service:
---impl:
---FileServiceImpl.java
---UserServiceImpl.java
---IFileService.java
---IUserService.java
---utils:
---CodeGenerator.java
---Application.java

4 代码展示

4.1 UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* <p>
* 前端控制器
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@RestController
@RequestMapping("/user")
public class UserController {

}

4.2 UserMapper.xml

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youfeng.CodeGenerator.mapper.UserMapper">

</mapper>

4.3 UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
/**
* <p>
* Mapper 接口
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
public interface UserMapper extends BaseMapper<User> {

}

4.4 User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* <p>
* 实体类
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements Serializable {

private static final long serialVersionUID=1L;

@TableId(value = "userid", type = IdType.ASSIGN_ID)
private String userid;

private String username;

private String password;

private LocalDateTime createTime;

private LocalDateTime updateTime;


}

4.5 IUserService.java

1
2
3
4
5
6
7
8
9
10
11
/**
* <p>
* 服务类
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
public interface IUserService extends IService<User> {

}

4.6 UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
/**
* <p>
* 服务实现类
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

}

5 代码测试

5.1 完善 application.yml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: "jdbc:mysql://47.120.5.13:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai"
username: "['you databases username']"
password: "['you databases password']"

logging:
level:
root: warn
com.scaffold.test.mapper: trace

5.2 XML 增加 sql 语句 mapper/userMapper.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youfeng.CodeGenerator.mapper.UserMapper">
<insert id="insert">
insert into user
(username, password)
value
(#{username}, #{password})
</insert>
</mapper>

5.3 Mapper接口 com/youfeng/CodeGenerator/mapper/UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* <p>
* Mapper 接口
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
public interface UserMapper extends BaseMapper<User> {

/**
* 添加用户
*
* @param user 用户信息
* @return 添加结果 >0 表示插入成功
*/
int insert(User user);

}

5.4 Service接口和实现方法

5.4.1 com/youfeng/CodeGenerator/service/IUserService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* <p>
* 服务类
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
public interface IUserService extends IService<User> {

/**
* 添加用户
*
* @param user 用户信息
* @return 添加结果
*/
String addUser(User user);
}

5.4.2 com/youfeng/CodeGenerator/service/impl/UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* <p>
* 服务实现类
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

@Resource
private UserMapper mapper;

@Override
public String addUser(User user) {
return mapper.insert(user) > 0 ? "添加成功" : "添加失败";
}
}

5.5 Controller层:com/youfeng/CodeGenerator/controller/UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* <p>
* 前端控制器
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@RestController
@RequestMapping("/users")
public class UserController {
@Resource
private IUserService userService;

@PostMapping("/add")
public String add() {
User user = new User();
user.setUsername("-wuxin");
user.setPassword("456");
return userService.addUser(user);
}

@PostMapping("/insert")
public String insert() {
User user = new User();
user.setUsername("-snow");
user.setPassword("123");
return userService.save(user) ? "添加成功" : "添加失败";
}

}

5.6 启动项目

项目启动发现一个报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.youfeng.CodeGenerator.mapper.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.youfeng.CodeGenerator.mapper.UserMapper' in your configuration.


Process finished with exit code 1

问题原因:

遗漏自动注入,Spring没有扫描到UserMapper这个bean

解决办法:

方法一:在每个mapper接口上添加上注解@Mapper

方法二:在Application.java文件中添加注解@MapperScan(“这里填入mapper接口的路径”)

如果使用@Mapper这个注解的话,每个dao都需要添加,有些麻烦。

而解决方法二使用@MapperScan就可以很好的解决该问题的。

在启动类Application.java添加@MapperScan注解

1
2
3
4
5
6
7
8
9
10
/**
* @author -侑枫
*/
@MapperScan("com.youfeng.CodeGenerator.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

项目成功启动

5.7 PostMan测试

使用add()方法添加后的结果:

1
2
3
4
5
6
7
MariaDB [test]> select * from user;
+---------------------+----------+----------+------------+------------+
| userid | username | password | createtime | updatetime |
+---------------------+----------+----------+------------+------------+
| 1642912163666333697 | -wuxin | 123 | NULL | NULL |
+---------------------+----------+----------+------------+------------+
1 row in set (0.000 sec)

使用insert()方法添加后的结果:

1
2
3
4
5
6
7
8
MariaDB [test]> select * from user;
+---------------------+----------+----------+------------+------------+
| userid | username | password | createtime | updatetime |
+---------------------+----------+----------+------------+------------+
| 1642912163666333697 | -wuxin | 123 | NULL | NULL |
| 1642912299863773185 | -snow | 123 | NULL | NULL |
+---------------------+----------+----------+------------+------------+
2 rows in set (0.000 sec)

项目测试完毕

6 代码优化

测试结果我们可以发现一个问题,createtime和updatetime为NULL,这显然不符合我们的需求

6.1 修改User实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* <p>
*
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "userid", type = IdType.ASSIGN_ID)
private String userid;

private String username;

private String password;

@TableField(value = "createtime", fill = FieldFill.INSERT)
private String createTime;

@TableField(value = "updatetime", fill = FieldFill.INSERT_UPDATE)
private String updateTime;

}

6.2 在 config 包下创建 MyMetaObjectHandler.java 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @author -侑枫
* @date 2023/5/21 15:54:45
*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", simp.format(new Date()), metaObject);
this.setFieldValByName("updateTime", simp.format(new Date()), metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", simp.format(new Date()), metaObject);
}
}

6.3 修改controller后重新使用 PostMan 测试

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* <p>
* 前端控制器
* </p>
*
* @author -侑枫
* @since 2023-05-21
*/
@RestController
@RequestMapping("/users")
public class UserController {
@Resource
private IUserService userService;

@PostMapping("/add")
public String add() {
User user = new User();
user.setUsername("-wuxin");
user.setPassword("456");
return userService.addUser(user);
}

@PostMapping("/insert")
public String insert() {
User user = new User();
user.setUsername("-snow");
user.setPassword("456");
return userService.save(user) ? "添加成功" : "添加失败";
}

}

使用add()方法和insert()方法添加后的结果:
1
2
3
4
5
6
7
8
9
10
MariaDB [test]> select * from user;
+---------------------+----------+----------+---------------------+---------------------+
| userid | username | password | createtime | updatetime |
+---------------------+----------+----------+---------------------+---------------------+
| 1642912163666333697 | -wuxin | 123 | NULL | NULL |
| 1642912299863773185 | -snow | 123 | NULL | NULL |
| 1642912649085755393 | -wuxin | 456 | 2023-04-03 23:31:14 | 2023-04-03 23:31:14 |
| 1642912717310304257 | -snow | 456 | 2023-04-03 23:31:30 | 2023-04-03 23:31:30 |
+---------------------+----------+----------+---------------------+---------------------+
4 rows in set (0.000 sec)

代码优化完成,最后附上本文所写源代码:Mybatis Plus代码生成器使用

  • 标题: Mybatis Plus代码生成器
  • 作者: HYF
  • 创建于 : 2023-05-21 13:46:32
  • 更新于 : 2024-07-27 21:21:51
  • 链接: https://youfeng.ink/generator-0cf176b7f99d/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
Mybatis Plus代码生成器