MyBatisPlus-标准数据层的开发

MyBatisPlus-标准数据层的开发

标准数据层CRUD功能

功能 自定义接口
新增 boolean save(T t)
删除 boolean delete(int id)
修改 boolean update(T t)
根据id查询 T getById(int id)
查询全部 List<T> getAll()
分页查询 PageInfo<T> getAll(int page, int size)
按条件查询 List<T> getAll(Condition condition)

MP中的功能

功能 MP接口
新增 int insert(T t)
删除 int deleteById(Serializable id)
修改 int updateById(Serializable id)
根据id查询 T selectById(Serializable id)
查询全部 List<T> selectList()
分页查询 IPage<T> selectPage(IPage<T> page)
按条件查询 IPage<T> selectPage(Wrapper<T> queryWrapper)

分页查询

  1. 设置分页拦截器作为Spring管理的bean

    @Configuration
    public class MpConfig {
        @Bean
        public MybatisPlusInterceptor mpInterceptor(){
            // 1. 定义Mp拦截器
            MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
            // 2. 添加具体的拦截器
            mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            return mpInterceptor;
        }
    }
    
  2. 执行分页查询

    @Test
    void testGetByPage(){
        IPage page = new Page(1, 2);
        userDao.selectPage(page,null);
        System.out.println("当前页码值:" + page.getCurrent());
        System.out.println("每页显示数:" + page.getSize());
        System.out.println("一共多少页:" + page.getPages());
        System.out.println("一共多少条数据:" + page.getTotal());
        System.out.println("数据:" + page.getRecords());
    }