目录
一、分页插件
1.添加配置类
2.在测试类测试
结果
二、xml实现分页的自定义
1.UserMapper中定义接口方法
2.创建UserMapper.xml文件
3.在测试类测试
结果
三、乐观锁
1.场景
2.乐观锁与悲观锁
3.模拟修改冲突
数据库中添加商品表
添加数据
添加实体类
添加mapper
测试
结果
乐观锁实现流程
4.Mybatis-Plus实现乐观锁
首先修改实体类
添加乐观锁插件配置
测试
结果
一、分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能。
1.添加配置类
package com.example.mybatisplus.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 此配置类的作用是向 Spring 容器注册一个 MybatisPlusInterceptor 拦截器,* 该拦截器会在执行 SQL 时拦截分页查询语句,* 并自动处理分页逻辑,包含计算总记录数、总页数等。* */
@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}
2.在测试类测试
package com.example.mybatisplus;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.entity.User;
import com.example.mybatisplus.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.*;@SpringBootTest
public class Test4{@AutowiredUserMapper userMapper;@Testpublic void testPage(){//设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPage(page, null);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}
}
结果
二、xml实现分页的自定义
1.UserMapper中定义接口方法
/*** 根据年龄查询用户列表,分页显示* @param page 分页对象 ,xml中可以从里面进行取值 ,传递参数 Page 即自动分页 ,必须放在第一位* @param age 年龄* @return */Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
即:
2.创建UserMapper.xml文件
在这里面能配置分页的条件:
<?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.example.mybatisplus.mapper.UserMapper"><!--SQL片段,记录基础字段--><sql id="BaseColumns">id,name,age,email</sql><!--IPage<User> selectPageVo(Page<User> page, Integer age);--><select id="selectPageVo" resultType="com.example.mybatisplus.entity.User">SELECT <include refid="BaseColumns"></include> FROM t_user WHERE age > #{age}</select></mapper>
3.在测试类测试
@Testpublic void testSelectPageVo(){//设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPageVo(page, 20);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}
结果
因为加了age的限制,所以这里查询出来去分页的数据就变少了
三、乐观锁
1.场景
一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小 李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太
高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王 也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据 库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就 完全被小王的覆盖了。
那么现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1 万多。
2.乐观锁与悲观锁
上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。
如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证最终的价格是120元。
特性 | 悲观锁 | 乐观锁 |
---|---|---|
锁定时机 | 读取数据时锁定 | 更新数据时检查 |
冲突预防 | 通过锁定数据来预防冲突 | 通过检测数据变更来处理冲突 |
性能 | 通常性能较低,因为涉及到锁的获取和释放 | 通常性能较高,因为没有锁的开销 |
适用场景 | 高冲突环境,写操作频繁 | 低冲突环境,读操作频繁 |
实现方式 | 数据库锁机制,如行锁、表锁 | 版本号或时间戳等机制 |
用户体验 | 用户可能感知到锁的存在,如长时间等待 | 用户通常感知不到锁的存在 |
数据一致性 | 强一致性保证 | 最终一致性,依赖于冲突检测和处理 |
3.模拟修改冲突
数据库中添加商品表
CREATE TABLE t_product
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称 ',
price INT(11) DEFAULT 0 COMMENT '价格 ',
VERSION INT(11) DEFAULT 0 COMMENT '乐观锁版本号 ',
PRIMARY KEY (id)
);
添加数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本 ', 100);
添加实体类
package com.example.mybatisplus.entity;import lombok.*;@Data //lombok注解
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Product {private Long id;private String name;private Integer price;private Integer version;
}
添加mapper
package com.example.mybatisplus.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus.entity.Product;@Repository
public interface ProductMapper extends BaseMapper<Product> {}
测试
package com.example.mybatisplus;import com.example.mybatisplus.entity.Product;
import com.example.mybatisplus.mapper.ProductMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class Test5 {@AutowiredProductMapper productMapper;@Testpublic void testConcurrentUpdate() {//1、小李Product p1 = productMapper.selectById(1L);System.out.println("小李取出的价格:" + p1.getPrice());//2、小王Product p2 = productMapper.selectById(1L);System.out.println("小王取出的价格:" + p2.getPrice());//3、小李将价格加了50元,存入了数据库p1.setPrice(p1.getPrice() + 50);int result1 = productMapper.updateById(p1);System.out.println("小李修改结果:" + result1);//4、小王将商品减了30元,存入了数据库p2.setPrice(p2.getPrice() - 30);int result2 = productMapper.updateById(p2);System.out.println("小王修改结果:" + result2);//最后的结果Product p3 = productMapper.selectById(1L);//价格覆盖,最后的结果:70System.out.println("最后的结果:" + p3.getPrice());}}
结果
这里为了查看方便,我先将配置文件中的日志关闭了。

可以清晰看出变化结果:

乐观锁实现流程
数据库中添加version字段(当然这个咱们在创建时已经创了)
取出记录时,获取当前version
SELECT id,`name`,price,`version` FROM product WHERE id=1
更新时, version + 1,如果where语句中的version版本不对,则更新失败
UPDATE product SET price=price+50, `version`=`version` + 1 WHERE id=1 AND `version`=1
4.Mybatis-Plus实现乐观锁
首先修改实体类

添加乐观锁插件配置
在方才的分页配置这里加一条:
//添加乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

测试
当然在测试之前,先将数据调回100:
然后再进行测试:
/***乐观锁版本* */@Testpublic void testConcurrentVersionUpdate() {//小李取数据Product p1 = productMapper.selectById(1L);//小王取数据Product p2 = productMapper.selectById(1L);//小李修改 + 50p1.setPrice(p1.getPrice() + 50);int result1 = productMapper.updateById(p1);System.out.println("小李修改的结果:" + result1);//小王修改 - 30p2.setPrice(p2.getPrice() - 30);int result2 = productMapper.updateById(p2);System.out.println("小王修改的结果:" + result2);if(result2 == 0){//失败重试,重新获取version并更新p2 = productMapper.selectById(1L);p2.setPrice(p2.getPrice() - 30);result2 = productMapper.updateById(p2);}System.out.println("小王修改重试的结果:" + result2);//老板看价格Product p3 = productMapper.selectById(1L);System.out.println("老板看价格:" + p3.getPrice());}
结果
