02-MyBatisPlus标准数据层开发

二、标准数据层开发

2.1、MyBatis-Plus的CRUD操作

  • 自己自定义的接口和MyBatis-Plus实现的接口对比,如下表所示

    • 功能 自定义接口 MyBatisPlus接口
      新增 boolean save(T t) int insert(T t)
      删除 boolean delete(int id) int deleteById(Serializable id)
      修改 boolean update(T t) int updateById(T t)
      根据id查询 T selectById(int id) T selectById(Serializable id)
      查询全部 List selectAll() List selectList()
      分页查询 PageInfo selectAll(int page, int size) IPage selectPage(IPage page)
      按条件查询 List selectAll(Condition condition) IPage selectPage(Wrapper queryWrapper)
  • 编写测试类,实现CRUD

    • package com.coolman.test;
      import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
      import com.coolman.mapper.UserMapper;
      import com.coolman.model.User;
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import java.util.List;
      @SpringBootTest
      @Slf4j
      public class MyBatisPlusTest {
      // 注入mapper层对象
      @Autowired(required = false)
      private UserMapper userMapper;
      @Test
      // 查询所有
      public void selectAllTest() {
      // selectList查询所有用户,参数为null
      List<User> users = userMapper.selectList(null);
      users.forEach(user -> {
      log.info(user + "");
      });
      }
      @Test
      // 根据id查询
      public void selectByIdTest() {
      User user = userMapper.selectById(1);
      log.info("根据id查询:::" + user + "");
      }
      @Test
      // 根据特定条件查询(如查询大于18岁)
      public void selectByConditionTest() {
      // 1. 创建一个条件对象
      QueryWrapper<User> queryWrapper = new QueryWrapper<>();
      // 2. 在条件对象中设置条件
      queryWrapper.gt("age", 18);
      // 3. 调用mapper接口
      List<User> userList = userMapper.selectList(queryWrapper);
      userList.forEach(user -> {
      log.info("条件查询(大于18岁):::" + user);
      });
      }
      @Test
      // 新增
      public void insertTest() {
      // 初始化一个user对象
      User user = new User();
      // user.setId(); // Mybatis-plus会实现随机id
      user.setAge(18);
      user.setName("猛男");
      user.setGender("男");
      user.setTel("18950184444");
      user.setPassword("123");
      // 调用mapper接口
      userMapper.insert(user);
      }
      // 修改
      @Test
      public void updateByIdTest() {
      User user = new User();
      user.setId(1541171229338210306L); // 刚才新增的时候的id
      user.setName("超级猛男");
      user.setGender("女");
      // 调用mapper接口
      userMapper.updateById(user);
      }
      // 删除
      @Test
      public void deleteByIdTest() {
      // 调用mapper接口
      userMapper.deleteById(1541171229338210306L);
      }
      }
    • 具体实现效果这里就不过多演示,有兴趣可以自己运行起来后仔细查看SQL语句

2.2、MyBatis-plus分页操作

  • 1、设置分页拦截器作为Spring管理的Bean

    • ①、在config包下创建一个配置类:MyBatisPlusConfig

    • ②、在类上添加@Configuration注解

    • ③、编写方法

      • 1.方法上使用@Bean注解:添加MybatisPlusInterceptor对象到容器中
      • 2.创建MybatisPlusInterceptor拦截器对象
      • 3.添加内部分页拦截器:创建PaginationInnerInterceptor
    • package com.coolman.config;
      import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
      import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      @Configuration
      public class MyBatisPlusConfig {
      /*
      在执行selectPage方法的时候,这个拦截器会帮助开发人员拼接limit语句
      从而实现分页
      */
      @Bean
      public MybatisPlusInterceptor createMybatisPlusInterceptor() {
      // 1. 创建Mybatis-Plus拦截器
      MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
      // 2. 给Mybatis-Plus拦截器添加分页拦截器
      mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
      // 3. 返回拦截器
      return mybatisPlusInterceptor;
      }
      }
  • 2、在测试类中执行分页查询

    • ①、创建分页对象,前面是接口IPage,后面是实现类Page(第几页,每页大小)

    • ②、调用selectPage方法,传入Page对象(无需返回值

    • ③、获取分页结果

    • // 分页查询
      @Test
      public void selectPageTest() {
      // 创建Page对象
      // 这个Page对象就相当于以前使用的PageInfo对象,封装整个分页的数据
      Page<User> userPage = new Page<>(1, 3); // 1 是页码, 3 是页面大小
      // 分页结果会封装到Page对象中
      userMapper.selectPage(userPage, null);
      log.info("当前页面:::" + userPage.getCurrent());
      log.info("页面大小:::" + userPage.getSize());
      log.info("页面数据:::" + userPage.getRecords());
      log.info("总记录数:::" + userPage.getTotal());
      log.info("总页数:::" + userPage.getPages());
      }
posted @   OnlyOnYourself-Lzw  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示