首先自定义业务层分页对象
/**
* 借助此类封装业务层分页信息
* 建议:所有用于封装数据的对象都实现Servializable接口(此接口是对象
* 是否可以序列化的标识)
* FAQ?
* 1、何为序列化和反序列
* 1)序列化:将对象转换为字节
* 2)反序列化:将字节转化为对象
* 2、序列化和反序列化应用场景
* 1)将对象转换为字节存储到内存或文件
* 2)将对象转为字节通过网络进行传输
* 3)java中对象的序列化实现
* 1)ObjectOutputStream(用于将对象序列化)
* 2)ObjectInputStream(将字节反序列化为对象)
* @param
*/
@Data
public class PageObject<T> implements Serializable {
//序列化和反序列化操作时的唯一标识,建议只要实现序列化接口,都要手动添加这个id
private static final long serialVersionUID = -3130527491950235344L;
/**总记录数*/
private Integer rowCount;
/**当前页记录*/
private List<T> records;
/**总页数*/
private Integer pageCount;
/**页面大小(每页最多显示多少条记录)*/
private Integer pageSize;
/**页码值*/
private Integer pageCurrent;
public PageObject() {}
public PageObject(Integer rowCount, List<T> records, Integer pageSize, Integer pageCurrent) {
this.rowCount = rowCount;
this.records = records;
this.pageSize = pageSize;
this.pageCurrent = pageCurrent;
this.pageCount = this.rowCount/this.pageSize;
if (this.rowCount%this.pageSize!=0)
this.pageCount++;
}
}
mapper.xml写sql语句
<!--基于条件查询当前页记录 -->
<select id="findPageObjects" resultType="com.cy.pj.sys.pojo.SysLog">
select *
<include refid="queryWhere"/>
order by createdTime desc
limit #{startIndex},#{pageSize}
</select>
<!-- 基于条件查询总记录数-->
<select id="getRowCount" resultType="int">
select count(*)
<include refid="queryWhere"/>
</select>
<!-- 通过sql元素提取共性 -->
<sql id="queryWhere">
from sys_logs
<where>
<if test="username!=null and username!=''">
username like concat("%",#{username},"%")
</if>
</where>
</sql>
业务层
@Override
public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
//1、对参数进行校验(可以自己校验,也可以借助框架:spring validation)
if (pageCurrent == null || pageCurrent < 1)
throw new IllegalArgumentException("页码值不合法");//无效的参数异常
//2、基于查询条件查询总记录数
int rowCount = sysLogDao.getRowCount(username);
if (rowCount == 0)
throw new ServiceException("没有找到对应的记录");
//3查询当前页记录
int pageSize = 5;//页面大小,每页最多显示多少条记录
int startIndex = (pageCurrent - 1) * pageSize;//当前页起始位置
List<SysLog> records = sysLogDao.findPageObjects(username, startIndex, pageSize);
//4、封装查询结果
// PageObject pageObject=new PageObject<>();
// pageObject.setRowCount(rowCount);
// pageObject.setRecords(records);
// pageObject.setPageSize(pageSize);
// pageObject.setPageCurrent(pageCurrent);
// pageObject.setPageCount(rowCount/pageSize);
// if (rowCount/pageSize!=0){
// Integer pageCount = pageObject.getPageCount();
// pageCount++;
// }
PageObject<SysLog> pageObject = new PageObject<>(rowCount, records, pageSize, pageCurrent);
return pageObject;
}
控制层
@Autowired
private SysLogService sysLogService;
@GetMapping("doFindPageObjects")
public JsonResult doFindPageObjects(String username, Integer pageCurrent) {
PageObject<SysLog> pageObjects = sysLogService.findPageObjects(username, pageCurrent);
return new JsonResult(pageObjects);
}
pom文件添加jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
Pojo对象设计及实现
创建SysUser类型,基于此类型的对象封装用户信息,关键代码如下:
package com.cy.pj.sys.pojo;
@Data
public class SysUser implements Serializable {
private static final long serialVersionUID = -8248568398323868047L;
private Integer id;
private String username;
private String password;
private String salt;
private String mobile;
private String email;
private Integer valid;
private Integer deptId;
private String deptName;
private Date createdTime;
private Date modifiedTime;
private String createdUser;
private String modifiedUser; }
Dao 接口及方法设计
第一步:创建SysUserDao接口及方法,用于实现分页查询,关键代码如下:
package com.cy.pj.sys.dao;
@Mapper
public interface SysUserDao{
List<SysUser> findPageObjects(String username); }
第二步:创建SysUserMapper映射文件并添加分页查询SQL映射,关键代码如下:
<mapper namespace="com.cy.pj.sys.dao.SysUserDao">
<select id="findPageObjects" resultType="com.cy.pj.sys.pojo.SysUser">
select u.*,d.name deptName from sys_users u left join sys_depts d on u.deptId=d.id <where>
<if test="username!=null and username!=''">
u.username like concat ("%",#{username},"%") </if>
</where> order by u.createdTime desc </select>
</mapper>
Service 接口及方法设计
package com.cy.pj.sys.service.impl;
@Service
public class SysUserServiceImpl implements SysUserService{
@Autowired private SysUserDao sysUserDao;
public PageObject<SysUser> findPageObjects(String username, Integer pageCurrent){
int pageSize=5;
//使用PageHelper封装分页信息
Page<SysUser> page=PageHelper.startPage(pageCurrent,pageSize);
List<SysUser> records=sysUserDao.findPageObjects(username);
//PageObject为自定类,封装了查询到的分页内容
return new PageObject((int)page.getTotal(),records, pageSize,pageCurrent); } }
Controller类及方法设计
@Autowired
private SysUserService sysUserService;
@GetMapping("doFindPageObjects")
public JsonResult doFindPageObjects(String username, Integer pageCurrent){
PageObject<SysUser> pageObject= sysUserService.findPageObjects(username,pageCurrent);
return new JsonResult(pageObject);
}
业务层完成分页查询
查询到的分页信息直接从IPage中获取
//封装分页信息,第一个参数页码值、第二个参数每页显示数量
//IPage iPage = new Page(pageNum, pageSize);
Page<Users> pages = new Page<Users>(pageNum, pageSize);
//进行分页查询
//iPage = usersMapper.selectPage(pages, queryWrapper);
IPage<Users> iPage = usersMapper.selectPage(pages, queryWrapper);
//获取分页查询记录
List<Users> data = iPage.getRecords();
//获取总记录
long count = iPage.getTotal();
编辑分页配置类
@Configuration //标识配置文件
public class MybatisPlusConfig {
@Bean //将数据交给spring容器管理
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(true);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}