MybatisPlus——插件(MybatisPlus学习该专栏就够啦)

🌹🌹作者:Philosophy7
🚩🚩内容:这一章节介绍了MybatisPlus中的常用插件以及通用枚举
相信这一篇也能给小伙伴带来一定的收获
三连走起来!!! ⭐⭐⭐
在这里插入图片描述

一、分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

①添加配置类

@Configuration
//扫描mapper所在的包
@MapperScan("com.atguigu.mybatisplus.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

②测试

@SpringBootTest
public class MybatisPlusPageTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testPage(){
        Page<User> page = new Page<>(1,3);

        userMapper.selectPage(page,null);
        System.out.println(page.getRecords());
        System.out.println(page.getCurrent()); //获取当前页的页码
        System.out.println(page.getSize());
        System.out.println(page.getPages()); // 获取总页数
        System.out.println(page.getTotal()); //获取总记录数
        System.out.println(page.hasNext()); //是否有下一页数据
        System.out.println(page.hasPrevious()); //是否有上一页数据
    }

二、XML自定义分页接口方法

①UserMapper中定义接口方法

/**
 * 通过年龄来查询用户信息并分页
 * @param page
 * @param age
 * @return
 */
Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);

②UserMapper.xml中编写SQL

<select id="selectPageVo" resultType="user">
    select uid,user_name,age,email from t_user where age > #{age}
</select>

③测试

@Test
public void testPageVo(){
    //select uid,user_name,age,email from t_user where age > ? LIMIT ?
    Page<User> page = new Page<>(1,3);
    userMapper.selectPageVo(page,20);
    System.out.println(page.getRecords());
    System.out.println(page.getCurrent()); //获取当前页的页码
    System.out.println(page.getSize());
    System.out.println(page.getPages()); // 获取总页数
    System.out.println(page.getTotal()); //获取总记录数
    System.out.println(page.hasNext()); //是否有下一页数据
    System.out.println(page.hasPrevious()); //是否有上一页数据
}

三、乐观锁

场景

一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小 李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太 高,可能会影响销量。又通知小王,你把商品价格降低30元。

此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王 也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据 库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就 完全被小王的覆盖了。

现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1 万多。

乐观锁与悲观锁

上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过 了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。

如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证 最终的价格是120元。

模拟修改冲突

增加商品表

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);

创建实体类

@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    private Integer version;
}

添加Mapper

public interface ProductMapper extends BaseMapper<Product> {
}

乐观锁实现流程

数据库中添加version字段

更新时,version+1 如果where语句中的version版本不对 则更新失败

修改实体类

@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;

    @Version
    private Integer version;
}

添加乐观锁插件配置

  @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //添加分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        //添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

测试修改后的冲突

小李查询商品信息: 100

SELECT id,name,price,version FROM t_product WHERE id=?

小李将价格上涨50元 此时version = 1

UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?

小王查询商品信息 100

SELECT id,name,price,version FROM t_product WHERE id=?

此时 小王修改商品价格 version已更新 条件不成立 修改失败

最终结果 查询价格:150

优化流程

测试之前将数据库中数据修改回来 version = 0 price=100

@Test
public void test2(){
    //小李取数据
    Product product_Li = productMapper.selectById(1L);

    //小王取数据
    Product product_Wang = productMapper.selectById(1L);

    //小李修改数据 + 50
    product_Li.setPrice(product_Li.getPrice() + 50);
    int result = productMapper.updateById(product_Li);
    System.out.println("小李的修改结果:" + result);

    //小王修改数据 - 30
    product_Wang.setPrice(product_Wang.getPrice() - 30);
    int result2 = productMapper.updateById(product_Wang);
    System.out.println("result:" + result2);
    if (result2 == 0){
        //修改失败 重新获取version并更新
        product_Wang = productMapper.selectById(1L);
        product_Wang.setPrice(product_Wang.getPrice() - 30);
        result2 = productMapper.updateById(product_Wang);
    }
    System.out.println("小王修改的结果: " + result2);

    //最终的价格
    Product Boss = productMapper.selectById(1L);
    System.out.println(Boss);
}

在这里插入图片描述

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Philosophy7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值