mybatis系列五:使用pagehelper5插件进行分页

PageHelper是目前最强大最好用的分页插件。

使用PageHelper插件一定要注意jar包之间的依赖关系,否则就死活出不来结果呀!!

比方说作者这里使用的是pagehelper-5.0.2.jar    jsqlparser-0.9.5.jar    mybatis-3.2.8。那么我为啥知道版本之间的依赖关系是这样的呢,请看下图:


废话不多说了,下面来看具体的实例。

要使用pagehelper,可以配置在mybatis的核心配置文件中,也可以配置在spring中。这里采取mybatis的配置方式,spring的配置方式以后再探讨。

<?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>
案例1.   单张表的分页操作

sql配置:

<select id="selectByPage"  resultType="StudentEntity">
		select * from student 
		order by age desc
	</select>
代码:

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();
		}
	}
}


网上有人说pagehelper不支持表关联的分页,其实转换一下思路就可以啦!而且方法不止一种,so easy 啦!

案例2   用业务实体类处理表的关联及分页

sql代码:

<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>
根据查询结果建一个关联实体JoinEntity.java

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!
如果帮助到了您,请点个赞吧!











  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值