java使用插件pagehelper在mybatis中实现分页查询
摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件
PageHelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有中文注释
开源项目地址: https://pagehelper.github.io/
请求URL:http://localhost:8080/listCity?page=1&limit=10
显示数据:

1、PageHelper的maven依赖及插件配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
PageHelper除了本身的jar包外,它还依赖了一个叫jsqlparser的jar包,使用时,我们不需要单独指定jsqlparser的maven依赖,maven的间接依赖会帮我们引入。
2、配置拦截器插件
这个是配置在mybatis-config.xml文件中
文档中的示例
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3、我的配置mybatis-config.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<typeAliases>
<package name="edu.nf.entity"/>
</typeAliases>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--helperDialect 方言:就表示此插件针对哪个数据库进行优化处理
这个方言可以不配置,因为此插件可以依据你的 url 的信息来推断出
你用的数据库是哪一个
-->
<property name="helperDialect" value="mysql"/>
<!--分页合理化参数-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
<!--配置数据库-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/citydb?useSSL=true&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="mapper/city-mapper.xml"/>
</mappers>
</configuration>
4、city-mapper.xml 数据库查询语句配置:
<?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="edu.nf.dao.CityDao">
<resultMap id="cityMap" type="city" >
<id property="cityId" column="city_id"/>
<result property="cityEn" column="city_en"/>
<result property="cityCn" column="city_cn"/>
<result property="countryCode" column="country_code"/>
<result property="countryEn" column="country_en"/>
<result property="countryCn" column="country_cn"/>
<result property="provinceEn" column="province_en"/>
<result property="provinceCn" column="province_cn"/>
</resultMap>
<!-- 这里写查询全部数据,配置好的分页插件他会自己加上limit 查询语句后面不能加; -->
<select id="listCity" resultMap="cityMap">
select * from city_test
</select>
<delete id="deleteCity" parameterType="java.util.List">
delete from city_test where city_id in
<foreach collection="list" item="city" open="(" separator="," close=")">
#{city.cityId}
</foreach>
</delete>
</mapper>
5、后台分页查询 servlet:
/**
* @author hh
* @Date 2018/9/15
*/
@WebServlet("/listCity")
public class CityListServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json;charset=utf-8");
//取出前端请求参数
String page=req.getParameter("page");
String limit=req.getParameter("limit"); //分页查询结果 page页数 limit显示行数
List<City> listCity=new CityService().listCity(page,limit); // 包装Page对象 listCity:page结果 , navigatePages: 页码数量
PageInfo<City> list=new PageInfo<>(listCity,1); //自己写的一个响应视图类,因为前端用的是layui框架需要自己,所以自己定义ResponseView
ResponseView vo=new ResponseView();
//设值 取出总数据行
vo.setCount(list.getTotal());
//设值 查询的结果
vo.setData(list.getList());
//响应前端
resp.getWriter().print(new Gson().toJson(vo));
}
}
6、响应视图类 ResponseView (因为前端用的是layui框架需要自己,所以自己定义ResponseView):
package edu.nf.vo; /**
* @author hh
* @Date 2018/9/15
*/
public class ResponseView {
private int code =0;
private Long count=0L;
private Object data; public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public Long getCount() {
return count;
} public void setCount(Long count) {
this.count = count;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
}
}
7、实体类 City:
package edu.nf.entity; /**
* @author hh
* @Date 2018/9/14
*/
public class City {
private String cityId;
private String cityEn;
private String cityCn;
private String countryCode;
private String countryEn;
private String countryCn;
private String provinceEn;
private String provinceCn; public String getCityId() {
return cityId;
} public void setCityId(String cityId) {
this.cityId = cityId;
} public String getCityEn() {
return cityEn;
} public void setCityEn(String cityEn) {
this.cityEn = cityEn;
} public String getCityCn() {
return cityCn;
} public void setCityCn(String cityCn) {
this.cityCn = cityCn;
} public String getCountryCode() {
return countryCode;
} public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
} public String getCountryEn() {
return countryEn;
} public void setCountryEn(String countryEn) {
this.countryEn = countryEn;
} public String getCountryCn() {
return countryCn;
} public void setCountryCn(String countryCn) {
this.countryCn = countryCn;
} public String getProvinceEn() {
return provinceEn;
} public void setProvinceEn(String provinceEn) {
this.provinceEn = provinceEn;
} public String getProvinceCn() {
return provinceCn;
} public void setProvinceCn(String provinceCn) {
this.provinceCn = provinceCn;
}
}
8、service 逻辑业务层(CityService):
/**
* @author hh
* @Date 2018/9/15
*/
public class CityService {
/**
* 分页查询 城市信息集合
* @return
*/
public List<City> listCity(String offest,String pageSize){
//类型转换
Integer pnum=Integer.valueOf(offest);
Integer psize=Integer.valueOf(pageSize);
//调用PageHelper获取第1页,10条内容,默认查询总数count
PageHelper.startPage(pnum,psize);
//调用CityDaoImpl 分页查询
return new CityDaoImpl().listCity();
} /**
* 批量删除
* @param cityData
* @return
*/
public int deleteCity(String cityData){
List<City> list=new Gson().fromJson(cityData,new TypeToken<List<City>>(){}.getType());
try {
new CityDaoImpl().deleteCity(list);
return 200;
} catch (Exception e) {
e.printStackTrace();
return 403;
}
}
}
9、Dao 接口类:
/**
* @author hh
* @Date 2018/9/14
*/
public interface CityDao {
/**
* 城市信息列表
* @return
*/
List<City> listCity(); /**
* 批量删除
* @param listCity
*/
void deleteCity(List<City> listCity);
}
10、Dao实现类:
/**
* @author hh
* @Date 2018/9/14
*/
public class CityDaoImpl implements CityDao {
@Override
public List<City> listCity() {
List<City> list=null;
try(SqlSession sqlSession = MybatisUtil.getSqlSession()){
CityDao cityDao=sqlSession.getMapper(CityDao.class);
list=cityDao.listCity();
}
return list;
} @Override
public void deleteCity(List<City> listCity) {
try(SqlSession sqlSession = MybatisUtil.getSqlSession()){
CityDao cityDao=sqlSession.getMapper(CityDao.class);
cityDao.deleteCity(listCity);
}
}
}
我的项目案例(包括了上一篇博客的分页查询):点我下载
项目结构:

java使用插件pagehelper在mybatis中实现分页查询的更多相关文章
- mybatis中的高级查询
Mybatis作为一个ORM框架,肯定是支持sql高级查询的. 下面的一个案例来为大家详细讲解mybatis中的高级查询. 案例说明: 此案例的业务关系是用户,订单,订单详情与商品之间的关系. 以订单 ...
- 如何用Mybatis逆向工程实现分页查询(更改生成的Example)
如何用Mybatis逆向工程实现分页查询 一个很简单的方法,如果要mysql实现分页查询的话,执行下述语句即可 select * from table limit (offset)5,(limit) ...
- ssh框架中的分页查询
ssh中的分页查询是比较常用的,接下来我用代码来介绍如何实现一个分页查询 首先建立一个Model用来储存查询分页的信息 package com.haiziwang.qrlogin.utils; imp ...
- mongo中的分页查询
/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @p ...
- PageHelper在Mybatis中的使用
环境:Spring 4.2.1 Mybatis 3.2.8 pagehelper 5.1.2 Mybatis官方教程:https://github.com/pagehelper/Mybatis-Pag ...
- Mybatis中的in查询和foreach标签
Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...
- SSM-MyBatis-13:Mybatis中多条件查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 实体类 public class Book { private Integer bookID; private ...
- 【mybatis】mybatis中放置IN查询拼接sql过长,IN查询进行分批次查询的处理
需要使用的切割list集合的工具类,链接:https://www.cnblogs.com/sxdcgaq8080/p/9376947.html 处理逻辑,原本的一个LIst,进行切割,循环进行myba ...
- mybatis中Oracle分页语句的写法
最近一段时间使用oracle数据库查询分页, 用的是springboot. Oracle数据库中没有像mysql中limit的写法, 只能换其他方式写. 考虑到oracle中的ROWNUM变量, 使用 ...
随机推荐
- ⑧javaWeb之在例子中学习(过滤器Filter)
前言 本系列 Servlet & JSP 学习系列[传送门]逐渐到了中期了,希望大家喜欢我写的,总结的点点滴滴- 今天我们来讲讲过滤器 你们的支持是我写博客的动力哦. 最近买了两本书,觉得大二 ...
- 解决AssetBundle包加载预制体时,Shader显示异常的问题
现象: 预制体上的粒子效果显示为紫色方块. 原因:shader在打成AB包后与指定平台产生相关性,Editor中无法正常读取. 解决办法: 遍历所有加载的对象,重新赋值Shader 代码: //修正s ...
- 数据库 数据去重并取id最大的数据sql
SELECT * FROM(SELECT MAX(id) AS id FROM icbc_erp_kj_icbc_result WHERE STATUS ...
- Linux两台主机之间建立信任(ssh免密码)
背景: 有时候我们在两个主机之间复制文件的时候,提示输入密码,很不方便,那如何免密码复制呢?,就是使用通过linux公钥和秘钥,建立双机信任关系. 在整理之前,我先说下ssh免密码的要点 : 你想免密 ...
- linux读书笔记1
(1)进入控制台命令快捷键:Ctrl+alt+F1~F7
- #19 re&jieba模块
前言 在Python中,需要对字符串进行大量的操作,有时需要从一个字符串中提取到特定的信息,用切片肯定是不行的,所有这一节记录两个强大的文本处理模块,一个是正则表达式re模块,另一个是中文处理模块ji ...
- tomcat通过一个端口号实现多域名访问
最近在一个项目中遇到这样的一个场景,在一台服务器一个tomcat一个端口配置多个域名.没想到解决方法这么简单,通过虚拟目录来实现. 修改tomcat安装路径下/conf下的server.xml vim ...
- VRF在区块链中的应用
最近区块链领域流行了一种"怪病",许多区块链项目或者设计方案都加入了一个叫做VRFs的算法.那么, (1) 什么是VRFs? (2) VRFs在区块链中解决了什么问题? 本文旨在介 ...
- NLP入门(一)词袋模型及句子相似度
本文作为笔者NLP入门系列文章第一篇,以后我们就要步入NLP时代. 本文将会介绍NLP中常见的词袋模型(Bag of Words)以及如何利用词袋模型来计算句子间的相似度(余弦相似度,cosi ...
- Java-this
当方法中的参数和类中变量重名时,使用 this.变量 调用成员变量. public class test1 { String name; int age; public void te(String ...