java mysql vo mybatis 分页_使用mybatis-plus如何实现分页查询功能
使用mybatis-plus如何实现分页查询功能
发布时间:2020-11-06 17:34:12
来源:亿速云
阅读:538
作者:Leah
今天就跟大家聊聊有关使用mybatis-plus如何实现分页查询功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
引入依赖:
com.baomidou
mybatis-plus-boot-starter
3.2.0
mysql
mysql-connector-java
5.1.27
com.alibaba
druid
1.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> selectMaps(@Param("ew") Wrapper queryWrapper);
List selectObjs(@Param("ew") Wrapper queryWrapper);
IPage selectPage(IPage page, @Param("ew") Wrapper queryWrapper);
IPage> selectMapsPage(IPage page, @Param("ew") Wrapper queryWrapper);
}
分页查询
这点官方文档讲的也很详细:https://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 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如何实现分页查询功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
java mysql vo mybatis 分页_使用mybatis-plus如何实现分页查询功能相关推荐
- java mybatis 教程_(整理)MyBatis入门教程(一)
本文转载: 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几个网页: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache softw ...
- java实战:jsp快速实现简单易用的信息登记与查询功能
一.先看效果 本篇主要用于介绍信息登记以及查询功能,介绍jsp的数据录入和查询实现. 信息登记页面图示: 管理人员查询页面图示: 二.数据库准备 创建一个专用数据库和两张表: /*创建数据库*/ cr ...
- mybatis的mysql分页_使用MyBatis+Mysql实现分页的插件PageInfo使用介绍
使用方法 引入分页插件一共有下面2种方式,推荐使用Maven方式,这种方式方便更新,也是目前主流的依赖管理方式. 1. 引入Jar包 如果你想使用本项目的jar包而不是直接引入类,你可以在这里下载各个 ...
- 在mybatis用mysql的代码块_关于Mybatis 中使用Mysql存储过程的方法
1.存储过程的简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用 ...
- mysql读写分离插件_基于mybatis读写分离插件
我相信很多朋友都尝试写过读写分离插件,或者项目中用到过.首先读写分离的职责应该属于数据访问层而不是业务层,其次读写分离不应该侵入我们代码层中.因此在 service-dao-orm- 数据库驱动调用链 ...
- mysql简易oa系统_基于mybatis设计简单OA系统问题2
1. 标签用于格式化数字,百分比,货币. 教程: 2.错误: org.apache.jasper.JasperException: An exception occurred processing J ...
- mybatis 配置_配置Mybatis在Spring Boot工程中的整合
配置Mybatis在Spring Boot工程中的整合包,设置mybatis的实体类别名,输出执行sql语句配置项. 分析: 添加启动器依赖: 配置Mybatis:实体类别名包,日志,映射文件等: 配 ...
- mybatis 原理_了解Mybatis的工作原理吗
点击上方"Java知音",选择"置顶公众号" 技术文章第一时间送达! 作者:江南入直 cnblogs.com/scuury/p/10371246.html 推荐 ...
- mybatis 拼接_关于 Mybatis的 $ 和 # , 你真的知道他们的细节吗?
前言 在JDBC中,主要使用的是两种语句,一种是支持参数化和预编译的PrepareStatement,能够支持原生的Sql,也支持设置占位符的方式,参数化输入的参数,防止Sql注入,一种是支持原生Sq ...
最新文章
- Intel Realsense D435 如何设置曝光参数(exposure)
- Kotlin实战指南三:流程控制
- Spring框架第一天知识总结
- Nginx负载均衡策略之fair介绍
- html 登陆sql server,jsp实现注册与登录页面+sqlsever2008
- windows系统上安装与使用Android NDK r8d(二)
- debian下erlang新版本安装
- mysql多库备份_Mysql 之多库备份
- 7矩阵级数与矩阵函数
- 鸿蒙开放远程测试,跟阿斌一起学鸿蒙(3). 远程虚拟设备的限制和使用方法
- excel数据处理_如何用excel做仓库管理软件?这样做很好用,有权限和流程
- QQ查看撤回的语音消息,slk 文件转 mp3
- 稀疏矩阵计算器(三元组实现矩阵加减乘法)
- 拟物设计和Angular的实现 - Material Design
- Mac常用触摸板手势
- 搜狗拼音输入法2015 v7.5 官方版​
- Enterprise Architect(简称EA)使用技巧
- SPSS大学生毕业生就业问题分析
- python数据分析及可视化(九)pandas数据规整(分组聚合、数据透视表、时间序列、数据分析流程)
- 04 高性能网络设计专栏-网络编程