mybatis系列五:使用pagehelper5外掛進行分頁
阿新 • • 發佈:2018-12-23
PageHelper是目前最強大最好用的分頁外掛。
使用PageHelper外掛一定要注意jar包之間的依賴關係,否則就死活出不來結果呀!!
比方說作者這裡使用的是pagehelper-5.0.2.jar jsqlparser-0.9.5.jar mybatis-3.2.8。那麼我為啥知道版本之間的依賴關係是這樣的呢,請看下圖:
廢話不多說了,下面來看具體的例項。
要使用pagehelper,可以配置在mybatis的核心配置檔案中,也可以配置在spring中。這裡採取mybatis的配置方式,spring的配置方式以後再探討。
案例1. 單張表的分頁操作<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.obtk.entitys.StudentEntity" alias="StudentEntity"/> </typeAliases> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- property屬性可以不用配置,新版本能自動識別底層資料庫 --> <property name="helperDialect" value="mysql"/> </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/schooldb" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/obtk/entitys/UserMapper.xml" /> <mapper resource="com/obtk/entitys/StudentMapper.xml" /> </mappers> </configuration>
sql配置:
<select id="selectByPage" resultType="StudentEntity">
select * from student
order by age desc
</select>
程式碼:
網上有人說pagehelper不支援表關聯的分頁,其實轉換一下思路就可以啦!而且方法不止一種,so easy 啦!package com.obtk.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.obtk.entitys.StudentEntity; import com.obtk.utils.MybatisUtil; public class TestPageQuery { public static void main(String[] args) { SqlSession session=null; try { //4.得到session session=MybatisUtil.getSession(); //分頁引數,第一個引數是當前頁碼(pageNo),第二個引數是每頁顯示多少條(pageSize) PageHelper.startPage(2, 3); //5.執行語句 List<StudentEntity> stuList=session.selectList("stu.selectByPage"); for(StudentEntity stu : stuList){ System.out.println(stu.getStuName()+"\t"+stu.getGender()+"\t"+stu.getAge()); } //分頁實體 PageInfo<StudentEntity> thePage=new PageInfo<StudentEntity>(stuList); System.out.println("下一頁:"+thePage.getNextPage()+",上一頁:"+thePage.getPrePage() +",總條數:"+thePage.getTotal()+",總頁碼:"+thePage.getPages()); } catch (Exception e) { e.printStackTrace(); }finally{ MybatisUtil.closeSession(); } } }
案例2 用業務實體類處理表的關聯及分頁
sql程式碼:
根據查詢結果建一個關聯實體JoinEntity.java<select id="selectByJoinPage" resultType="com.obtk.entitys.JoinEntity"> SELECT s.stuId,s.stuName,s.gender,s.age,d.departName FROM student s INNER JOIN department d ON s.deptIdd=d.deptId ORDER BY s.age DESC </select>
package com.obtk.entitys;
import java.io.Serializable;
public class JoinEntity implements Serializable{
private static final long serialVersionUID = -935724073017787380L;
private int stuId;
private String stuName;
private String gender;
private int age;
private String departName;
public JoinEntity() {
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
}
測試類:
package com.obtk.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.entitys.JoinEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class TestPageJoinQuery {
public static void main(String[] args) {
SqlSession session=null;
try {
//4.得到session
session=MybatisUtil.getSession();
//分頁引數,第一個引數是當前頁碼(pageNo),第二個引數是每頁顯示多少條(pageSize)
PageHelper.startPage(2, 3);
//5.執行語句
List<JoinEntity> stuList=session.selectList("stu.selectByJoinPage");
for(JoinEntity stu : stuList){
System.out.println(stu.getStuName()+"\t"+stu.getGender()+"\t"+stu.getAge()+"\t"+stu.getDepartName());
}
//分頁實體
PageInfo<JoinEntity> thePage=new PageInfo<JoinEntity>(stuList);
System.out.println("下一頁:"+thePage.getNextPage()+",上一頁:"+thePage.getPrePage()
+",總條數:"+thePage.getTotal()+",總頁碼:"+thePage.getPages());
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
案例3 用hashmap處理表的關聯及分頁sql程式碼
<select id="selectByMapPage" resultType="hashmap">
SELECT s.stuId,s.stuName,s.gender,s.age,d.departName
FROM student s INNER JOIN department d
ON s.deptIdd=d.deptId
ORDER BY s.age DESC
</select>
java程式碼:
package com.obtk.test;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class TestPageMapQuery {
public static void main(String[] args) {
SqlSession session=null;
try {
//4.得到session
session=MybatisUtil.getSession();
//分頁引數,第一個引數是當前頁碼(pageNo),第二個引數是每頁顯示多少條(pageSize)
PageHelper.startPage(2, 3);
//5.執行語句
List<Map> stuList=session.selectList("stu.selectByMapPage");
for(Map oneRow : stuList){
System.out.println(oneRow.get("stuName")+"\t"+oneRow.get("gender")+"\t"
+oneRow.get("age")+"\t"+oneRow.get("departName"));
}
//分頁實體
PageInfo<Map> thePage=new PageInfo<Map>(stuList);
System.out.println("下一頁:"+thePage.getNextPage()+",上一頁:"+thePage.getPrePage()
+",總條數:"+thePage.getTotal()+",總頁碼:"+thePage.getPages());
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
怎麼樣,是不是so easy!如果幫助到了您,請點個贊吧!