当前位置 : 主页 > 手机开发 > harmonyos >

mybatisplus 示例练习4 乐观锁lock

来源:互联网 收集:自由互联 发布时间:2023-08-26
新建springboot工程 导入依赖 dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.1/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/


新建springboot工程

mybatisplus 示例练习4 乐观锁lock_java

mybatisplus 示例练习4 乐观锁lock_Test_02

导入依赖

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


    }
}
上一篇:spring基础jdbcTemplate
下一篇:没有了
网友评论