日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis-Plus插件

發布時間:2024/1/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis-Plus插件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一. 分頁插件
    • 1. 添加配置類
    • 2. 測試
  • 二. xml自定義分頁
    • 1. UserMapper中定義接口方法
    • 2. UserMapper.xml中編寫SQL
    • 3. 測試
  • 三. 樂觀鎖
    • 1. 場景
    • 2. 樂觀鎖與悲觀鎖
    • 3. 模擬修改沖突
      • 3.1 數據庫中增加商品表
      • 3.2 添加實體
      • 3.3 添加mapper
      • 3.4 測試
      • 3.5 樂觀鎖實現流程
      • 3.6 Mybatis-Plus實現樂觀鎖

一. 分頁插件

MyBatis Plus自帶分頁插件,只要簡單的配置即可實現分頁功能

1. 添加配置類


2. 測試


@SpringBootTest public class MyBatisPlusPluginsTest {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@Testpublic void testPage(){Page<User> page = new Page<>(2, 3);userMapper.selectPage(page, null);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());} }

二. xml自定義分頁

1. UserMapper中定義接口方法

package com.atguigu.mybatisplus.mapper;import com.atguigu.mybatisplus.pojo.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository;import java.util.Map;/*** Date:2022/2/12* Author:ybc* Description:*/ @Repository public interface UserMapper extends BaseMapper<User> {/*** 根據id查詢用戶信息為map集合* @param id* @return*/Map<String, Object> selectMapById(Long id);/*** 通過年齡查詢用戶信息并分頁* @param page MyBatis-Plus所提供的分頁對象,必須位于第一個參數的位置* @param age* @return*/Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);}

2. UserMapper.xml中編寫SQL

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.atguigu.mybatisplus.mapper.UserMapper"><!--Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);--><!--設置過類型別名User,這個類型別名就是類名不區分大小寫--><select id="selectPageVo" resultType="User">select uid,user_name,age,email from mybatis_plus.t_user where age > #{age}</select></mapper>

3. 測試


@Testpublic void testPageVo(){Page<User> page = new Page<>(1, 3);userMapper.selectPageVo(page, 20);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());}

三. 樂觀鎖

1. 場景

2. 樂觀鎖與悲觀鎖

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) );INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人筆記本', 100);

3.2 添加實體

3.3 添加mapper

package com.atguigu.mybatisplus.mapper;import com.atguigu.mybatisplus.pojo.Product; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.springframework.stereotype.Repository;/*** Date:2022/2/15* Author:ybc* Description:*/ @Repository public interface ProductMapper extends BaseMapper<Product> { }

3.4 測試


package com.atguigu.mybatisplus;import com.atguigu.mybatisplus.mapper.ProductMapper; import com.atguigu.mybatisplus.mapper.UserMapper; import com.atguigu.mybatisplus.pojo.Product; import com.atguigu.mybatisplus.pojo.User; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;/*** Date:2022/2/14* Author:ybc* Description:*/ @SpringBootTest public class MyBatisPlusPluginsTest {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@Testpublic void testProduct01(){//小李查詢商品價格Product productLi = productMapper.selectById(1);System.out.println("小李查詢的商品價格:"+productLi.getPrice());//小王查詢商品價格Product productWang = productMapper.selectById(1);System.out.println("小王查詢的商品價格:"+productWang.getPrice());//小李將商品價格+50productLi.setPrice(productLi.getPrice()+50);productMapper.updateById(productLi);//小王將商品價格-30productWang.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 productLaoban = productMapper.selectById(1);System.out.println("老板查詢的商品價格:"+productLaoban.getPrice());}}

3.5 樂觀鎖實現流程

3.6 Mybatis-Plus實現樂觀鎖

修改實體類

package com.atguigu.mybatisplus.entity; import com.baomidou.mybatisplus.annotation.Version; import lombok.Data;@Data public class Product {private Long id;private String name;private Integer price;@Versionprivate Integer version; }

添加樂觀鎖插件配置

@Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//添加分頁插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//添加樂觀鎖插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor; }

測試修改沖突

優化流程

@Test public void testConcurrentVersionUpdate() {//小李取數據Product p1 = productMapper.selectById(1L);//小王取數據Product p2 = productMapper.selectById(1L);//小李修改 + 50p1.setPrice(p1.getPrice() + 50);int result1 = productMapper.updateById(p1);System.out.println("小李修改的結果:" + result1);//小王修改 - 30p2.setPrice(p2.getPrice() - 30);int result2 = productMapper.updateById(p2);System.out.println("小王修改的結果:" + result2);if(result2 == 0){//失敗重試,重新獲取version并更新p2 = productMapper.selectById(1L);p2.setPrice(p2.getPrice() - 30);result2 = productMapper.updateById(p2);}System.out.println("小王修改重試的結果:" + result2);//老板看價格Product p3 = productMapper.selectById(1L);System.out.println("老板看價格:" + p3.getPrice()); }

總結

以上是生活随笔為你收集整理的MyBatis-Plus插件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。