mybatisplus 示例练习4 乐观锁lock

127 篇文章 0 订阅
40 篇文章 0 订阅

新建springboot工程

导入依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

数据库,建表

drop table if exists o_product;
create table o_product(
	id bigint(20) not null auto_increment primary key,
	name varchar(30) not null comment '商品名称',
	price int(11) comment '价格',
	version int(11) default 0 comment '乐观锁版本号'
);

insert into o_product(name,price) values ('笔记本电脑',100);

 配置文件

applicaion.properties

#springboot默认数据源
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=mysql123
#默认日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

实体类

Product.java

package com.shrimpking.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/10 18:01
 */
@Data
@TableName("o_product")
public class Product
{
    private Long id;

    private String name;

    private Integer price;

    @Version
    private Integer version;
}

 mapper接口

ProductMapper.java

package com.shrimpking.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shrimpking.pojo.Product;
import org.springframework.stereotype.Repository;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/10 18:02
 */
@Repository
public interface ProductMapper extends BaseMapper<Product>
{
}

配置类

MybatisPlusConfig.java

package com.shrimpking.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/10 18:22
 */
@Configuration
public class MybatisPlusConfig
{
    /**
     * 乐观锁插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
        MybatisPlusInterceptor interceptor
                = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
package com.shrimpking;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.shrimpking.mapper")
public class Mybatisplus04Application
{

    public static void main(String[] args)
    {
        SpringApplication.run(Mybatisplus04Application.class, args);
    }

}

测试

ProductTest.java

package com.shrimpking;

import com.shrimpking.mapper.ProductMapper;
import com.shrimpking.pojo.Product;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/10 18:04
 */
@SpringBootTest
public class ProductTest
{
    @Autowired
    private ProductMapper productMapper;

    @Test
    public void test()
    {
        List<Product> products = productMapper.selectList(null);
        products.forEach(System.out::println);
    }

    @Test
    public void test2()
    {
        //乐观锁
        //小李查询商品价格
        Product productLi = productMapper.selectById(1L);
        System.out.println("小李查询的商品价格是:" + productLi.getPrice());

        //小王查询商品价格
        Product productWang = productMapper.selectById(1L);
        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(1L);
            productNew.setPrice(productNew.getPrice() - 30);
            productMapper.updateById(productNew);
        }

        //老板查询
        Product productLaoban = productMapper.selectById(1L);
        System.out.println("老板查询的价格是: " + productLaoban.getPrice());


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

虾米大王

有你的支持,我会更有动力

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

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

打赏作者

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

抵扣说明:

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

余额充值