大家好,又见面了,我是你们的朋友全栈君。
Mybatis分页查询(通过SQL分页实现)
前言
实现有哪几种方式:
- 网页分页 (一次查询所有数据,加载到网页,那么适合数量小的操作)
- 服务器端分页 java程序中查询所有数据,网页需要哪一页,就给哪一页数据,会撑爆java服务器,建议查询缓存优化
- 数据库分页 请求一页数据,查询数据库即可
本文采用Oracle中的rownum实现分页,数据表使用Oracle中Scott的EMP表
一、搭建环境
目录结构
- 导入jar包 mybatis-3.2.7.jar ojdbc6.jar
- 数据库配置文件(db.properties)
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@10.25.164.149:1521:orcl
db.username=scott
db.password=123456
- mybatis-config.xml
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 这是映射文件,一个表一个,我们的sql就写在这里 -->
<mapper resource="config/EmpMapper.xml"/>
</mappers>
</configuration>
- MybatisUtils.java
package net.neuedu.mybatis3.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static
{
String resource = "config/mybatis-config.xml";
InputStream inputStream=null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}finally
{
}
sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSession getSqlSession()
{
SqlSession sqlSession=null;
sqlSession=sqlSessionFactory.openSession();
return sqlSession;
}
}
二、创建实体类
Emp.java
代码语言:javascript复制package net.neuedu.mybatis3.domain;
import java.math.BigDecimal;
import java.util.Date;
public class Emp {
private Integer empno;
private String ename;
private String job;
private Short mgr;
private Date hiredate;
private BigDecimal sal;
private BigDecimal comm;
// private Short deptno;
public Integer getEmpno() {
return empno;
}
@Override
public String toString() {
return "Emp [empno=" empno ", ename=" ename ", job=" job
", mgr=" mgr ", hiredate=" hiredate ", sal="
sal ", comm=" comm "]";
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename == null ? null : ename.trim();
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job == null ? null : job.trim();
}
public Short getMgr() {
return mgr;
}
public void setMgr(Short mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public BigDecimal getSal() {
return sal;
}
public void setSal(BigDecimal sal) {
this.sal = sal;
}
public BigDecimal getComm() {
return comm;
}
public void setComm(BigDecimal comm) {
this.comm = comm;
}
// public Short getDeptno() {
// return deptno;
// }
//
// public void setDeptno(Short deptno) {
// this.deptno = deptno;
// }
}
三、EmpMapper接口
代码语言:javascript复制public interface EmpMapper {
/** * 查询员工表中一共有多少条数据 * @return */
public Integer selectSize();
/** * 分页查询 * @param pageUtil * @return */
public List<Emp> selectEmpByPage(PageUtil pageUtil);}
四、分页工具类
PageUtil.java
代码语言:javascript复制package net.neuedu.mybatis3.domain;
/** * 具体功能:告诉他每页显示几条数据,和一共有多少数据,还有显示第几页数据,它帮我们算出需要分多少页,每页的起始索引 * @author hp * */
public class PageUtil {
public final static int PAGECOUNT=3;//每页显示几条数据 3
private int pageNumber;//显示第几页数据 1
private int size;// 一共有多少条数据 3
private int startIndex;//对应页面开始的索引 include
private int endIndex;//对应页面结束的索引 不包含
private int pageNums;//一共有多少页
@Override
public String toString() {
return "PageUtil [pageCount=" PAGECOUNT ", pageNumber="
pageNumber ", size=" size ", startIndex=" startIndex
", endIndex=" endIndex ", pageNums=" pageNums "]";
}
/** * 构造方法:需要三个参数,每页显示几条数据,一共有多少条数据,显示第几页数据 * @param pageCount * @param size * @param pageNumber */
public PageUtil(int pageCount,int size,int pageNumber)
{
//根据参数给对应属性赋值
this.pageNumber=pageNumber;
this.size=size;
//帮我们算出需要分多少页,每页的起始索引
this.pageNums=size%pageCount==0?size/pageCount:size/pageCount 1;
this.startIndex=this.PAGECOUNT*(pageNumber-1) 1;//0
if(pageNumber==pageNums)//如果请求页码刚好是最后一页,那么结束索引要好好计算一下
{
this.endIndex=size;
}else {
this.endIndex=this.PAGECOUNT*pageNumber;
}
//处理pageNumber=0的情况
if(pageNumber==0)
{
this.startIndex=0;
this.endIndex=0;
}
}
public static void main(String[] args) {
PageUtil pageUtil=new PageUtil(10, 3,1);//13页, 10-20
System.out.println(pageUtil);
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getEndIndex() {
return endIndex;
}
public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}
public int getPageNums() {
return pageNums;
}
public void setPageNums(int pageNums) {
this.pageNums = pageNums;
}
}
五、SQL实现分页
EmpMapper.xml
代码语言:javascript复制<?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="net.neuedu.mybatis3.mapper.EmpMapper">
<!--查询员工表中一共有多少条数据 public Integer selectSize(); -->
<select id="selectSize" resultType="Integer">select count(1) from emp</select>
<!-- /** * 分页查询 * @param pageUtil * @return */ -->
<select id="selectEmpByPage" parameterType="PageUtil" resultType="Emp">
select ee.* from
(select e.*,rownum rn from emp e
where rownum<![CDATA[<=]]>#{endIndex}) ee
where ee.rn>=#{startIndex}
</select>
</mapper>
测试
代码语言:javascript复制package test;
import java.util.List;
import net.neuedu.mybatis3.domain.Emp;
import net.neuedu.mybatis3.domain.PageUtil;
import net.neuedu.mybatis3.mapper.EmpMapper;
import net.neuedu.mybatis3.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
public class TestPageSQL {
public static void main(String[] args) {
SqlSession sqlSession=MybatisUtils.getSqlSession();
EmpMapper mapper=sqlSession.getMapper(net.neuedu.mybatis3.mapper.EmpMapper.class);
//查询员工表中一共有多少条数据
Integer size=mapper.selectSize();
//实例化分页工具类,它会帮我们算出开始索引和结束索引
PageUtil pageUtil=new PageUtil(3, size, 5);
//分页查询
List<Emp> list=mapper.selectEmpByPage(pageUtil);
System.out.println(list.toString());
sqlSession.close();
}
}
测试结果
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140939.html原文链接:https://javaforall.cn