06、插件
6.1、分页插件
MyBatis Plus
自带分页插件,只要简单的配置即可实现分页功能
6.1.1、添加配置
<bean class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.atguigu.mp.pojo"></property>
<property name="globalConfig" ref="globalConfig"></property>
<!--配置插件-->
<property name="plugins">
<array>
<ref bean="mybatisPlusInterceptor"></ref>
</array>
</property>
</bean>
<!--配置MyBatis-Plus插件-->
<bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
<property name="interceptors">
<list>
<ref bean="paginationInnerInterceptor"></ref>
</list>
</property>
</bean>
<!--配置MyBatis-Plus分页插件的bean-->
<bean id="paginationInnerInterceptor" class="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor">
<!--设置数据库类型-->
<property name="dbType" value="MYSQL"></property>
</bean>
6.1.2、测试
@Test
public void testPage() {
Page<User> page = new Page<>(2,3);
userMapper.selectPage(page,null);
//当前页
System.out.println(page.getCurrent());
//分页数据
System.out.println(page.getRecords());
//总页数
System.out.println(page.getPages());
//总记录数
System.out.println(page.getTotal());
//是否有下一页
System.out.println(page.hasNext());
//是否有上一页
System.out.println(page.hasPrevious());
}
6.2、xml自定义分页
1、UserMapper
中定义接口方法
public interface UserMapper extends BaseMapper<User> {
/**
* 根据年龄进行查询用户信息,并分页
* @param page MyBatis-Plus所提供的的分页对象,必须位于第一个参数的位置
* @param age
* @return
*/
Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
}
2、UserMapper.xml
中编写SQL
<!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);-->
<select id="selectPageVo" resultType="user">
select uid,user_name,age,email from t_user where age>#{age}
</select>
3、测试
@Test
public void testPageVo() {
Page<User> page = new Page<>(1,3);
userMapper.selectPageVo(page, 18);
//当前页
System.out.println(page.getCurrent());
//分页数据
System.out.println(page.getRecords());
//总页数
System.out.println(page.getPages());
//总记录数
System.out.println(page.getTotal());
//是否有下一页
System.out.println(page.hasNext());
//是否有上一页
System.out.println(page.hasPrevious());
}
6.3、乐观锁
6.3.1、场景
一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据 库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。 现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。
6.3.2、乐观锁与悲观锁
上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过 了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。
如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证最终的价格是120元。
6.3.3、模拟修改冲突
1、数据库中增加商品表
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)
);
2、添加数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
3、创建实体类
import lombok.Data;
@Data
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
4、添加mapper接口
public interface ProductMapper extends BaseMapper<Product> {
}
5、测试
@Test
public void testProduct01() {
//小李查询商品价格
Product productLi = productMapper.selectById(1);
System.out.println("小李查询的商品价格:" + productLi.getPrice());
//小王查询商品价格
Product productWang = productMapper.selectById(1);
System.out.println("小王查询的商品价格:" + productWang.getPrice());
//小李将商品价格+50
productLi.setPrice(productLi.getPrice() + 50);
productMapper.updateById(productLi);
//小王将商品价格-30
productWang.setPrice(productWang.getPrice()-30);
productMapper.updateById(productWang);
//老板查询商品价格
Product productBoss = productMapper.selectById(1);
System.out.println("老板查询的商品价格:" + productBoss.getPrice());
}
结果:
小李查询的商品价格:100
小王查询的商品价格:100
老板查询的商品价格:70
6.3.4、乐观锁实现流程
数据库中添加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
6.3.5、MyBatis-Plus实现乐观锁
1、修改实体类 --> 添加@version
import lombok.Data;
@Data
public class Product {
private Long id;
private String name;
private Integer price;
@Version//标识乐观锁的版本号字段
private Integer version;
}
2、添加乐观锁插件配置
<!--配置MyBatis-Plus插件-->
<bean id="mybatisPlusInterceptor" class="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
<property name="interceptors">
<list>
<ref bean="paginationInnerInterceptor"></ref>
<ref bean="optimisticLockerInnerInterceptor"></ref>
</list>
</property>
</bean>
<!--配置乐观锁插件-->
<bean id="optimisticLockerInnerInterceptor" class="com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor"></bean>
3、测试修改冲突
小李查询商品信息:
SELECT id,name,price,version FROM t_product WHERE id=?
小王查询商品信息:
SELECT id,name,price,version FROM t_product WHERE id=?
小李修改商品价格,自动将version+1
UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 150(Integer), 1(Integer), 1(Long), 0(Integer)
小王修改商品价格,此时version
已更新,条件不成立,修改失败
UPDATE t_product SET name=?, price=?, version=? WHERE id=? AND version=?
Parameters: 外星人笔记本(String), 70(Integer), 1(Integer), 1(Long), 0(Integer)
最终,小王修改失败,查询价格:150
SELECT id,name,price,version FROM t_product WHERE id=?
4、优化流程
@Test
public void testProduct01() {
//小李查询商品价格
Product productLi = productMapper.selectById(1);
System.out.println("小李查询的商品价格:" + productLi.getPrice());
//小王查询商品价格
Product productWang = productMapper.selectById(1);
System.out.println("小王查询的商品价格:" + productWang.getPrice());
//小李将商品价格+50
productLi.setPrice(productLi.getPrice() + 50);
productMapper.updateById(productLi);
//小王将商品价格-30
productWang.setPrice(productWang.getPrice()-30);
int result = productMapper.updateById(productWang);
if (result == 0) {
//操作失败,重试
Product productNew = productMapper.selectById(1);
productNew.setPrice(productNew.getPrice() - 30);
productMapper.updateById(productNew);
}
//老板查询商品价格
Product productBoss = productMapper.selectById(1);
System.out.println("老板查询的商品价格:" + productBoss.getPrice());
}