MyBatis-Plus学习笔记

1.MyBatis-Plus简介:

MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。MyBatis-Plus提供了通用的mapper和service,可以在不编写任何SQL语句的情况下,快速的实现对单表的CRUD批量、逻辑删除、分页等操作。

2.BaseMapper提供的CRUD功能

2.1 添加功能:

java 复制代码
    @Test
    public void insert(){
        User user = new User();
        user.setName("users");
        user.setAge(43);
        user.setEmail("users@xja.com");
        int result = this.userMapper.insert(user);
    }

2.2 删除功能:

2.2.1 deleteById:

java 复制代码
    @Test
    public void deleteById(){
        int result = this.userMapper.deleteById(1696801209841856514L);
    }

2.2.2 deleteByMap:

java 复制代码
    @Test
    public void deleteByMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","admin");
        map.put("age",40);
        int result = this.userMapper.deleteByMap(map);
    }

2.2.3 deleteBatchIds(批量删除):

java 复制代码
    @Test
    public void deleteBatchIds(){
        List<Long> ids = Arrays.asList(1L, 2L, 3L);
        int result = this.userMapper.deleteBatchIds(ids);
    }

2.2.4 delete(根据条件删除):

java 复制代码
@Test
public void delete() {
    User user = new User();
    user.setName("zhangsan");
    user.setAge(40);

    QueryWrapper<User> wrapper = new QueryWrapper<>(user);
    this.userMapper.delete(wrapper);
}

2.3 修改功能:

2.3.1 updateById:

java 复制代码
    @Test
    public void updateById(){
        User user = new User();
        user.setId(1L);
        user.setName("Tom");
        user.setAge(40);
        int result = this.userMapper.updateById(user);
    }

2.3.2 update(根据条件修改):

java 复制代码
    @Test
    public void update() {
        User user = new User();
        user.setAge(80); // 需要更新的字段
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "Jack"); // 设置更新条件
        // 执行更新操作
        int result = this.userMapper.update(user, wrapper);
    }

2.4 查询功能:

2.4.1 selectById:

java 复制代码
@Test
public void selectById() {
    User user = this.userMapper.selectById(1L);
    System.out.println(user);
}

2.4.2 selectBatchIds:

java 复制代码
@Test
public void selectBatchIds() {
    List<Long> ids = Arrays.asList(1L, 2L, 3L, 4L, 5L);
    List<User> users = this.userMapper.selectBatchIds(ids);
    users.forEach(System.out::println);
}

2.4.3 selectOne:

java 复制代码
@Test
public void selectOne() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("name", "Tom");
    // 只能查询一条数据,如果超过一条则报错
    User user = this.userMapper.selectOne(wrapper);
    System.out.println(user);
}

2.4.4 selectAll:

java 复制代码
    @Test
    public void selectAll(){
        //通过条件构造器查询一个list集合,若没有条件,则可以设置null 为参数
        List<User> userList = this.userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

2.4.5 selectCount(根据wrapper查询中记录数):

java 复制代码
@Test
public void selectCount() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.gt("age", 23); //查询年龄大于23岁的成员信息
    Integer count = this.userMapper.selectCount(wrapper);
    System.out.printf("总记录数:%d", count);
}

2.4.6 selectList:

java 复制代码
@Test
public void selectList() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.lt("age", 50); // 年龄小于50岁
    List<User> users = this.userMapper.selectList(wrapper);
    users.forEach(System.out::println);
}
相关推荐
老虎062721 分钟前
JavaWeb(苍穹外卖)--学习笔记13(微信小程序开发,缓存菜品,Spring Cache)
笔记·学习·微信小程序
@蓝莓果粒茶2 小时前
LeetCode第350题_两个数组的交集II
c++·python·学习·算法·leetcode·职场和发展·c#
无名工程师2 小时前
AI 学习过程中各阶段的学习重点、时间规划以及不同方向的选择与建议等内容
人工智能·学习
试着2 小时前
零基础学习性能测试第五章:JVM性能分析与调优-垃圾回收器的分类与回收
jvm·学习·零基础·性能测试·垃圾回收器
livemetee2 小时前
Flink2.0学习笔记:Stream API 常用转换算子
大数据·学习·flink
WXX_s2 小时前
【OpenCV篇】OpenCV——03day.图像预处理(2)
人工智能·python·opencv·学习·计算机视觉
qhd吴飞3 小时前
mybatis 差异更新法
java·前端·mybatis
艾莉丝努力练剑3 小时前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
YGY Webgis糕手之路3 小时前
OpenLayers 快速入门(九)Extent 介绍
前端·经验分享·笔记·vue·web
花月mmc3 小时前
CanMV-K230 AI学习笔记系列
人工智能·笔记·学习