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属性文件
1 driver=oracle.jdbc.driver.OracleDriver 2 url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl 3 username=scott 4 password=tiger
jdbc.properties
4.在src下创建configuration.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <!-- 引用jdbc链接数据库属性文件 --> 5 <properties resource="jdbc.properties"/> 6 7 <!-- 全局参数的配置 --> 8 <settings> 9 <!-- 开启二级缓存 --> 10 <setting name="cacheEnabled" value="true"/> 11 </settings> 12 13 14 <!-- 配置别名 --> 15 <typeAliases> 16 <typeAlias type="com.entity.Emp" alias="Emp"/> 17 <typeAlias type="com.page.EmpPage" alias="EmpPage"/> 18 </typeAliases> 19 20 <environments default="development"> 21 <environment id="development"> 22 <transactionManager type="jdbc"/> 23 <dataSource type="POOLED"> 24 <property name="driver" value="${driver}"/> 25 <property name="url" value="${url}"/> 26 <property name="username" value="${username}"/> 27 <property name="password" value="${password}"/> 28 </dataSource> 29 30 31 32 </environment> 33 34 35 </environments> 36 37 <mappers> 38 <mapper resource="com/mapper/EmpMapper.xml"/> 39 </mappers> 40 41 </configuration>
configuration.xml
5.在src下创建log4j.properties日志文件
1 log4j.rootLogger=DEBUG, Console 2 3 #Console 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 7 8 log4j.logger.java.sql.ResultSet=INFO 9 log4j.logger.org.apache=INFO 10 log4j.logger.java.sql.Connection=DEBUG 11 log4j.logger.java.sql.Statement=DEBUG 12 log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.properties
6.在src下创建com.entity包下创建Emp.java
1 package com.entity; 2 3 public class Emp { 4 private int empno; 5 private String ename; 6 private double sal; 7 private int deptno; 8 9 public Emp() { 10 } 11 public Emp(int empno, String ename, double sal, int deptno) { 12 this.empno = empno; 13 this.ename = ename; 14 this.sal = sal; 15 this.deptno = deptno; 16 } 17 public int getEmpno() { 18 return empno; 19 } 20 public void setEmpno(int empno) { 21 this.empno = empno; 22 } 23 public String getEname() { 24 return ename; 25 } 26 public void setEname(String ename) { 27 this.ename = ename; 28 } 29 public double getSal() { 30 return sal; 31 } 32 public void setSal(double sal) { 33 this.sal = sal; 34 } 35 public int getDeptno() { 36 return deptno; 37 } 38 public void setDeptno(int deptno) { 39 this.deptno = deptno; 40 } 41 @Override 42 public String toString() { 43 return "Emp [deptno=" + deptno + ", empno=" + empno + ", ename=" 44 + ename + ", sal=" + sal + "]"; 45 } 46 47 48 }
Emp.java
7.在src下创建com.util包下创建MyBatisUtil.java
1 package com.util; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 public class MyBatisUtil { 12 private static SqlSessionFactory sqlSessionFactory=null; 13 static{ 14 try { 15 Reader reader=Resources.getResourceAsReader("configuration.xml"); 16 sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 21 } 22 23 public static SqlSession getSqlSession(boolean isCommit){ 24 return sqlSessionFactory.openSession(isCommit); 25 } 26 27 }
MyBatisUtil.java
8.在src下创建com.page包下创建EmpPage.java
1 package com.page; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.entity.Emp; 8 import com.mapper.EmpMapper; 9 import com.util.MyBatisUtil; 10 11 public class EmpPage { 12 private Integer pageNo; 13 private Integer pageSize; 14 private Integer totalPage; 15 private Integer totalCount; 16 17 18 public EmpPage() { 19 } 20 public EmpPage(Integer pageNo, Integer pageSize) { 21 this.pageNo = pageNo; 22 this.pageSize = pageSize; 23 } 24 public Integer getPageNo() { 25 return pageNo; 26 } 27 public void setPageNo(Integer pageNo) { 28 if(pageNo!=null){ 29 this.pageNo = pageNo; 30 }else{ 31 this.pageNo=1; 32 } 33 34 } 35 public Integer getPageSize() { 36 return pageSize; 37 } 38 public void setPageSize(Integer pageSize) { 39 this.pageSize = pageSize; 40 } 41 public Integer getTotalPage() { 42 return totalPage; 43 } 44 public void setTotalPage(Integer totalPage) { 45 this.totalPage = totalPage; 46 } 47 public Integer getTotalCount() { 48 return totalCount; 49 } 50 public void setTotalCount(Integer totalCount) { 51 this.totalCount = totalCount; 52 } 53 54 /** 55 * 查询总记录数 56 * @return 总记录数 57 */ 58 public int totalCount(){ 59 SqlSession sqlSession=MyBatisUtil.getSqlSession(false); 60 EmpMapper mapper=sqlSession.getMapper(EmpMapper.class); 61 Integer count=mapper.totalCount(); 62 sqlSession.clearCache(); 63 sqlSession.close(); 64 65 //修改 66 67 return count; 68 } 69 70 /** 71 * 计算总页数 72 * @param pageSize 页面大小 73 * @param totalCount 总的记录数 74 * @return 总页数 75 */ 76 public int totalPage(int pageSize,int totalCount){ 77 int count=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1; 78 this.setTotalPage(count); 79 return count; 80 } 81 82 /** 83 * 分页查询 84 * @param pageSize 页面大小 85 * @param pageNo 当前页 86 * @return 分页查询集合 87 */ 88 public List<Emp> findEmpPage(EmpPage page){ 89 SqlSession sqlSession=MyBatisUtil.getSqlSession(false); 90 EmpMapper mapper=sqlSession.getMapper(EmpMapper.class); 91 List<Emp> list=mapper.findEmpPage(page); 92 sqlSession.close(); 93 return list; 94 } 95 96 97 }
EmpPage.java
9.在src下创建com.mapper包下创建EmpMapper.java
1 package com.mapper; 2 3 import java.util.List; 4 5 import com.entity.Emp; 6 import com.page.EmpPage; 7 8 public interface EmpMapper { 9 /** 10 * 查询总条数 11 * @return 12 */ 13 Integer totalCount(); 14 15 /** 16 * 分页查询 17 */ 18 List<Emp> findEmpPage(EmpPage page); 19 20 }
EmpMapper.java
10.在src下创建com.test包下创建EmpMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.mapper.EmpMapper"> 4 <!-- 开启二级缓存 --> 5 <!--回收策略为先进先出--> 6 <!--自动刷新时间60s--> 7 <!--最多缓存512个引用对象--> 8 <!--只读--> 9 <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/> 10 11 <!-- 查询总条数 --> 12 <select id="totalCount" resultType="Integer"> 13 select count(*) from emp 14 </select> 15 16 <!-- 分页查询 --> 17 <select id="findEmpPage" resultType="Emp" parameterType="EmpPage"> 18 <![CDATA[ 19 select e.empno,e.ename,e.sal,e.deptno from 20 ( 21 select rownum r,empno,ename,sal,deptno from emp 22 where rownum<=(#{pageNo}*#{pageSize}) 23 )e where 24 e.r>(#{pageNo}-1)*#{pageSize} 25 ]]> 26 27 <!-- 28 select e.empno,e.ename,e.sal,e.deptno from 29 ( 30 select rownum r,empno,ename,sal,deptno from emp 31 where rownum<=3 32 )e where 33 e.r>0 34 --> 35 36 </select> 37 38 </mapper>
EmpMapper.xml
11.在src下创建com.mapper包下创建Test.java
1 package com.test; 2 3 import java.util.List; 4 5 import com.entity.Emp; 6 import com.page.EmpPage; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 EmpPage page=new EmpPage(2, 3); 12 int count=page.totalCount(); 13 count=page.totalCount(); 14 System.out.println("------总条数:"+count); 15 System.out.println("-----------分页查询数据如下---------"); 16 List<Emp> list=page.findEmpPage(page); 17 for (Emp emp : list) { 18 System.out.println(emp); 19 } 20 } 21 22 }
Test.java
时间: 2024-10-22 13:23:48