文章目录
1 摘要
2 官方文档
3 核心依赖
4 核心代码
4.1 方法应用
4.2 Mapper 文件
4.3 DAO 接口
1 摘要
每个项目中都会用到分页查询,这一技术具有高度的复用性,为了避免每次创建新项目时重新编写一边分页查询,或者因为项目的数据库更换而重新编写分页代码,将分页查询提取出来封装成专门的第三方库就显得十分必要。本文将介绍基于 Spring Boot 2.X 集成 Mybatis PageHelper 分页插件。
2 官方文档
MyBatis Pagination - PageHelper
3 核心依赖
<!-- mybatis pageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${mybatis.pagehelper.version}</version>
</dependency>
其中 ${mybatis.pagehelper.version} 的版本为 1.2.12
4 核心代码
4.1 方法应用
package com.ljq.demo.springboot.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljq.demo.springboot.common.api.ApiResult;
import com.ljq.demo.springboot.common.page.QueryUtil;
import com.ljq.demo.springboot.dao.article.ArticleDao;
import com.ljq.demo.springboot.entity.ArticleEntity;
import com.ljq.demo.springboot.service.ArticleService;
import com.ljq.demo.springboot.vo.article.ArticleListParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 文章表业务层具体实现类
*
* @author junqiang.lu
* @date 2019-11-25 14:01:38
*/
@Service("articleService")
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleDao articleDao;
/**
* 查询列表
*
* @param articleListParam
* @return
* @throws Exception
*/
@Override
public ApiResult list(ArticleListParam articleListParam) throws Exception {
long start = System.currentTimeMillis();
QueryUtil queryMap = new QueryUtil(BeanUtil.beanToMap(articleListParam, false, true));
PageInfo<ArticleEntity> pageInfo = PageHelper.startPage(articleListParam.getCurrPage(), articleListParam.getPageLimit())
.setOrderBy(articleListParam.getProperties() + " " + articleListParam.getDirection())
.doSelectPageInfo(() -> articleDao.queryListPage(queryMap));
long end = System.currentTimeMillis();
log.info("查询耗时: {}", (end - start));
return ApiResult.success(pageInfo);
}
}
关于 PageHelper 的一些方法说明:
startPage: 设置查询起始点与每页显示条数
setOrderBy: 设置排序规则
doSelectPageInfo: 需要执行的查询语句,Mybatis Mapper 中定义的接口(支持 lambda 表达式),返回的结果是分页信息PageInfo(包含结果列表以及分页参数)
doSelectPage: 也是执行查询语句,与 doSelectPageInfo 一样,只是返回结果不同,doSelectPage 返回的是 Page, 如果将这个结果返回至前端,则只有数据列表,而没有分页信息。Page 可转化为 PageInfo
注意 : 方法顺序必须是 startPage -> setOrderBy -> doSelectPage ,若将 setOrderBy 放在 doSelectPage 后边,则排序不生效(亲测结果是这样的)
4.2 Mapper 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ljq.demo.springboot.dao.article.ArticleDao">
<!-- 文章表结果集resultMap -->
<resultMap type="com.ljq.demo.springboot.entity.ArticleEntity" id="articleMap">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<collection property="tagList" column="id" javaType="java.util.List" ofType="com.ljq.demo.springboot.entity.ArticleTagEntity"
select="com.ljq.demo.springboot.dao.article.ArticleTagDao.queryTagsByArticleId">
<id property="id" column="id" />
<result property="tagName" column="tag_name" />
</collection>
</resultMap>
<!-- 文章表-基础字段 -->
<sql id="article_base_field">
a.`id`,
a.`title`,
a.`content`
</sql>
<!-- 文章表-列表字段 -->
<sql id="article_list_field">
<include refid="article_base_field" />
,
at.`id` at_id,
at.`tag_name` at_tag_name
</sql>
<!-- 列表查询 -->
<select id="queryListPage" parameterType="java.util.Map" resultMap="articleMap">
SELECT
<include refid="article_base_field" />
FROM article a
LEFT JOIN `article_to_tag` att ON att.article_id = a.id
LEFT JOIN `article_tag` at ON at.id = att.tag_id
WHERE 1 = 1
<if test="title != null and '' != title">
AND a.title LIKE CONCAT(CONCAT("%", #{title}), "%")
</if>
<if test="articleTag != null and '' != articleTag">
AND at.tag_name LIKE CONCAT(CONCAT("%", #{articleTag}), "%")
</if>
GROUP BY a.id
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ljq.demo.springboot.dao.article.ArticleTagDao">
<!-- 文章标签表结果集resultMap -->
<resultMap type="com.ljq.demo.springboot.entity.ArticleTagEntity" id="articleTagMap">
<result property="id" column="id"/>
<result property="tagName" column="tag_name"/>
</resultMap>
<!-- 文章标签表-基础字段 -->
<sql id="article_tag_base_field">
at.`id`,
at.`tag_name`
</sql>
<!-- 查询某一篇文章的标签(列表) S -->
<select id="queryTagsByArticleId" parameterType="long" resultMap="articleTagMap">
SELECT
<include refid="article_tag_base_field" />
FROM `article_to_tag` att
LEFT JOIN `article_tag` at ON at.id = att.tag_id
LEFT JOIN `article` a ON a.id = att.article_id
WHERE a.id = #{id}
</select>
<!-- 查询某一篇文章的标签(列表) E -->
</mapper>
注意: 在一对多的关系中,可以使用 collection 来对查询结果进行封装。在 collection 标签中有 select 属性,用于将一个子查询结果封装到 resultMap 中,这个查询可以为本 Mapper 文件中的查询,也可以是其他 Mapper 文件的查询,如果为其他 Mapper 中的查询,则需要指定具体的 Mapper ,具体参考示例中ArticleDao.xml的代码
4.3 DAO 接口
package com.ljq.demo.springboot.dao.article;
import com.ljq.demo.springboot.dao.BaseDao;
import com.ljq.demo.springboot.entity.ArticleTagEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 文章标签表
*
* @author junqiang.lu
* @date 2019-11-25 14:01:38
*/
@Repository
public interface ArticleTagDao extends BaseDao<ArticleTagEntity> {
/**
* 查询某一篇文章的标签(列表)
*
* @param articleId
* @return
*/
List<ArticleTagEntity> queryTagsByArticleId(@Param("articleId") long articleId);
}
package com.ljq.demo.springboot.dao.article;
import com.ljq.demo.springboot.dao.BaseDao;
import com.ljq.demo.springboot.entity.ArticleEntity;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* 文章表
*
* @author junqiang.lu
* @date 2019-11-25 14:01:38
*/
@Repository
public interface ArticleDao extends BaseDao<ArticleEntity> {
/**
* 查询列表
*
* @param queryMap
* @return
*/
List<ArticleEntity> queryListPage(Map<String, Object> queryMap);
}
7 参考文档推荐
官方文档 MyBatis Pagination - PageHelper
官方文档 How to use PageHelper
官方文档 PageHelper integration with Spring Boot
原文链接:https://blog.csdn.net/Mrqiang9001/article/details/103278234