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插件相关推荐
- Mybatis Plugin插件安装破解及使用
为什么80%的码农都做不了架构师?>>> Mybatis Plugin 一.Mybatis Plugin插件是什么 提供Mapper接口与配置文件中对应SQL的导航 编辑XML ...
- Linux编译mybatis,使用mybatis assembly插件打成tar包,在linux系统中运行服务-Go语言中文社区...
使用mybatis assembly插件打成tar包,在linux系统中运行服务 assembly插件插件地址: 链接:https://pan.baidu.com/s/1i6bWPxF 密码:gad5 ...
- springboot整合mybatis分页插件
1.springboot版本为2.0.1,数据库为mysql,引入pagehelper的pom依赖 <!--mybatis分页插件--> <dependency><gro ...
- 解决使用mybatis分页插件PageHelper的一个报错问题
解决使用mybatis分页插件PageHelper的一个报错问题 参考文章: (1)解决使用mybatis分页插件PageHelper的一个报错问题 (2)https://www.cnblogs.co ...
- MyBatis分页插件PageHelper使用练习
转载自:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 1.环境准备: 分页插件p ...
- mybatis的插件分析
mybatis的插件分析 mybatis插件回在解析配置是通过pluginAll方法将插件添加到插件链中,然后会在sqlSessionfactory.openSession()方法中将插件链绑到exe ...
- 【MyBatis】MyBatis分页插件PageHelper的使用
转载自 https://www.cnblogs.com/shanheyongmu/p/5864047.html 好多天没写博客了,因为最近在实习,大部分时间在熟悉实习相关的东西,也没有怎么学习新的东西 ...
- java delegate怎么写_美团面试官:你说你们公司的Mybatis分页插件是你写的,给我说说它的设计原理?...
来源:http://my.oschina.net/zudajun 大多数框架,都支持插件,用户可通过编写插件来自行扩展功能,Mybatis也不例外. 我们从插件配置.插件编写.插件运行原理.插件注册与 ...
- MyBatis学习总结(17)——Mybatis分页插件PageHelper
2019独角兽企业重金招聘Python工程师标准>>> 如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊 ...
- Mybatis生成器插件扩展,生成OR操作
Mybatis生成器插件扩展,生成OR操作 ManExample example = new ManExample();ManExample.Criteria and = example.create ...
最新文章
- [转]应届毕业生生存法则--工作篇
- squid反向代理(实现缓存)加速web
- 【prometheus API】删除指定指标数据
- SpringBoot项目打成jar包后,无法读取resources下的文件
- android 官方教程中文版
- mysql表空间被占用,同名表无法创建或导入
- Java GUI简单点名器
- 17.如何正确使用TCP
- 【重磅】这家技术贼牛的开源公司开始狂招人啦!
- Visio 2003 开发入门
- python 普通克里金(Kriging)法
- 期权与期货有哪些不同?
- 通过Jsoup 和 htmlunit 爬取全国行政区划信息查询平台的省市区区划数据
- 结构光三维重建(二)线结构光三维重建
- 那些陪伴了我大学青春的网易博客也要停运啦
- hdwiki 附件上传大小设置
- 模拟支付宝、淘宝登录1
- android自动登录简书,Android 自动登录——持久化Cookie
- 在Ubuntu 14.04下安装Pepper Flash
- python dwg文件_Python将dwg文件转换为shapefi
热门文章
- 2020 第十一届蓝桥杯大赛软件类省赛第二场 C/C++ 大学 B 组 完整题面和题解
- win10开机自动打开http://go.microsoft.com/fwlink/?LinkID=219472clcid=0x409
- 什么是深度学习? 模仿人脑的算法
- 网易互娱2017实习生招聘游戏研发工程师在线笔试第二场(神奇的数)
- From CodingHorror: The Long, Dismal History of ...
- 搭建远程仓库(源)来托管 Sencha 包(Package)
- 一箭穿心程序编码c语言,一个简单的一箭穿心程序
- preHandle执行多次问题
- linux查看更多历史记录,查看更多历史,如何查看浏览历史记录
- 图文电视related