如何实现mybatis-plus的详细分页查询示例?

2026-05-21 10:405阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计945个文字,预计阅读时间需要4分钟。

如何实现mybatis-plus的详细分页查询示例?

根据要求,以下是对原文的简化修改:

配置步骤:

1.引入mybatis-plus依赖:

groupId: com.baomidou artifactId: mybatis-plus-boot-starter version: 3.2.0

2.引入MySQL驱动。

按照官方文档进行的配置:快速开始|mybatis-plus

引入依赖:

如何实现mybatis-plus的详细分页查询示例?

<!-- 引入mybatisPlus -->   <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 引入mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 引入Druid依赖,阿里巴巴所提供的数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version>     </dependency>

在application.yml配置

spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456

在启动类上面添加@MapperScan注解,扫描mapper包

@SpringBootApplication @MapperScan("com.qiao.demo02.mapper") public class SpringbootDemo02Application { public static void main(String[] args) { SpringApplication.run(SpringbootDemo02Application.class, args); } }

新建User和UserMapper

user类

@Data public class User { @TableId private Integer userId; private String userName; private Integer userAge; private String userEmail; }

UserMapper接口

public interface UserMapper extends BaseMapper<User> { }

最重要的是继承BaseMapper<E>接口:里面声明了很强大的CRUD方法

public interface BaseMapper<T> extends Mapper<T> { int insert(T entity); int deleteById(Serializable id); int deleteByMap(@Param("cm") Map<String, Object> columnMap); int delete(@Param("ew") Wrapper<T> wrapper); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList); int updateById(@Param("et") T entity); int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper); T selectById(Serializable id); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); T selectOne(@Param("ew") Wrapper<T> queryWrapper); Integer selectCount(@Param("ew") Wrapper<T> queryWrapper); List<T> selectList(@Param("ew") Wrapper<T> queryWrapper); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper); List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper); IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); }

分页查询

这点官方文档讲的也很详细:mp.baomidou.com/guide/page.html

新建一个config包,在里面建一个MybatisPlus配置类 返回一个分页拦截器

package com.qiao.demo02.config; @Configuration @ConditionalOnClass(value = {PaginationInterceptor.class}) public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }

这样就能使用mybatis的分页功能了

Junit测试

@Resource private UserMapper userMapper; @Test public void queryUserForPage(){ IPage<User> userPage = new Page<>(2, 2);//参数一是当前页,参数二是每页个数 userPage = userMapper.selectPage(userPage, null); List<User> list = userPage.getRecords(); for(User user : list){ System.out.println(user); } }

Controller返回json串

先定义一个包装类UserVo,用来保存分页所需要的数据

package com.qiao.demo02.vo; @Data public class UserVo { private Integer current; private Integer size; private Long total; private List<User> userList; }

然后在控制器编写代码,这里省略了service层,实际开发业务代码写在service层,Controller只负责:接受参数、调用service层方法处理业务逻辑,返回结果

Controller类贴上了@RestController注解

@GetMapping("queryUser") public UserVo queryList(Integer current, Integer size) { /** * 这些代码应该写在service层 */ UserVo userVo = new UserVo(); IPage<User> page = new Page<>(current, size); userMapper.selectPage(page, null); userVo.setCurrent(current); userVo.setSize(size); userVo.setTotal(page.getTotal()); userVo.setUserList(page.getRecords()); return userVo; }

附上结果,前端直接处理json数据即可

到此这篇关于mybatis-plus分页查询的实现示例的文章就介绍到这了,更多相关mybatis-plus 分页查询 内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计945个文字,预计阅读时间需要4分钟。

如何实现mybatis-plus的详细分页查询示例?

根据要求,以下是对原文的简化修改:

配置步骤:

1.引入mybatis-plus依赖:

groupId: com.baomidou artifactId: mybatis-plus-boot-starter version: 3.2.0

2.引入MySQL驱动。

按照官方文档进行的配置:快速开始|mybatis-plus

引入依赖:

如何实现mybatis-plus的详细分页查询示例?

<!-- 引入mybatisPlus -->   <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 引入mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <!-- 引入Druid依赖,阿里巴巴所提供的数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version>     </dependency>

在application.yml配置

spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456

在启动类上面添加@MapperScan注解,扫描mapper包

@SpringBootApplication @MapperScan("com.qiao.demo02.mapper") public class SpringbootDemo02Application { public static void main(String[] args) { SpringApplication.run(SpringbootDemo02Application.class, args); } }

新建User和UserMapper

user类

@Data public class User { @TableId private Integer userId; private String userName; private Integer userAge; private String userEmail; }

UserMapper接口

public interface UserMapper extends BaseMapper<User> { }

最重要的是继承BaseMapper<E>接口:里面声明了很强大的CRUD方法

public interface BaseMapper<T> extends Mapper<T> { int insert(T entity); int deleteById(Serializable id); int deleteByMap(@Param("cm") Map<String, Object> columnMap); int delete(@Param("ew") Wrapper<T> wrapper); int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList); int updateById(@Param("et") T entity); int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper); T selectById(Serializable id); List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); T selectOne(@Param("ew") Wrapper<T> queryWrapper); Integer selectCount(@Param("ew") Wrapper<T> queryWrapper); List<T> selectList(@Param("ew") Wrapper<T> queryWrapper); List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper); List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper); IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper); }

分页查询

这点官方文档讲的也很详细:mp.baomidou.com/guide/page.html

新建一个config包,在里面建一个MybatisPlus配置类 返回一个分页拦截器

package com.qiao.demo02.config; @Configuration @ConditionalOnClass(value = {PaginationInterceptor.class}) public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }

这样就能使用mybatis的分页功能了

Junit测试

@Resource private UserMapper userMapper; @Test public void queryUserForPage(){ IPage<User> userPage = new Page<>(2, 2);//参数一是当前页,参数二是每页个数 userPage = userMapper.selectPage(userPage, null); List<User> list = userPage.getRecords(); for(User user : list){ System.out.println(user); } }

Controller返回json串

先定义一个包装类UserVo,用来保存分页所需要的数据

package com.qiao.demo02.vo; @Data public class UserVo { private Integer current; private Integer size; private Long total; private List<User> userList; }

然后在控制器编写代码,这里省略了service层,实际开发业务代码写在service层,Controller只负责:接受参数、调用service层方法处理业务逻辑,返回结果

Controller类贴上了@RestController注解

@GetMapping("queryUser") public UserVo queryList(Integer current, Integer size) { /** * 这些代码应该写在service层 */ UserVo userVo = new UserVo(); IPage<User> page = new Page<>(current, size); userMapper.selectPage(page, null); userVo.setCurrent(current); userVo.setSize(size); userVo.setTotal(page.getTotal()); userVo.setUserList(page.getRecords()); return userVo; }

附上结果,前端直接处理json数据即可

到此这篇关于mybatis-plus分页查询的实现示例的文章就介绍到这了,更多相关mybatis-plus 分页查询 内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!