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

*/

Page selectPageVo(@Param(“page”) Page page, @Param(“age”) Integer age);




[]( )②UserMapper.xml中编写SQL

------------------------------------------------------------------------------------



select uid,user_name,age,email from t_user where age > #{age}



[]( )③测试

------------------------------------------------------------------



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

> 

>     //小王取数据


# 《MySql面试专题》

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/9a95122d0f207d6f9b6d2eaeb529c0bd.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/738dab8cfa71d47e3b8afc75c933a8d1.webp?x-oss-process=image/format,png)

# 《MySql性能优化的21个最佳实践》

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/3ea43f9814154a6358256ca6461469e5.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/742dff431f45d48e5dc49c347ba0ea18.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/0cba6cbb1afcdd558a17f6663ae29780.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/24d931a9adc2c0dd18128126321e0631.webp?x-oss-process=image/format,png)

# 《MySQL高级知识笔记》

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/dd0611f4b89d61a776f29d3584b2b218.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/a8e292bf484b5f21e464244e8ed2f484.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/07836f4346ad9ebc92e0dbb5c88b5f0e.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/53d40ef1b38a43bc645bacfc86aa5494.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/cfe2fb3c04f4770a704398ae53f3baa7.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/135d16d604f6dcf7589adde474043949.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/fa882bd98791b991f707c565d6186f27.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/16818708a821259b7608f0312539cf05.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/c79cd5f2590ac0f3cd5b642dead3a204.webp?x-oss-process=image/format,png)

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/f619d7ed604ce232ba77a4f8db56be62.webp?x-oss-process=image/format,png)

文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图

![全网火爆MySql 开源笔记,图文并茂易上手,阿里P8都说好](https://img-blog.csdnimg.cn/img_convert/3ff75d4ca683ca0362b54743ec2f9c59.webp?x-oss-process=image/format,png)

**关注我,点赞本文给更多有需要的人**
)]

[外链图片转存中...(img-A1tRuv2u-1719283890459)]

[外链图片转存中...(img-P49dR47c-1719283890459)]

[外链图片转存中...(img-reCZu3MZ-1719283890460)]

[外链图片转存中...(img-GWHz5a1L-1719283890460)]

[外链图片转存中...(img-laiazObq-1719283890460)]

文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图

[外链图片转存中...(img-vn7Obc89-1719283890461)]

**关注我,点赞本文给更多有需要的人**
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值