• SpringBoot 整合 MyBatis-Plus


    作者:大三的土狗

    专栏:SpringBoot入门到精通
    专栏:MyBatis-Plus
    在这里插入图片描述


    前言

      整合MyBaitsPlus(简称MP),是在MyBatis的基础上再升级一下,国人开发的技术,符合中国人开发习惯,谁用谁知道,这里 有我整理的 SpringBoot 整合 MyBatis 的详细步骤 。

    SpringBoot 整合是十分便捷的,整合究竟核心如下:

    • 导入对应技术的starter坐标
    • 根据对应技术的要求做配置

    1、导入对应的starter

    
        com.baomidou
        mybatis-plus-boot-starter
        3.4.3
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、starter写法

    starter所属命名规则示例
    官方提供spring-boot-starter-技术名称spring-boot-starter-web
    spring-boot-starter-test
    第三方提供第三方技术名称-spring-boot-starterdruid-spring-boot-starter
    第三方提供第三方技术名称-boot-starter(第三方技术名称过长,简化命名)mybatis-plus-boot-starter

    3、配置数据源相关信息

    #2.配置相关信息
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mp
        username: root
        password: root
        
      mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl     # 日志实现类  -->打印sql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、映射接口(Mapper)

    public interface UserMapper extends BaseMapper {
    
    }
    
    • 1
    • 2
    • 3

      核心在于Dao接口继承了一个BaseMapper的接口,这个接口中帮助开发者预定了若干个常用的API接口,简化了通用API接口的开发工作。

    在这里插入图片描述

    5、测试类

    @Test
        void select() {
            //1、查询所有,不加条件去查询
            userMapper.selectList(null).forEach(System.out::println);       //forEach遍历打印
    
            //2、查询所有,加条件去查询
                //2.1、new QueryWrapper
            QueryWrapper queryWrapper1 = new QueryWrapper();
                //2.2、设置条件
            queryWrapper1.eq("age", 20);
            /**
             * lt           #小于
             * gt       #大于
             * ne       #不等于
             * eq       #等于
             * le           #小于等于
             * ge       #大于等于
             * between      #between
             * like like    模糊查询        #likeLeft 左模糊  likeRight 右模糊
             * isNull
             * isNotNull
             * in                         #inSql in sql语句
             * notIn
             * orderBy                  #排序 ASC DESC 升序降序
             * orderByDesc
             */
            userMapper.selectList(queryWrapper1).forEach(System.out::println);
    
            //3、多条件去查询
            QueryWrapper queryWrapper2 = new QueryWrapper();
                //3.1、设置多条件
                Map map = new HashMap<>();
                map.put("age",20);
                map.put("name","张三");
                //3.2、map放进queryWrapper
            queryWrapper2.allEq(map);
    
            //byId
            User user = userMapper.selectById(1);
            System.out.println(user);
    
            //byBatchIds
            userMapper.selectBatchIds(Arrays.asList(1,2,3)).forEach(System.out::println);       //Arrays.asList(1,2,3)是一个数组,把数组转换成list集合
    
            //通过map条件查询
            //map只能是一个条件,不能是多个条件
            Map map1 = new HashMap<>();
            map1.put("age",20);
            map1.put("name","张三");
            userMapper.selectByMap(map).forEach(System.out::println);
    
            //4、分组查询
            QueryWrapper queryWrapper3 = new QueryWrapper();
            queryWrapper3.gt("age",20);
            System.out.println(userMapper.selectCount(queryWrapper3));
    
            //将查询结果放入map中
            userMapper.selectMaps(queryWrapper1).forEach(System.out::println);
    
            //分页查询
            //1、配置类paginationInterceptor
            //2、设置分页参数
            Page page = new Page<>(1,3);      //当前页,每页显示条数2
            Page userPage = userMapper.selectPage(page, null);//分页参数,查询条件
    
            System.out.println(userPage.getCurrent());      //当前页
            System.out.println(userPage.getSize());     //每页显示条数
    
            userPage.getRecords().forEach(System.out::println);     //查询结果
    
    
            //封装到map中
            Page> mapPage = new Page<>(1,3);     //分页参数,查询条件
            userMapper.selectMapsPage(mapPage, null).getRecords().forEach(System.out::println);     //查询结果
    
            //查询所有,只输出id
            userMapper.selectObjs(null).forEach(System.out::println);     //查询结果
    
            //查询一个
            System.out.println(userMapper.selectOne(null));     //查询结果
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    6、表的通用前缀配置

    mybatis-plus:
      global-config:
        db-config:
          table-prefix: tb_		#设置所有表的通用前缀名称为tb_
    
    • 1
    • 2
    • 3
    • 4

    7、MybatisX 快速开发插件

    MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。

    • 安装方法:打开 IDEA,进入 File -> Settings -> Plugins -> Browse Repositories,输入 mybatisx 搜索并安装。
      在这里插入图片描述

    • 功能:

      • Java 与 XML 调回跳转

      • Mapper 方法自动生成 XML
        在这里插入图片描述
        在这里插入图片描述


    总结

    1. 手工添加MyBatis-Plus对应的starter
    2. 数据层接口使用BaseMapper简化开发
    3. 需要使用的第三方技术无法通过勾选确定时,需要手工添加坐标
  • 相关阅读:
    visual studio code 创建 SSH 远程连接
    mycat分库分表实战
    云端IDE的技术选型1
    营销妙招:如何让产品与消费者发生关系?链动2+1模式分享
    美团笔试真题2023第一场(4题)
    力扣:647. 回文子串
    MySQL数据库之JDBC编程
    2.360 度视频的流式传输技术
    mysql基于SSM的自习室管理系统毕业设计源码201524
    SpringBoot SpringBoot 运维实用篇 2 配置高级 2.2 临时属性【开发环境】
  • 原文地址:https://blog.csdn.net/qq_53463544/article/details/126400378