摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件

PageHelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有中文注释

请求URL:http://localhost:8080/listCity?page=1&limit=10

显示数据:

e88576d1c25acf7b8ebc4db1cca42f6a.png

1、PageHelper的maven依赖及插件配置

com.github.pagehelper

pagehelper

5.1.6

PageHelper除了本身的jar包外,它还依赖了一个叫jsqlparser的jar包,使用时,我们不需要单独指定jsqlparser的maven依赖,maven的间接依赖会帮我们引入。

2、配置拦截器插件

这个是配置在mybatis-config.xml文件中

文档中的示例

3、我的配置mybatis-config.xml:

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4、city-mapper.xml 数据库查询语句配置:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from city_test

delete from city_test where city_id in#{city.cityId}

View Code

5、后台分页查询 servlet:

/***@authorhh

* @Date 2018/9/15*/@WebServlet("/listCity")public class CityListServlet extendsHttpServlet {

@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

resp.setContentType("application/json;charset=utf-8");//取出前端请求参数

String page=req.getParameter("page");

String limit=req.getParameter("limit");//分页查询结果 page页数 limit显示行数

List listCity=newCityService().listCity(page,limit);//包装Page对象 listCity:page结果 , navigatePages: 页码数量

PageInfo list=new PageInfo<>(listCity,1);//自己写的一个响应视图类,因为前端用的是layui框架需要自己,所以自己定义ResponseView

ResponseView vo=newResponseView();//设值 取出总数据行

vo.setCount(list.getTotal());//设值 查询的结果

vo.setData(list.getList());//响应前端

resp.getWriter().print(newGson().toJson(vo));

}

}

6、响应视图类 ResponseView (因为前端用的是layui框架需要自己,所以自己定义ResponseView):

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packageedu.nf.vo;/***@authorhh

* @Date 2018/9/15*/

public classResponseView {private int code =0;private Long count=0L;privateObject data;publicInteger getCode() {returncode;

}public voidsetCode(Integer code) {this.code =code;

}publicLong getCount() {returncount;

}public voidsetCount(Long count) {this.count =count;

}publicObject getData() {returndata;

}public voidsetData(Object data) {this.data =data;

}

}

View Code

7、实体类 City:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packageedu.nf.entity;/***@authorhh

* @Date 2018/9/14*/

public classCity {privateString cityId;privateString cityEn;privateString cityCn;privateString countryCode;privateString countryEn;privateString countryCn;privateString provinceEn;privateString provinceCn;publicString getCityId() {returncityId;

}public voidsetCityId(String cityId) {this.cityId =cityId;

}publicString getCityEn() {returncityEn;

}public voidsetCityEn(String cityEn) {this.cityEn =cityEn;

}publicString getCityCn() {returncityCn;

}public voidsetCityCn(String cityCn) {this.cityCn =cityCn;

}publicString getCountryCode() {returncountryCode;

}public voidsetCountryCode(String countryCode) {this.countryCode =countryCode;

}publicString getCountryEn() {returncountryEn;

}public voidsetCountryEn(String countryEn) {this.countryEn =countryEn;

}publicString getCountryCn() {returncountryCn;

}public voidsetCountryCn(String countryCn) {this.countryCn =countryCn;

}publicString getProvinceEn() {returnprovinceEn;

}public voidsetProvinceEn(String provinceEn) {this.provinceEn =provinceEn;

}publicString getProvinceCn() {returnprovinceCn;

}public voidsetProvinceCn(String provinceCn) {this.provinceCn =provinceCn;

}

}

View Code

8、service 逻辑业务层(CityService):

/***@authorhh

* @Date 2018/9/15*/

public classCityService {/*** 分页查询 城市信息集合

*@return

*/

public ListlistCity(String offest,String pageSize){//类型转换

Integer pnum=Integer.valueOf(offest);

Integer psize=Integer.valueOf(pageSize);//调用PageHelper获取第1页,10条内容,默认查询总数count

PageHelper.startPage(pnum,psize);//调用CityDaoImpl 分页查询

return newCityDaoImpl().listCity();

}/*** 批量删除

*@paramcityData

*@return

*/

public intdeleteCity(String cityData){

List list=new Gson().fromJson(cityData,new TypeToken>(){}.getType());try{newCityDaoImpl().deleteCity(list);return 200;

}catch(Exception e) {

e.printStackTrace();return 403;

}

}

}

9、Dao 接口类:

/***@authorhh

* @Date 2018/9/14*/

public interfaceCityDao {/*** 城市信息列表

*@return

*/ListlistCity();/*** 批量删除

*@paramlistCity*/

void deleteCity(ListlistCity);

}

10、Dao实现类:

/***@authorhh

* @Date 2018/9/14*/

public class CityDaoImpl implementsCityDao {

@Overridepublic ListlistCity() {

List list=null;try(SqlSession sqlSession =MybatisUtil.getSqlSession()){

CityDao cityDao=sqlSession.getMapper(CityDao.class);

list=cityDao.listCity();

}returnlist;

}

@Overridepublic void deleteCity(ListlistCity) {try(SqlSession sqlSession =MybatisUtil.getSqlSession()){

CityDao cityDao=sqlSession.getMapper(CityDao.class);

cityDao.deleteCity(listCity);

}

}

}

我的项目案例(包括了上一篇博客的分页查询):点我下载

项目结构:

76cde3821948b329840e61c0543489b7.png

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐