文章目录
1. ActiveRecord简介
ActiveRecord(简称AR)也属于ORM(对象关系映射)层,由Rails最早提出,遵循标准的ORM模型:表映射到记录,记录映射到对象,字段映射到对象属性。配合遵循的命名和配置惯例,能够很大程度的快速实现模型的操作,而且简洁易懂。
ActiveRecord的主要思想是:
每一个数据库表对应创建一个类,类的每一个对象实例对应于数据库中表的一行记录;通常表的每个字段在类中都有相应的Field;
ActiveRecord同时负责把自己持久化,在ActiveRecord中封装了对数据库的访问,即CURD;
ActiveRecord是一种领域模型(Domain Model),封装了部分业务逻辑;
2. 基于AR的CRUD
在MP中,开启AR非常简单,只需要将实体对象继承Model即可。
接下来,我们编写测试类
2.1 插入操作
@Test
public void test02_ARInsert(){
User user = new User();
user.setName("小星星");
user.setAge(30);
user.setPassword("123456");
user.setUserName("xingxing");
user.setMail("xingxing@llll.cn");
boolean flag = user.insert();
System.out.println(flag);
}
2.2 删除操作
@Test
public void test04_ARDelete(){
User user = new User();
user.setId(9l);
boolean flag = user.deleteById();
System.out.println(flag);
}
2.3 更新操作
@Test
public void test03_ARUpdate(){
User user = new User();
user.setId(10l);
user.setAge(19);
boolean flag = user.updateById();
System.out.println(flag);
}
2.4 查询操作
查询年龄低于30岁
@Test
public void test05_ARfind(){
QueryWrapper<User> wrapper= new QueryWrapper<>();
wrapper.le("age", 30);
User user = new User();
List<User> userList = user.selectList(wrapper);
for (User user1 : userList) {
System.out.println(user1);
}
}
根据id进行查询
@Test
public void test01_ARselectById(){
User user = new User();
user.setId(1L);
User user1 = user.selectById();
System.out.println(user1);
}
查询操作用到了基本比较操作,下面列举几种比较操作了解一下
eq:等于
ne:不等于
gt:大于
ge:大于等于
lt:小于
le:小于等于
3. 乐观锁
3.1 场景
一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,价格太高,可能会影响销量。又通知小王,你把商品价格降低30元。
此时,小李和小王同时操作商品后台系统。小李操作的时候,系统先取出商品价格100元;小王也在操作,取出的商品价格也是100元。小李将价格加了50元,并将100+50=150元存入了数据库;小王将商品减了30元,并将100-30=70元存入了数据库。是的,如果没有锁,小李的操作就完全被小王的覆盖了。
现在商品价格是70元,比成本价低10元。几分钟后,这个商品很快出售了1千多件商品,老板亏1万多。
3.2 乐观锁与悲观锁
上面的故事,如果是乐观锁,小王保存价格前,会检查下价格是否被人修改过了。如果被修改过了,则重新取出的被修改后的价格,150元,这样他会将120元存入数据库。
如果是悲观锁,小李取出数据后,小王只能等小李操作完之后,才能对价格进行操作,也会保证最终的价格是120元。
3.3 新建product数据表并添加数据
CREATE TABLE tb_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 tb_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
3.4 编写Product实体并创建接口
import lombok.Data;
@Data
public class Product {
private Long id;
private String name;
private Integer price;
private Integer version;
}
public interface ProductMapper extends BaseMapper<Product> {
}
3.5 添加测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class TestLock {
@Resource
private ProductMapper productMapper;
@Test
public 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);
//价格覆盖,最后的结果:70
System.out.println("最后的结果:" + p3.getPrice());
}
}
我们运行程序
可以看出,这样操作老板血亏,那么如何解决呢,这时候我们就可以引入乐观锁来实现
3.6 添加乐观锁配置插件
- 数据库中添加version字段,取出记录时,获取当前version
- 添加乐观锁插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//添加乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
- 修改测试代码,优化流程
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class TestLock {
@Resource
private ProductMapper productMapper;
@Test
public 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);
if(result2==0){
p2 = productMapper.selectById(1L);
p2.setPrice(p2.getPrice()-30);
result2 = productMapper.updateById(p2);
}
System.out.println("小王修改结果:" + result2);
//最后的结果
Product p3 = productMapper.selectById(1L);
//价格覆盖,最后的结果:70
System.out.println("最后的结果:" + p3.getPrice());
}
}
运行程序,查看结果
可以看出,数据修改成功,老板赚米了…