入门MyBatisPlus前,需要有以下基础,MyBatis、Spring、SpringMVC
为什么要学习它呢?MyBatisPlus可以节省我们大量工作时间,提高工作效率,所有的CRUD代码它都可以自动化完成!
MyBatisPlus官网
简介
(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性
支持的数据库
代码如下(示例):
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 自动生成分配ID,雪花算法 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
说明:我们使用mybatis-plus可以节省我们大量的代码,尽量不要同时导入mybatis和mybatis-plus依赖,避免出现版本差异的问题!
连接数据库的配置方式和mybatis一样
我们所有的sql现在是不可见的,我们希望知道它是怎么执行的,可以增加日志
代码如下(示例):
server:
port: 9075
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: admin123
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1. pojo类
这里创建数据库就不做描述了,数据库创建一个简单便于测试的用户表即可
/**
*
* 用户实体类
*
*
* @author LQW
* @since 2022-11-12
*/
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="User对象", description="")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键ID")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "年龄")
private Integer age;
@ApiModelProperty(value = "邮箱")
private String email;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "修改时间")
private Date updateTime;
@ApiModelProperty(value = "乐观锁")
@Version
private Integer version;
@ApiModelProperty(value = "逻辑删除")
private Integer dr;
}
主键生成策略
数据库插入的id的默认值为:全局的唯一id
默认 ID_WORKER 全局唯一id
主键自增
雪花算法:
snowflake是Twitter开源的分布式ID生成算法,结果是一个long型的ID。其核心思想是:使用41bit作为毫秒数,10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),最后还有一个符号位,永远是0。可以保证几乎全球唯一!
//实体类主键字段上增加该注解
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
2. mapper接口
注意点:我们需要在主启动类上去扫描我们的mapper包下的所有接口
@SpringBootApplication
@MapperScan(basePackages = "com.wen.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
/**
*
* Mapper 接口
* // 在对应的Mapper上面继承基本的类 BaseMapper
*
*
* @author LQW
* @since 2022-11-12
*/
// 代表持久层
@Repository
public interface UserMapper extends BaseMapper<User> {
// 所有的CRUD操作都已经编写完成了
// 你不需要像以前一样配置一大堆文件了!
}
3.service服务类
/**
*
* 服务类
*
*
* @author LQW
* @since 2022-11-12
*/
public interface UserService extends IService<User> {
// 所有的CRUD操作都已经编写完成了
// 你不需要像以前一样配置一大堆文件了
}
4.serviceImpl服务实现类
/**
*
* 服务实现类
*
*
* @author LQW
* @since 2022-11-12
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
1.自动填充
创建时间、修改时间!这些个操作一遍都是自动化完成的,我们不希望手动更新!
实体类中创建时间,修改时间增加如下注解
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty(value = "修改时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
编写处理器来处理这个注解即可!
package com.wen.mybatisplus.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 配置自动填充
* @author LQW
* @createDate 2022/11/12 13:56
*/
@Slf4j
@Component // 一定不要忘记把处理器加到IOC容器中!
public class MyMetaObjectHandler implements MetaObjectHandler {
// 插入时的填充策略
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill.....");
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
}
// 更新时的填充策略
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill.....");
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
2.乐观锁
在面试过程中,我们经常会被问道乐观锁,悲观锁!这个其实非常简单!
乐观锁 : 故名思意十分乐观,它总是认为不会出现问题,无论干什么不去上锁!如果出现了问题,再次更新值测试
悲观锁:故名思意十分悲观,它总是认为总是出现问题,无论干什么都会上锁!再去操作!
我们这里主要讲解 乐观锁机制!
乐观锁实现方式:
我们数据库表和实体类中需要增加version(字段可以自定义)字段及注解
@ApiModelProperty(value = "乐观锁")
@Version
private Integer version;
注册组件
package com.wen.mybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
1. 基础配置
2.
3. @author LQW
4. @createDate 2022/11/12 13:56
*/
// 扫描我们的 mapper 文件夹
@MapperScan("com.wen.mybatisplus.mapper")
@EnableTransactionManagement
@Configuration // 配置类
public class MyBatisPlusConfig {
/**
* 注册乐观锁插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
3.分页插件
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor2() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
4.逻辑删除
物理删除 :从数据库中直接移除
逻辑删除 :再数据库中没有被移除,而是通过一个变量来让他失效! dr = 0 => dr = 1
管理员可以查看被删除的记录!防止数据的丢失,类似于回收站!
我们需要在数据库表和实体类中增加dr(字段可自定义)字段及逻辑删除注解
@ApiModelProperty(value = "逻辑删除")
@TableLogic
private Integer dr;
删除配置
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置逻辑删除
global-config:
db-config:
logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
1. 基础查询方法
因为前面配置了逻辑删除,所以查询默认增加dr=0的条件
条件构造器
十分重要:Wrapper
我们写一些复杂的sql就可以使用它来替代!
基础条件如下
@Autowired
private UserMapper mapper;
/**
* 基础查询方法
*/
@Test
void test01() {
//参数为null,默认不加条件则查询全部内容
List<User> userList = mapper.selectList(null);
userList.forEach(System.out::println);
//条件构造器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//查询姓名为Tom1且年龄为28的用户
wrapper.eq("name","Tom1")
.eq("age",28);
List<User> userList2 = mapper.selectList(wrapper);
userList2.forEach(System.out::println);
//分页查询
// 参数一:当前页
// 参数二:页面大小
// 使用了分页插件之后,所有的分页操作也变得简单的!
Page<User> page = new Page<>(1,5);
//查询年龄大于18小于24的用户
wrapper.clear();
wrapper.gt("age",18)
.lt("age",24);
Page<User> userPage = mapper.selectPage(page, wrapper);
//查询到的内容:userPage.getRecords()
//查询到的条数:userPage.getTotal()
userPage.getRecords().forEach(System.out::println);
System.out.println(userPage.getTotal());
}
控制台输出结果
//不带条件的查询,查询全部
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,dr FROM user WHERE dr=0
==> Parameters:
<== Columns: id, name, age, email, create_time, update_time, version, dr
<== Row: 1, Jone1, 18, test1@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 1562114249870802946, 张三12333, 28, test10@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 1562457359293407234, 张三123, 28, test10@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 2, Jack1, 20, test2@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 3, Tom1, 28, test3@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 4, Sandy1, 21, test4@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 5, Billie1, 24, test5@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Total: 7
//使用条件构造器
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,dr FROM user WHERE dr=0 AND (name = ? AND age = ?)
==> Parameters: Tom1(String), 28(Integer)
<== Columns: id, name, age, email, create_time, update_time, version, dr
<== Row: 3, Tom1, 28, test3@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Total: 1
//使用条件构造器的分页查询
==> Preparing: SELECT COUNT(*) FROM user WHERE dr = 0 AND (age > ? AND age < ?)
==> Parameters: 18(Integer), 24(Integer)
<== Columns: COUNT(*)
<== Row: 2
<== Total: 1
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,dr FROM user WHERE dr=0 AND (age > ? AND age < ?) LIMIT ?
==> Parameters: 18(Integer), 24(Integer), 5(Long)
<== Columns: id, name, age, email, create_time, update_time, version, dr
<== Row: 2, Jack1, 20, test2@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Row: 4, Sandy1, 21, test4@baomidou.com, 2022-11-12 21:14:46, 2022-11-12 21:14:46, 1, 0
<== Total: 2
2. 基础新增方法
单行新增
/**
* 基础新增方法
*/
@Test
void test02() {
User user = new User();
user.setName("测试用户");
user.setAge(18);
user.setEmail("[email protected]");
user.setDr(0);
user.setVersion(1);
//新增方法
mapper.insert(user);
}
//控制台输出
==> Preparing: INSERT INTO user ( id, name, age, email, create_time, update_time, version, dr ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 1591442435652657154(String), 测试用户(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:46:58.889(Timestamp), 2022-11-12 22:46:58.889(Timestamp), 1(Integer), 0(Integer)
<== Updates: 1
批量新增
@Autowired
private UserService service;
/**
* 批量新增
*/
@Test
void test03() {
List<User> list = new ArrayList<>();
//循环遍历赋值,便于测试批量新增
for (int i = 1; i <=10; i++) {
User user = new User();
user.setName("测试用户"+i);
user.setAge(18);
user.setEmail("[email protected]");
user.setDr(0);
user.setVersion(1);
list.add(user);
}
//批量新增
service.saveBatch(list);
}
//控制台输出
==> Preparing: INSERT INTO user ( id, name, age, email, create_time, update_time, version, dr ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 1591444192134909953(String), 测试用户1(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.666(Timestamp), 2022-11-12 22:53:57.666(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.686 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192164270082(String), 测试用户2(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.686(Timestamp), 2022-11-12 22:53:57.686(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.687 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184641(String), 测试用户3(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.687(Timestamp), 2022-11-12 22:53:57.687(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.687 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184642(String), 测试用户4(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.688(Timestamp), 2022-11-12 22:53:57.688(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.688 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184643(String), 测试用户5(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.688(Timestamp), 2022-11-12 22:53:57.688(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.689 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184644(String), 测试用户6(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.689(Timestamp), 2022-11-12 22:53:57.689(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.690 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184645(String), 测试用户7(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.69(Timestamp), 2022-11-12 22:53:57.69(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.691 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184646(String), 测试用户8(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.691(Timestamp), 2022-11-12 22:53:57.691(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.692 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184647(String), 测试用户9(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.692(Timestamp), 2022-11-12 22:53:57.692(Timestamp), 1(Integer), 0(Integer)
2022-11-12 22:53:57.692 INFO 2880 --- [ main] c.w.m.config.MyMetaObjectHandler : start insert fill.....
==> Parameters: 1591444192227184648(String), 测试用户10(String), 18(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.692(Timestamp), 2022-11-12 22:53:57.692(Timestamp), 1(Integer), 0(Integer)
3. 基础修改方法
单行修改
因为前面配置了乐观锁,所以修改的时候都需要验证version
/**
* 基础修改方法
*/
@Test
void test04(){
User user = new User();
user.setId("1591437410968764418");
user.setName("测试修改用户");
user.setAge(38);
user.setEmail("[email protected]");
user.setVersion(2);
//修改方法
mapper.updateById(user);
}
//控制台输出
==> Preparing: UPDATE user SET name=?, age=?, email=?, update_time=?, version=? WHERE id=? AND version=? AND dr=0
==> Parameters: 测试修改用户(String), 38(Integer), 1018314@qq.com(String), 2022-11-12 23:08:37.304(Timestamp), 3(Integer), 1591437410968764418(String), 2(Integer)
<== Updates: 1
批量修改
因为前面配置了乐观锁,所以修改的时候都需要验证version
@Autowired
private UserService service;
/**
* 批量修改
*/
@Test
void test05(){
//先查询获取需要修改的用户数据
//条件构造器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//查询姓名为Tom1且年龄为28的用户
wrapper.eq("name", "Tom1")
.eq("age", 28);
List<User> userList2 = mapper.selectList(wrapper);
//批量修改内容
for (User user : userList2) {
user.setName("测试批量修改Tom1用户");
}
service.updateBatchById(userList2);
}
//控制台输出
==> Preparing: UPDATE user SET name=?, age=?, email=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? AND dr=0
==> Parameters: 测试批量修改Tom1用户(String), 28(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.0(Timestamp), 2022-11-12 23:13:59.089(Timestamp), 2(Integer), 1591444192164270082(String), 1(Integer)
2022-11-12 23:13:59.090 INFO 25456 --- [ main] c.w.m.config.MyMetaObjectHandler : start update fill.....
==> Parameters: 测试批量修改Tom1用户(String), 28(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.0(Timestamp), 2022-11-12 23:13:59.09(Timestamp), 2(Integer), 1591444192227184641(String), 1(Integer)
2022-11-12 23:13:59.090 INFO 25456 --- [ main] c.w.m.config.MyMetaObjectHandler : start update fill.....
==> Parameters: 测试批量修改Tom1用户(String), 28(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.0(Timestamp), 2022-11-12 23:13:59.09(Timestamp), 2(Integer), 1591444192227184642(String), 1(Integer)
2022-11-12 23:13:59.090 INFO 25456 --- [ main] c.w.m.config.MyMetaObjectHandler : start update fill.....
==> Parameters: 测试批量修改Tom1用户(String), 28(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.0(Timestamp), 2022-11-12 23:13:59.09(Timestamp), 2(Integer), 1591444192227184643(String), 1(Integer)
2022-11-12 23:13:59.091 INFO 25456 --- [ main] c.w.m.config.MyMetaObjectHandler : start update fill.....
==> Parameters: 测试批量修改Tom1用户(String), 28(Integer), 101834@qq.com(String), 2022-11-12 22:53:57.0(Timestamp), 2022-11-12 23:13:59.091(Timestamp), 2(Integer), 1591444192227184644(String), 1(Integer)
4. 基础删除方法
单行删除
因为前面配置了逻辑删除,所以这里走的是update语句而不是delete语句
/**
* 基础删除方法
*/
@Test
void test06(){
//删除方法,根据主键删除
mapper.deleteById("1591437410968764418");
}
//控制台输出
==> Preparing: UPDATE user SET dr=1 WHERE id=? AND dr=0
==> Parameters: 1591437410968764418(String)
<== Updates: 1
批量删除
因为前面配置了逻辑删除,所以这里走的是update语句而不是delete语句
/**
* 批量删除
*/
@Test
void test07(){
//先查询获取需要删除的用户数据
//条件构造器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//查询姓名为Tom1且年龄为28的用户
wrapper.eq("name", "Tom1")
.eq("age", 18);
//获取查询出来的用户主键
List<String> ids = mapper.selectList(wrapper).stream()
.map(User::getId).collect(Collectors.toList());
//批量删除
service.removeByIds(ids);
}
//控制台输出
==> Preparing: UPDATE user SET dr=1 WHERE id IN ( ? , ? , ? , ? ) AND dr=0
==> Parameters: 1591444192227184645(String), 1591444192227184646(String), 1591444192227184647(String), 1591444192227184648(String)
<== Updates: 4
MyBatisPlus可以节省我们大量工作时间,提高工作效率,所有的CRUD代码它都可以自动化完成!
入门还是非常简单的,基础方法使用起来没有特别复杂的地方,基本实操了几次后都会了,赶紧上手MyBatisPlus吧
具体内容讲解出自:b站UP主 遇见狂神说
MyBatisPlus讲解