6.MyBaits的分页和缓存查询

时间:2021-11-27 00:25:43

1. 创建javaweb项目MyBaits_Page_CaChe

2.在项目的WebRoot下的WEB-INF下的lib文件下加入jar文件

log4j-1.2.17.jar

mybatis-3.2.3.jar

ojdbc14.jar

3.在src下创建jdbc.properties属性文件

 driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=scott
password=tiger

jdbc.properties

4.在src下创建configuration.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>
<!-- 引用jdbc链接数据库属性文件 -->
<properties resource="jdbc.properties"/> <!-- 全局参数的配置 -->
<settings>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings> <!-- 配置别名 -->
<typeAliases>
<typeAlias type="com.entity.Emp" alias="Emp"/>
<typeAlias type="com.page.EmpPage" alias="EmpPage"/>
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource> </environment> </environments> <mappers>
<mapper resource="com/mapper/EmpMapper.xml"/>
</mappers> </configuration>

configuration.xml

5.在src下创建log4j.properties日志文件

 log4j.rootLogger=DEBUG,Console 

 #Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.Console.layout=org.apache.log4j.PatternLayout ###log4j to file###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=mybatis.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %p %F %M [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

6.在src下创建com.entity包下创建Emp.java

 package com.entity;

 public class Emp {
private int empno;
private String ename;
private double sal;
private int deptno; public Emp() {
}
public Emp(int empno, String ename, double sal, int deptno) {
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.deptno = deptno;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp [deptno=" + deptno + ", empno=" + empno + ", ename="
+ ename + ", sal=" + sal + "]";
} }

Emp.java

7.在src下创建com.util包下创建MyBatisUtil.java

 package com.util;

 import java.io.IOException;
import java.io.Reader; 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 MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory=null;
static{
try {
Reader reader=Resources.getResourceAsReader("configuration.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
} } public static SqlSession getSqlSession(boolean isCommit){
return sqlSessionFactory.openSession(isCommit);
} }

MyBatisUtil.java

8.在src下创建com.page包下创建EmpPage.java

 package com.page;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;

 import com.entity.Emp;
import com.mapper.EmpMapper;
import com.util.MyBatisUtil; public class EmpPage {
private Integer pageNo;
private Integer pageSize;
private Integer totalPage;
private Integer totalCount; public EmpPage() {
}
public EmpPage(Integer pageNo, Integer pageSize) {
this.pageNo = pageNo;
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
if(pageNo!=null){
this.pageNo = pageNo;
}else{
this.pageNo=1;
} }
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
} /**
* 查询总记录数
* @return 总记录数
*/
public int totalCount(){
SqlSession sqlSession=MyBatisUtil.getSqlSession(false);
EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);
Integer count=mapper.totalCount();
sqlSession.clearCache();
sqlSession.close(); //修改 return count;
} /**
* 计算总页数
* @param pageSize 页面大小
* @param totalCount 总的记录数
* @return 总页数
*/
public int totalPage(int pageSize,int totalCount){
int count=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
this.setTotalPage(count);
return count;
} /**
* 分页查询
* @param pageSize 页面大小
* @param pageNo 当前页
* @return 分页查询集合
*/
public List<Emp> findEmpPage(EmpPage page){
SqlSession sqlSession=MyBatisUtil.getSqlSession(false);
EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);
List<Emp> list=mapper.findEmpPage(page);
sqlSession.close();
return list;
} }

EmpPage.java

9.在src下创建com.mapper包下创建EmpMapper.java

 package com.mapper;

 import java.util.List;

 import com.entity.Emp;
import com.page.EmpPage; public interface EmpMapper {
/**
* 查询总条数
* @return
*/
Integer totalCount(); /**
* 分页查询
*/
List<Emp> findEmpPage(EmpPage page); }

EmpMapper.java

10.在src下创建com.test包下创建EmpMapper.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="com.mapper.EmpMapper">
<!-- 开启二级缓存 -->
<!--回收策略为先进先出-->
<!--自动刷新时间60s-->
<!--最多缓存512个引用对象-->
<!--只读-->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/> <!-- 查询总条数 -->
<select id="totalCount" resultType="Integer">
select count(*) from emp
</select> <!-- 分页查询 -->
<select id="findEmpPage" resultType="Emp" parameterType="EmpPage">
<![CDATA[
select e.empno,e.ename,e.sal,e.deptno from
(
select rownum r,empno,ename,sal,deptno from emp
where rownum<=(#{pageNo}*#{pageSize})
)e where
e.r>(#{pageNo}-1)*#{pageSize}
]]> <!--
select e.empno,e.ename,e.sal,e.deptno from
(
select rownum r,empno,ename,sal,deptno from emp
where rownum<=3
)e where
e.r>0
--> </select> </mapper>

EmpMapper.xml

11.在src下创建com.mapper包下创建Test.java

 package com.test;

 import java.util.List;

 import com.entity.Emp;
import com.page.EmpPage; public class Test { public static void main(String[] args) {
EmpPage page=new EmpPage(2, 3);
int count=page.totalCount();
count=page.totalCount();
System.out.println("------总条数:"+count);
System.out.println("-----------分页查询数据如下---------");
List<Emp> list=page.findEmpPage(page);
for (Emp emp : list) {
System.out.println(emp);
}
} }

Test.java