【Plug】七、插件

1、分页插件

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

1.1、添加配置类

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

1.2、测试

1.2.1、使用QueryWrapper

IPage<User> page = new Page<>(1, 3);
IPage<User> userIPage = 
    userMapper.selectPage(page, 
                          new LambdaQueryWrapper<User>().ne(User::getId, 1L));
System.out.println("==========================");
System.out.println("userIPage==>" + userIPage);;
System.out.println("分页对象记录列表:【");
userIPage.getRecords().forEach(System.out::println);
System.out.println("】\n当前页:" + userIPage.getCurrent());
System.out.println("每页显示条数:" + userIPage.getSize());
System.out.println("当前分页总页数:" + userIPage.getPages());
System.out.println("总条数:" + userIPage.getTotal());
System.out.println("是否存在上一页:" + ((Page)userIPage).hasPrevious());
System.out.println("是否存在下一页:" + ((Page)userIPage).hasNext());
==>  Preparing: SELECT COUNT(*) AS total FROM user WHERE (id <> ?)
==> Parameters: 1(Long)
<==    Columns: total
<==        Row: 9
<==      Total: 1
==>  Preparing: SELECT id,name,age,email FROM user WHERE (id <> ?) LIMIT ?
==> Parameters: 1(Long), 3(Long)
<==    Columns: id, name, age, email
<==        Row: 2, 小明, 12, xxx@xxx.com
<==        Row: 3, 小黑, 20, hhi@hi.com
<==        Row: 4, 咕咕咕, 16, gugugu@gugu.com
<==      Total: 3
==========================
userIPage==>com.baomidou.mybatisplus.extension.plugins.pagination.Page@69d23296
分页对象记录列表:【
User(id=2, name=小明, age=12, email=xxx@xxx.com)
User(id=3, name=小黑, age=20, email=hhi@hi.com)
User(id=4, name=咕咕咕, age=16, email=gugugu@gugu.com)
】
当前页:1
每页显示条数:3
当前分页总页数:3
总条数:9
是否存在上一页:false
是否存在下一页:true

1.2.2、自定义XML

// UserMapper中定义接口方法
IPage<User> queryByPage(@Param("page")IPage<User> page, @Param("id") Long id);
<!--UserMapper.xml-->
<select id="queryByPage" resultType="hom.wang.mb.pojo.User">
    SELECT id,name,age,email FROM user WHERE (id <![CDATA[ <> ]]> #{id})
</select>
// Test
IPage<User> page = new Page<>(1, 3);
IPage<User> userIPage = userMapper.queryByPage(page, 1L);
System.out.println("==========================");
System.out.println("userIPage==>" + userIPage);;
System.out.println("分页对象记录列表:【");
userIPage.getRecords().forEach(System.out::println);
System.out.println("】\n当前页:" + userIPage.getCurrent());
System.out.println("每页显示条数:" + userIPage.getSize());
System.out.println("当前分页总页数:" + userIPage.getPages());
System.out.println("总条数:" + userIPage.getTotal());
System.out.println("是否存在上一页:" + ((Page)userIPage).hasPrevious());
System.out.println("是否存在下一页:" + ((Page)userIPage).hasNext());
==>  Preparing: SELECT COUNT(*) AS total FROM user WHERE (id <> ?)
==> Parameters: 1(Long)
<==    Columns: total
<==        Row: 9
<==      Total: 1
==>  Preparing: SELECT id,name,age,email FROM user WHERE (id <> ?) LIMIT ?
==> Parameters: 1(Long), 3(Long)
<==    Columns: id, name, age, email
<==        Row: 2, 小明, 12, xxx@xxx.com
<==        Row: 3, 小黑, 20, hhi@hi.com
<==        Row: 4, 咕咕咕, 16, gugugu@gugu.com
<==      Total: 3
==========================
userIPage==>com.baomidou.mybatisplus.extension.plugins.pagination.Page@1cf0cacc
分页对象记录列表:【
User(id=2, name=小明, age=12, email=xxx@xxx.com)
User(id=3, name=小黑, age=20, email=hhi@hi.com)
User(id=4, name=咕咕咕, age=16, email=gugugu@gugu.com)
】
当前页:1
每页显示条数:3
当前分页总页数:3
总条数:9
是否存在上一页:false
是否存在下一页:true

2、乐观锁插件

2.1、发现问题

// 实体类
@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    private Integer version;
}
// Mapper.java
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
// Test
/*
	期望是原价100先加60再减20最终为140
	实际A和B同时改价格,都读到了原价100,结果B覆盖了A的操作最终变成了80
*/
// A 查询价格
Product productA = productMapper.selectById(1L);
System.out.println("A获取到的价格是:" + productA.getPrice());
// B 查询价格
Product productB = productMapper.selectById(1L);
System.out.println("B 获取到的价格是:" + productB.getPrice());
// A 处理价格+60
productA.setPrice(productA.getPrice() + 60);
productMapper.updateById(productA);
// B 处理价格-20
productB.setPrice(productB.getPrice() - 20);
productMapper.updateById(productB);
// 最终价格查询
Product productFinal = productMapper.selectById(1L);
System.out.println("最终价格价格是:" + productFinal.getPrice());

2.2、乐观锁插件闪亮登场

点击查看MyBatis-Plus官方 乐观锁插件

2.2.1、乐观锁实现流程

数据库中添加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

2.2.2、Mybatis-Plus实现乐观锁

// 修改实体类,给版本字段加上 @Version 注解
@Data
public class Product {
    private Long id;
    private String name;
    private Integer price;
    @Version
    private Integer version;
}
// 新建或修改上面的配置类,添加乐观锁插件
@Configuration
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

重新运行上面2.1的Test代码,最终价格是160,因为B的修改不符合条件,输出如下:

==>  Preparing: SELECT id,name,price,version FROM t_product WHERE id=?
==> Parameters: 1(Integer)
<==    Columns: id, name, price, version
<==        Row: 1, 外星人笔记本, 100, 0
<==      Total: 1
A获取到的价格是:100

==>  Preparing: SELECT id,name,price,version FROM t_product WHERE id=?
==> Parameters: 1(Integer)
<==    Columns: id, name, price, version
<==        Row: 1, 外星人笔记本, 100, 0
<==      Total: 1
B 获取到的价格是:100

==>  Preparing: UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
==> Parameters: 外星人笔记本(String), 160(Integer), 1(Integer), 1(Long), 0(Integer)
<==    Updates: 1

==>  Preparing: UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
==> Parameters: 外星人笔记本(String), 80(Integer), 1(Integer), 1(Long), 0(Integer)
<==    Updates: 0

==>  Preparing: SELECT id,name,price,version FROM t_product WHERE id=?
==> Parameters: 1(Integer)
<==    Columns: id, name, price, version
<==        Row: 1, 外星人笔记本, 160, 1
<==      Total: 1
最终价格价格是:160

2.2.3、优化处理流程

// A 查询价格
Product productA = productMapper.selectById(1);
System.out.println("A获取到的价格是:" + productA.getPrice());
// B 查询价格
Product productB = productMapper.selectById(1);
System.out.println("B 获取到的价格是:" + productB.getPrice());
// A 处理价格+60
productA.setPrice(productA.getPrice() + 60);
productMapper.updateById(productA);
// B 处理价格-20
productB.setPrice(productB.getPrice() - 20);
int  resultB = productMapper.updateById(productB);
while (resultB == 0){
    productB = productMapper.selectById(1);
    System.out.println("B 重新获取到的价格是:" + productB.getPrice());
    productB.setPrice(productB.getPrice() - 20);
    resultB = productMapper.updateById(productB);
}
// 最终价格查询
Product productFinal = productMapper.selectById(1);
System.out.println("最终价格价格是:" + productFinal.getPrice());

最终价格是140,达到期望。

3、MyBatisX插件

MyBatis-Plus为我们提供了强大的mapper和service模板,能够大大的提高开发效率

但是在真正开发过程中,MyBatis-Plus并不能为我们解决所有问题,例如一些复杂的SQL,多表 联查,我们就需要自己去编写代码和SQL语句,我们该如何快速的解决这个问题呢?

这个时候可 以使用MyBatisX插件 MyBatisX一款基于 IDEA 的快速开发插件,为效率而生。

点击访问MyBatis-Plus官网 MybatisX快速开发插件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

纯纯的小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值