摘要: 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); } } }
我的項目案例(包括了上一篇博客的分頁查詢):點我下載
項目結構: