使用Oracle实现的MyBatis分页查询效果

时间:2021-12-01 22:16:53

1、mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"./mybatis-3-config.dtd">
<configuration>
<!-- properties配置文件中属性值,在整个配置文件中通过${}进行引用 -->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver" />
</properties>

<typeAliases>
<typeAlias type="com.wh.pojo.Emp" alias="Emp"/>
</typeAliases>

<!-- 数据源环境信息配置 -->
<environments default="development2">

<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- mysql数据源配置 -->
<property name="driver" value="${driver}" />
<property name="url" value="jdbc:mysql://localhost/ys" />
<property name="username" value="root" />
<property name="password" value="admin" />
</dataSource>
</environment>

<environment id="development2">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- oracle数据源配置 -->
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="com/wh/mapper/DeptMapper.xml" />
<mapper resource="com/wh/mapper/EmpMapper.xml" />
</mappers>

</configuration>

2、EmpMapper.java

package com.wh.mapper;

import java.util.List;

import com.wh.common.PagerOracle;
import com.wh.pojo.Emp;
import com.wh.vo.VoOracle;

public interface EmpMapper {

public void insertDept(Emp emp);

public List<Emp> selectAll();

public List<VoOracle> selectOracle(PagerOracle vo);

}

3、EmpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"./mybatis-3-mapper.dtd">

<!--namespace 绑定接口 -->
<mapper namespace="com.wh.mapper.EmpMapper">

<resultMap id="EmpResultMap" type="Emp">
<result property="pno" column="EMPNO" />
<result property="pname" column="ENAME" />
<result property="pjob" column="JOB" />
<result property="pmgr" column="MGR" />
<result property="phiredate" column="HIREDATE" />
<result property="psal" column="SAL" />
<result property="pcomm" column="COMM" />
<result property="pdeptno" column="DEPTNO" />
</resultMap>

<!--查询所有 id 接口中的方法名 resultType 返回结果类型 resultType="com.wh.pojo.Dept"-->
<select id="selectAll" resultMap="EmpResultMap">
select * from EMP
</select>

<!--oracle分页查询 -->
<select id="selectOracle" parameterType="com.wh.common.PagerOracle" resultType="com.wh.vo.VoOracle">
SELECT r.ename,r.dname,r.loc FROM (SELECT e.ename,d.dname,d.loc,ROWNUM s FROM dept d,emp e

<![CDATA[ WHERE d.deptno=e.deptno AND ROWNUM<#{end} ) r where r.s>#{start} ]]>

</select>


</mapper>

4、EmpDaoImpl.java  

package com.wh.mapperImpl;

import java.util.List;

import com.wh.common.PagerOracle;
import com.wh.mapper.EmpMapper;
import com.wh.pojo.Emp;
import com.wh.vo.VoOracle;


public class EmpDaoImpl extends BaseDao implements EmpMapper {

@Override
public List<Emp> selectAll() {
//获得连接
this.openSession();
//找到接口
EmpMapper mapper=(EmpMapper) ss.getMapper(EmpMapper.class);
return mapper.selectAll();
}

@Override
public void insertDept(Emp emp) {
// TODO Auto-generated method stub

}

@Override
public List<VoOracle> selectOracle(PagerOracle vo) {
//获得连接
this.openSession();
//找到接口
EmpMapper mapper=(EmpMapper) ss.getMapper(EmpMapper.class);
return mapper.selectOracle(vo);
}

}

5、PagerOracle.java

package com.wh.common;

public class PagerOracle {
private int recordCount;// 总记录数
private int pageSize = 5;// 每页个数 自定义
private int pageCount; // 总页数
private int pageNum;// 当前页数
private int start;// 起始记录数
private int end;

public PagerOracle(int recordCount, int pageNum) {
super();
this.recordCount = recordCount;
this.pageNum = pageNum;
// 总页数
if (recordCount % pageSize == 0) {
pageCount = recordCount / pageSize;
}
else {
pageCount = recordCount / pageSize + 1;
}
// 当前页码处理
if (pageNum < 1) {
pageNum = 1;
}
if (pageNum > pageCount) {
pageNum = pageCount;
}
// 起始位置 oracle从1开始
start = (pageNum-1) * pageSize+1;
end= start + pageSize - 1;
}

public int getRecordCount() {
return recordCount;
}

public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getPageCount() {
return pageCount;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public int getPageNum() {
return pageNum;
}

public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}

public int getStart() {
return start;
}

public void setStart(int start) {
this.start = start;
}

public int getEnd() {
return end;
}

public void setEnd(int end) {
this.end = end;
}

}

6、VoOracle.java

package com.wh.vo;

import com.wh.common.PagerOracle;

public class VoOracle {
private String ename;
private String dname;
private String Loc;
private PagerOracle pager;

public VoOracle() {

}

public VoOracle(PagerOracle pager) {
super();
this.pager = pager;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public String getDname() {
return dname;
}

public void setDname(String dname) {
this.dname = dname;
}

public String getLoc() {
return Loc;
}

public void setLoc(String loc) {
Loc = loc;
}

public PagerOracle getPager() {
return pager;
}

public void setPager(PagerOracle pager) {
this.pager = pager;
}

@Override
public String toString() {
return "VoOracle [ename=" + ename + ", dname=" + dname + ", Loc=" + Loc + ", pager=" + pager + "]";
}

}

TestEmp.java

package com.wh.junit;
/**
* mybatis编写顺序
* DeptMapper.java、DeptMapper.xml、DeptDaoImpl.java、TestMyBatis.java
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.wh.common.Pager;
import com.wh.common.PagerOracle;
import com.wh.mapperImpl.DeptDaoImpl;
import com.wh.mapperImpl.EmpDaoImpl;
import com.wh.pojo.Dept;
import com.wh.pojo.Emp;
import com.wh.vo.VO;
import com.wh.vo.VoOracle;

public class TestEmp {

//插入
@Test
public void testInsertDept() throws IOException{
//DeptDaoImpl dao=new DeptDaoImpl();
//dao.insertDept(new Dept(4,"技术","4楼"));
}

//查询所有
@Test
public void testSelectAll() throws IOException{
EmpDaoImpl dao=new EmpDaoImpl();
List<Emp> list = dao.selectAll();
for(Emp p:list){
System.out.println(p);
}
}

//Oracle分页查询
@Test
public void testSelectOracle() throws IOException{
EmpDaoImpl dao=new EmpDaoImpl();
//
List<VoOracle> list = dao.selectOracle(new PagerOracle(15,2));
for(VoOracle p:list){
System.out.println(p);
}
}

}