• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mybatis-plus分页查询的实现示例

java 搞代码 4年前 (2022-01-05) 24次浏览 已收录 0个评论

这篇文章主要介绍了mybatis-plus分页查询的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

引入依赖:

 <!-- 引入mybatisPlus -->    com.baomidoumybatis-plus-boot-starter3.2.0<!-- 引入mysql驱动包 --> mysqlmysql-connector-java5.1.27<!-- 引入Druid依赖,阿里巴巴所提供的数据源 --> com.alibabadruid1.0.29     

在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 { } 

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

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

分页查询

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

新建一个config包,在里面建来源gaodaima#com搞(代@码网一个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 userPage = new Page(2, 2);//参数一是当前页,参数二是每页个数 userPage = userMapper.selectPage(userPage, null); List 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 userList; } 

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

Controller类贴上了@RestController注解

 @GetMapping("queryUser") public UserVo queryList(Integer current, Integer size) { /** * 这些代码应该写在service层 */ UserVo userVo = new UserVo(); IPage 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分页查询的实现示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mybatis-plus分页查询的实现示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址