什么是 MyBatis-Plus
MyBatis-Plus(简称 MP)是一个基于 MyBatis 的增强工具,在 MyBatis 的基础上,只做增强不做改变,为简化开发、提高效率而生。MyBatis-Plus 有着十分丰富的功能,如无侵入式的 CURD 操作、分页插件、代码生成器、SQL注入器、性能分析插件、SQL执行分析等等。
官网地址:MyBatis-Plus 官网地址
如何使用 MyBatis-Plus
1. 引入 MyBatis-Plus 的依赖
在 Maven 工程中,在 pom.xml 文件中加入以下依赖:
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
这里以最新版本的 MyBatis-Plus 3.4.2 为例。
2. 配置 MyBatis-Plus 的配置文件
在 Spring Boot 工程中,使用 MyBatis-Plus 时,需要在 application.yml 或 application.properties 中对 MyBatis-Plus 进行配置。下面是一个示例:
yaml
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml # 扫描 mapper.xml 文件的位置
type-aliases-package: com.example.entity # 扫描实体类的位置
global-config:
db-config:
id-type: auto # id 自增策略
table-prefix: t_ # 表名前缀
db-type: mysql # 数据库类型
这些配置项详细说明如下:
- mapper-locations:mapper.xml 文件存放的位置,可以使用 classpath: 前缀指定类路径。
- type-aliases-package:实体类所在的包名,这里使用 com.example.entity。
- id-type:主键生成的策略,auto 表示自增,none 表示不生成
- table-prefix:表名前缀,生成实体类时去掉前缀。
- db-type:数据库类型。
3. 编写实体类
MyBatis-Plus 的实体类一般和数据库表一一对应,可以使用 @TableName 注解指定对应的表名,使用 @TableField 注解指定对应的列名。示例代码如下:
@Data
@TableName("user") // 指定对应的表名
public class User {
@TableId(type = IdType.AUTO) // 主键自增
private Long id;
@TableField("name") // 指定对应的列名
private String username;
private String password;
private String email;
}
这里使用 Lombok 的 @Data 注解简化 Getter 和 Setter 方法的编写,使用了 @TableId 和 @TableField 注解来指定对应的主键和列名。
4. 编写 Mapper 接口
MyBatis-Plus 可以根据接口中的方法自动生成 SQL 语句,需要在 Mapper 接口中定义对应的方法。示例代码如下:
public interface UserMapper extends BaseMapper<User> {
}
这里使用了 MyBatis-Plus 提供的 BaseMapper 接口,可以直接使用 MyBatis-Plus 提供的简单查询方法,如 selectById、selectList 等,也可以自定义 SQL 语句。
5. 使用 MyBatis-Plus 的 CRUD 操作
使用 MyBatis-Plus 的 CRUD 操作非常简单,因为它已经封装了基本的增删改查方法,可以直接使用。示例代码如下:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public List<User> getAllUser() {
return userMapper.selectList(null);
}
@Override
public boolean addUser(User user) {
return userMapper.insert(user) > 0;
}
@Override
public boolean updateUser(User user) {
return userMapper.updateById(user) > 0;
}
@Override
public boolean deleteUser(Long id) {
return userMapper.deleteById(id) > 0;
}
}
这里使用 @Autowired 自动注入了 UserMapper 对象,并使用其中的 selectById、selectList、insert、updateById、deleteById 等方法。
6. 使用 MyBatis-Plus 的分页插件
MyBatis-Plus 提供了分页插件,可以很方便的实现分页查询,示例代码如下:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Page<User> getUserByPage(int pageNo, int pageSize) {
Page<User> page = new Page<>(pageNo, pageSize);
return userMapper.selectPage(page, null);
}
}
这里使用了 MyBatis-Plus 提供的 Page 类和 selectPage 方法,可以很方便地实现分页查询。
7. 使用 MyBatis-Plus 的代码生成器
MyBatis-Plus 还提供了代码生成器,可以根据数据库表自动生成实体类、Mapper 接口以及 XML 映射文件,大大简化了开发过程。使用代码生成器只需要在配置文件中添加以下配置:
yaml
mybatis-plus:
generator:
# 配置代码生成器
global-config:
author: your name
output-dir: src/main/java
file-override: true # 是否覆盖已有文件
datasource:
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 密码
package-info:
parent: com.example
strategy:
table-prefix: t_
naming: underline_to_camel # 表名映射到实体的命名策略
column-underline: true # 是否使用下划线分隔字段名
然后在项目中编写以下代码即可:
public class CodeGenerator {
public static void main(String[] args) {
AutoGenerator generator = new AutoGenerator();
generator.setGlobalConfig(globalConfig()); // 配置全局属性
generator.setDataSource(dataSource()); // 数据源配置
generator.setPackageInfo(packageConfig()); // 包配置
generator.setStrategy(strategy()); // 策略配置
generator.setTemplate(templateConfig()); // 模板配置
generator.execute(); // 执行代码生成器
}
}
这里使用了 AutoGenerator 类来生成代码,具体的配置可以参考 MyBatis-Plus 官方文档。
8. 总结
本文介绍了如何使用 MyBatis-Plus,其中包括配置 MyBatis-Plus 的配置文件、编写实体类、Mapper 接口以及使用 MyBatis-Plus 的 CRUD 操作、分页插件和代码生成器。MyBatis-Plus 可以大大简化开发过程,提高开发效率,具有十分广泛的应用前景。