• MyBatis-Plus(三、增删改查)


    作者:大三的土狗

    专栏:MyBatis-Plus
    在这里插入图片描述

    前言


      MyBatis非常方便,代码简洁,开发速度极高,通过继承BaseMapper就可以获取到各种各样的单表操作,无需再写其他的接口方法,非常的简便快捷。


      我们可以看到BaseMapper 为我们提供了很多方法供我们CRUD使用。
    在这里插入图片描述

    Mapper CRUD 接口

    说明:

    • 通用 CRUD 封装[BaseMapper 接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
    • 泛型 T 为任意实体对象
    • 参数 Serializable为任意类型主键 Mybatis-Plus不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
    • 对象 Wrapper为 条件构造器

    1、查询

    1、查询所有,不加条件去查询

    //1、查询所有,不加条件去查询
    userMapper.selectList(null).forEach(System.out::println);       //forEach遍历打印
    
    • 1
    • 2

    2、查询所有,加条件去查询

    //2、查询所有,加条件去查询
                //2.1、new QueryWrapper
            QueryWrapper queryWrapper1 = new QueryWrapper();
                //2.2、设置条件
            queryWrapper1.eq("age", 20);
      
            userMapper.selectList(queryWrapper1).forEach(System.out::println);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • lt #小于

      • gt #大于
      • ne #不等于
      • eq #等于
      • le #小于等于
      • ge #大于等于
      • 等等

    3、多条件去查询

    new一个hashmap,把条件放到map中,再把map放到条件中。

    QueryWrapper queryWrapper2 = new QueryWrapper();
               //3.1、设置多条件
               Map map = new HashMap<>();
               map.put("age",20);
               map.put("name","张三");
               //3.2、map放进queryWrapper
            queryWrapper2.allEq(map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、分页查询

    //4、分页查询
            //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);     //查询结果
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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<String,Object> 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<String,Object> 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<User> page = new Page<>(1,3);      //当前页,每页显示条数2
            Page<User> userPage = userMapper.selectPage(page, null);//分页参数,查询条件
    
            System.out.println(userPage.getCurrent());      //当前页
            System.out.println(userPage.getSize());     //每页显示条数
    
            userPage.getRecords().forEach(System.out::println);     //查询结果
    
    
            //封装到map中
            Page<Map<String,Object>> 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

    2、添加

        /**
         * Insert
         */
        @Test
        void insertTest(){
            User user = new User();
            user.setName("张三");
            user.setAge(20);
            userMapper.insert(user);
            System.out.println(user);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3、删除

        /**
         * Delete
         */
        @Test
        void deleteTest(){
            //通过id删除
            userMapper.deleteById(1);
    
            //通过多条件id删除
            userMapper.deleteBatchIds(Arrays.asList(1,2,3));
    
            //通过条件删除
            QueryWrapper queryWrapper = new QueryWrapper ();
            queryWrapper.eq("name","张三");       //与查询相同
            userMapper.delete(queryWrapper);
    
            //通过条件删除---Map
            Map<String,Object> map = new HashMap<>();
            map.put("name","张三");
            userMapper.deleteByMap(map);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    4、修改

       /**
         * Update
         */
        @Test
        void updateTest(){
    
            //通过id更新
            User user = userMapper.selectById(1);       //查询数据
            user.setName("李四");     //修改数据
            user.setAge(30);
            userMapper.updateById(user);
    
            //通过QueryWrapper更新
            User user1 = userMapper.selectById(1);      //查询数据
            QueryWrapper queryWrapper = new QueryWrapper();         //构建条件
            queryWrapper.eq("name","张三");
            queryWrapper.eq("age",20);
            userMapper.update(user1,queryWrapper);     //更新数据
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5、自定义 SQL(多表关联查询)

    当我们需要多表查询时,简单的crud满足不了我们的需求时,我们就需要通过自定义sql,来实现我们的所需的功能。

    package com.southwind.mybatisplus.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.southwind.mybatisplus.entity.ProductVO;
    import com.southwind.mybatisplus.entity.User;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface UserMapper extends BaseMapper<User> {
        @Select("select p.*,u.name userName from product p,user u where p.user_id = u.id and u.id = #{id}")
        List<ProductVO> productList(Integer id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    总结

      Mybatis 为我们提供了基本的增删改查的接口,特别是 Mybatis-Plus 提供的 Wrappers 更是可以组合出更复杂的查询语句以满足我们需求。但有些复杂的操作,比如联表查询等,这些就需要使用自定义 SQL 语句进行操作的。

  • 相关阅读:
    Mac os通过dmg安装docker以后在终端中使用 docker -v docker --version等找不到docker命令解决
    HCIA 访问控制列表ACL
    latex
    使用 TensorFlow 创建 DenseNet 121
    【Java】x-easypdf: 一种简单易用的PDF处理库
    iwemeta元宇宙:阿里首任COO:如何打造销售铁军
    PostgreSQL逻辑备份与恢复
    扩展我们的分析处理服务(Smartly.io):使用 Citus 对 PostgreSQL 数据库进行分片
    AD - 将修改后的 PCB 封装更新到当前 PCB 中
    1310. 数三角形
  • 原文地址:https://blog.csdn.net/qq_53463544/article/details/126358081