【精选】MybatisPlus最全教程 |
您所在的位置:网站首页 › mybatisplus比mybatis好到哪 › 【精选】MybatisPlus最全教程 |
文章目录
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,基友搭配,效率翻倍。
架构设计: 基于 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接口为我们定义了哪些方法?* 插入一条记录 * * * @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中常用的注解更多请看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');编写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结构: 条件构造抽象类,最顶端父类,抽象类中提供3个方法以及其他方法 用于查询条件封装,生成 sql 的 where 条件,QueryWrapper(LambdaQueryWrapper) 和UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where条件
Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。 4.1.4、LambdaQueryWrapper用于Lambda语法使用的查询Wrapper 4.1.5、LambdaUpdateWrapperLambda 更新封装Wrapper 4.1.6、QueryWrapperEntity 对象封装操作类,不是用lambda语法,自身的内部属性 entity 也用于生成 where 条件 Update 条件封装,用于Entity对象更新操作
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 |