在使用 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
| <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
|
public class CodeGenerator { public static void main(String[] args) { 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); 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");
strategyConfig.setRestControllerStyle(true);
strategyConfig.setLogicDeleteFieldName("logicDel"); strategyConfig.setEntityLombokModel(true); autoGenerator.setStrategy(strategyConfig); 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
|
@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
|
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
|
@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
|
public interface IUserService extends IService<User> {
}
|
4.6 UserServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12
|
@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>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public interface UserMapper extends BaseMapper<User> {
int insert(User user);
}
|
5.4 Service接口和实现方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public interface IUserService extends IService<User> {
String addUser(User user); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@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 ? "添加成功" : "添加失败"; } }
|
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
|
@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
|
@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
|
@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; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@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
|
@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代码生成器使用