【精选】MybatisPlus最全教程

您所在的位置:网站首页 mybatisplus比mybatis好到哪 【精选】MybatisPlus最全教程

【精选】MybatisPlus最全教程

2023-11-08 02:24| 来源: 网络整理| 查看: 265

文章目录 1、介绍2、基于mybatis-plus的入门2.1、mybatis与mybatis-plus实现方式对比2.2、BaseMapper接口介绍2.2.1、如何理解核心接口BaseMapper?2.2.2、BaseMapper接口为我们定义了哪些方法?2.2.3、BaseMapper接口源码2.2.4、mybatis-plus中常用的注解 3、快速使用3.1引入依赖3.2、创建数据库3.3、配置application.yml3.4、创建pojo实体类3.5、增删查改操作 4、条件构造器:Wrapper4.1、7种构造器介绍4.1.1、Wrapper4.1.2、AbstractWrapper4.1.3、AbstractLambdaWrapper4.1.4、LambdaQueryWrapper4.1.5、LambdaUpdateWrapper4.1.6、QueryWrapper4.1.7、UpdateWrapper 4.2、带条件的CURD4.2.1、带条件的查询4.2.2、带条件的更新4.2.3、带条件的删除 4.3、wrapper查询实例 5、扩展5.1、全局ID生成策略5.2、逻辑删除5.3、执行SQL分析打印(好东西)5.3.1、依赖5.3.2、yml配置5.3.3、添加p6spy:spy.properties5.3.4、SQL 日志美化插件:5.3.5、数据安全保护5.3.5.1、得到16位随机秘钥5.3.5.2、根据秘钥加密 数据库连接信息5.3.5.3、修改配置文件 注意要mpw:开头(联系上面)5.3.5.4、在部署的时候需要解密 5.4、乐观锁插件使用5.4.1、介绍5.4.2、为什么需要锁(并发控制)5.4.3、MybatisPlus使用乐观锁 5.5、代码生成器

1、介绍

官网:https://baomidou.com/ 简介:MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

愿景: 我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

在这里插入图片描述 特性:

1、无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑 2、损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作 3、强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 4、支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错 5、支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配 置,完美解决主键问题 6、支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作 7、支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere ) 8、内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层 代码,支持模板引擎,更有超多自定义配置等您来使用 9、内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于 普通 List 查询 10、分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、 Postgre、SQLServer 等多种数据库 11、内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查 询 12、内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操 作

架构设计: 在这里插入图片描述

2、基于mybatis-plus的入门 2.1、mybatis与mybatis-plus实现方式对比

基于 Mybatis 需要编写 xxxMapper 接口,并手动编写 CRUD 方法 提供 xxxMapper.xml 映射文件,并手动编写每个方法对应的 SQL 语句. 基于 Mybatis-plus 只需要创建 xxxMapper 接口, 并继承BaseMapper 接口.这就是使用 mybatis-plus 需要完成的所有操作,甚至不需要创建 SQL 映射文件

2.2、BaseMapper接口介绍 2.2.1、如何理解核心接口BaseMapper?

在使用Mybatis-Plus是,核心操作类是BaseMapper接口,其最终也是利用的Mybatis接口编程的实现机制,其默认提供了一系列的增删改查的基础方法,并且开发人员对于这些基础操作不需要写SQL进行处理操作(Mybatis提供的机制就是需要开发人员在mapper.xml中提供sql语句),那样我们可以猜测肯定是Mybatis-Plus完成了BaseMapper接口提供的方法的SQL语句的生成操作。

2.2.2、BaseMapper接口为我们定义了哪些方法?

在这里插入图片描述

2.2.3、BaseMapper接口源码 package com.baomidou.mybatisplus.core.mapper; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; public interface BaseMapper { /** *

* 插入一条记录 *

* * @param entity 实体对象 */ int insert(T entity); /** *

* 根据 ID 删除 *

* * @param id 主键ID */ int deleteById(Serializable id); /** *

* 根据 columnMap 条件,删除记录 *

* * @param columnMap 表字段 map 对象 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** *

* 根据 entity 条件,删除记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 删除(根据ID 批量删除) *

* * @param idList 主键ID列表(不能为 null 以及 empty) */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList); /** *

* 根据 ID 修改 *

* * @param entity 实体对象 */ int updateById(@Param(Constants.ENTITY) T entity); /** *

* 根据 whereEntity 条件,更新记录 *

* * @param entity 实体对象 (set 条件值,不能为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); /** *

* 根据 ID 查询 *

* * @param id 主键ID */ T selectById(Serializable id); /** *

* 查询(根据ID 批量查询) *

* * @param idList 主键ID列表(不能为 null 以及 empty) */ List selectBatchIds(@Param(Constants.COLLECTION) Collection idList); /** *

* 查询(根据 columnMap 条件) *

* * @param columnMap 表字段 map 对象 */ List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap); /** *

* 根据 entity 条件,查询一条记录 *

* * @param queryWrapper 实体对象 */ T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询总记录数 *

* * @param queryWrapper 实体对象 */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 entity 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录 * 注意: 只返回第一个字段的值 *

* * @param queryWrapper 实体对象封装操作类(可以为 null) */ List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 entity 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */ IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); /** *

* 根据 Wrapper 条件,查询全部记录(并翻页) *

* * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ IPage selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); } 2.2.4、mybatis-plus中常用的注解

在这里插入图片描述

@TableName:对数据表名注解 @TableId:表主键标识 @TableId(value = "id", type = IdType.AUTO):自增 @TableId(value = "id", type = IdType.ID_WORKER_STR):分布式全局唯一ID字符串类型 @TableId(value = "id", type = IdType.INPUT):自行输入 @TableId(value = "id", type = IdType.ID_WORKER):分布式全局唯一ID 长整型类型 @TableId(value = "id", type = IdType.UUID):32位UUID字符串 @TableId(value = "id", type = IdType.NONE):无状态 @TableField:表字段标识 @TableField(exist = false):表示该属性不为数据库表字段,但又是必须使用的。 @TableField(exist = true):表示该属性为数据库表字段。 @TableField(condition = SqlCondition.LIKE):表示该属性可以模糊搜索。 @TableField(fill = FieldFill.INSERT):注解填充字段 ,生成器策略部分也可以配置! @FieldStrategy: @FieldFill @Version:乐观锁注解、标记 @EnumValue:通枚举类注解 @TableLogic:表字段逻辑处理注解(逻辑删除) @SqlParser:租户注解 @KeySequence:序列主键策略

更多请看https://baomidou.com/guide/annotation.html#tablefield

3、快速使用 3.1引入依赖 com.baomidou mybatis‐plus 3.3.1 com.baomidou mybatis‐plus‐boot‐starter 3.3.1 org.projectlombok lombok 1.18.12

注意:不需要再引用mybatis与mybatis-spring的maven依赖

3.2、创建数据库 CREATE TABLE `t_student` ( `sid` int(10) NOT NULL AUTO_INCREMENT, `s_name` varchar(100) NOT NULL, `sage` int(3) DEFAULT NULL, `ssex` char(1) DEFAULT NULL, `sphone` char(11) DEFAULT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; INSERT INTO `t_student` (`sid`, `s_name`, `sage`, `ssex`, `sphone`) VALUES ('4', '张三', '18', '1', '12345678912'); INSERT INTO `t_student` (`sid`, `s_name`, `sage`, `ssex`, `sphone`) VALUES ('5', '李四', '20', '1', '12467897452'); INSERT INTO `t_student` (`sid`, `s_name`, `sage`, `ssex`, `sphone`) VALUES ('8', '小丽', '15', '2', '4678'); INSERT INTO `t_student` (`sid`, `s_name`, `sage`, `ssex`, `sphone`) VALUES ('9', '赵六六', '15', '1', '7897564'); INSERT INTO `t_student` (`sid`, `s_name`, `sage`, `ssex`, `sphone`) VALUES ('10', '小特', '50', '1', '4564654');

在这里插入图片描述

3.3、配置application.yml # 设置开发环境 spring: profiles: active: dev #数据库连接 datasource: url: jdbc:mysql://localhost:3307/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource username: root password: 489773 # 配置日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置逻辑删除 global-config: db-config: logic-delete-value: 1 logic-not-delete-value: 0 3.4、创建pojo实体类 package com.zhz.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.extension.activerecord.Model; import java.io.Serializable; import java.util.Objects; /** * @author zhz * @date 2020/03/24 **/ @Data @AllArgsConstructor @NoArgsConstructor @ToString @TableName(value = "t_student") public class Student{ /* * @TableId: * value: 指定表中的主键列的列名, 如果实体属性名与列名一致,可以省略不指定. * type: 指定主键策略. */ @TableId(type = IdType.AUTO) private Integer sid; @TableField("s_name") private String sname; private Integer sage; private String ssex; private String sphone; } 3.5、增删查改操作

编写StudentMapper接口继承BaseMapper接口

package com.zhz.mapper; import org.apache.ibatis.annotations.Mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.pojo.Student; /** * @author zhz *基于Mybatis‐plus实现: 让XxxMapper接口继承 BaseMapper接口即可. *BaseMapper : 泛型指定的就是当前Mapper接口所操作的实体类类型 */ @Mapper public interface StudentMapperextends BaseMapper { }

准备测试类(直接套在平常的开发上也是可以的)

package com.zhz.test; import java.util.Map; import java.util.ArrayList; import java.util.HashMap; import com.zhz.mapper.StudentMapper; import com.bjsxt.pojo.Student; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * @author zhz * @date 2020/03/26 * mybatisPlus基本增删查改 **/ public class TestMybatisPlusBase { @Autowired private StudentMapper studentMapper; /** * 测试使用mp查询所有学生信息 */ @Test public void testSelAllStu() { //查询所有学生信息 List students = studentMapper.selectList(null); //输出结果 for (Student student:students) { System.out.println(student); } } /** * 测试使用Mp完成新增 */ @Test public void testIns() { //创建学生对象存储要新增的学生信息 Student student = new Student(); student.setSname("赵六六"); student.setSage(15); student.setSsex("1"); student.setSphone("7897564"); //新增学生信息 int insert = studentMapper.insert(student); //输出结果 System.out.println("添加得数量:"+insert); System.out.println("主键:"+student.getSid()); } /** * 测试使用Mp完成修改 */ @Test public void testUp(){ //创建学生对象存储要修改的学生信息 Student student = new Student(); student.setSid(6); student.setSage(20); //修改学生信息 int i = studentMapper.updateById(student); //输出结果 System.out.println("修改的条数:"+i); } /** * 删除:通过ID删除 */ @Test public void testDelById(){ //根据ID删除学生信息 int i = studentMapper.deleteById(7); //输出结果 System.out.println("删除的条数:"+i); } /** * 删除:指定条件删除数据(deleteByMap) */ @Test public void testDelByMap(){ Map map = new HashMap(); map.put("s_name","小红"); //指定条件删除学生信息 int i = studentMapper.deleteByMap(map); System.out.println("删除的条数:"+i); } /** * 删除:多选删除 */ @Test public void testDelByIds(){ List list = new ArrayList(); list.add(6); list.add(7); //删除符合Id要求的数据 int i = studentMapper.deleteBatchIds(list); System.out.println("删除的条数:"+i); } /** * 查询:通过ID查询 */ @Test public void testSelById(){ //根据ID查询学生信息 Student student = studentMapper.selectById(4); //输出结果 System.out.println(student); } /** * 查询:通过指定的条件完成查询 */ @Test public void testSelByMap(){ Map map = new HashMap (); map.put("s_name","张三"); //指定查询条件查询学生信息 List list = studentMapper.selectByMap(map); //输出结果 System.out.println(list); } /** * 查询:根据ID集合获取数据 */ @Test public void testSelectBatchIds(){ List list = new ArrayList(); list.add(4); list.add(5); //指定查询条件查询学生信息 List students = studentMapper.selectBatchIds(list); //输出结果 System.out.println(students); } } 4、条件构造器:Wrapper

结构: 在这里插入图片描述

4.1、7种构造器介绍 4.1.1、Wrapper

条件构造抽象类,最顶端父类,抽象类中提供3个方法以及其他方法 在这里插入图片描述

4.1.2、AbstractWrapper

用于查询条件封装,生成 sql 的 where 条件,QueryWrapper(LambdaQueryWrapper) 和UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where条件

在这里插入图片描述 重要的方法: 在这里插入图片描述

4.1.3、AbstractLambdaWrapper

Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。

4.1.4、LambdaQueryWrapper

用于Lambda语法使用的查询Wrapper

4.1.5、LambdaUpdateWrapper

Lambda 更新封装Wrapper

4.1.6、QueryWrapper

Entity 对象封装操作类,不是用lambda语法,自身的内部属性 entity 也用于生成 where 条件

在这里插入图片描述

select(String... sqlSelect) select(Predicate predicate) select(Class entityClass, Predicate predicate) /* 例: select("id", "name", "age") 例: select(i ‐> i.getProperty().startsWith("zhz")) */ 4.1.7、UpdateWrapper

Update 条件封装,用于Entity对象更新操作

在这里插入图片描述 set方法

set(String column, Object val) set(boolean condition, String column, Object val) /* SQL SET 字段 例: set("name", "zhz") 例: set("name", "")‐‐‐>数据库字段值变为空字符串 例: set("name", null)‐‐‐>数据库字段值变为null 说明:boolean condition为控制该字段是否拼接到最终的sql语句中 */

setSql方法

setSql(String sql) /* 设置 SET 部分 SQL 例: setSql("name = '老李头'") */ 4.2、带条件的CURD 4.2.1、带条件的查询 // 根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 entity 条件,查询全部记录 List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录 List selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 entity 条件,查询全部记录(并翻页) IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页) IPage selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询总记录数 Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); 4.2.2、带条件的更新 @Test void update() { UpdateWrapper updateWrapper=new UpdateWrapper(); updateWrapper.eq("s_name", "张三").eq("sage", 18).set("id", 100); empolyeeMapper.update(student, updateWrapper); } 4.2.3、带条件的删除 // 根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper wrapper); // 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap); 4.3、wrapper查询实例 package com.zhz; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zhz.mapper.StudentMapper; import com.zhz.pojo.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import java.util.Map; @SpringBootTest public class WrapperTest { @Autowired private StudentMapper studentMapper; @Test void contextLoads() { // 查询name不为空的用户,并且邮箱不为空的用户,年龄大于等于12 QueryWrapper wrapper = new QueryWrapper(); wrapper .isNotNull("s_name") .ge("sage",12); userMapper.selectList(wrapper).forEach(System.out::println); } @Test void test2(){ // 查询名字zhz QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("s_name","zhz"); User user = userMapper.selectOne(wrapper); // 查询一个数据,出现多个结果使用List 或者 Map System.out.println(user); } @Test void test3(){ // 查询年龄在 20 ~ 30 岁之间的用户 QueryWrapper wrapper = new QueryWrapper(); wrapper.between("sage",20,30); // 区间 Integer count = userMapper.selectCount(wrapper);// 查询结果数 System.out.println(count); } // 模糊查询 @Test void test4(){ // 查询年龄在 20 ~ 30 岁之间的用户 QueryWrapper wrapper = new QueryWrapper(); // 左和右 t% wrapper .notLike("s_name","e") .likeRight("sphone","131"); List maps = userMapper.selectMaps(wrapper); maps.forEach(System.out::println); } // 模糊查询 @Test void test5(){ QueryWrapper wrapper = new QueryWrapper(); // id 在子查询中查出来 wrapper.inSql("sid","select sid from student where sid


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3