1.创建javaWeb项目MyBatis_Lazy并在WebRoot下的WEB-INF下的lib下添加如下jar文件
cglib-nodep-2.1_3.jar
log4j-1.2.17.jar
mybatis-3.2.3.jar
ojdbc14.jar
2.在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
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
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 <!-- 1.jdbc链接数据库的主配置文件 --> 5 <properties resource="jdbc.properties"/> 6 7 <!-- 2.全局参数配置 --> 8 <settings> 9 <!--打开延迟加载的开关 --> 10 <setting name="lazyLoadingEnabled" value="true"/> 11 <!--将积极加载改为消极加载及按需加载 --> 12 <setting name="aggressiveLazyLoading" value="false"/> 13 </settings> 14 15 <!-- 别名配置 --> 16 <typeAliases> 17 <typeAlias type="com.entity.Dept" alias="Dept"/> 18 <typeAlias type="com.entity.Emp" alias="Emp"/> 19 </typeAliases> 20 21 <!-- 3.数据源的配置 --> 22 <environments default="development"> 23 <environment id="development"> 24 <transactionManager type="jdbc"/> 25 <dataSource type="POOLED"> 26 <property name="driver" value="${driver}"/> 27 <property name="url" value="${url}"/> 28 <property name="username" value="${username}"/> 29 <property name="password" value="${password}"/> 30 </dataSource> 31 </environment> 32 </environments> 33 <!-- 4.注册映射文件 --> 34 <mappers> 35 <mapper resource="com/mapper/ScottMapper.xml"/> 36 </mappers> 37 38 </configuration>
5.在src下com.entity包下创建Dept.java
1 package com.entity; 2 3 public class Dept { 4 5 private int deptno; 6 7 private String dname; 8 9 public Dept() { 10 } 11 public Dept(int deptno, String dname) { 12 this.deptno = deptno; 13 this.dname = dname; 14 } 15 public int getDeptno() { 16 return deptno; 17 } 18 public void setDeptno(int deptno) { 19 this.deptno = deptno; 20 } 21 public String getDname() { 22 return dname; 23 } 24 public void setDname(String dname) { 25 this.dname = dname; 26 } 27 @Override 28 public String toString() { 29 return "Dept [deptno=" + deptno + ", dname=" + dname + "]"; 30 } 31 32 33 }
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 Dept dept; //外键列:部门对象 8 9 10 public Emp() { 11 } 12 public Emp(int empno, String ename, double sal, Dept dept) { 13 this.empno = empno; 14 this.ename = ename; 15 this.sal = sal; 16 this.dept = dept; 17 } 18 public int getEmpno() { 19 return empno; 20 } 21 public void setEmpno(int empno) { 22 this.empno = empno; 23 } 24 public String getEname() { 25 return ename; 26 } 27 public void setEname(String ename) { 28 this.ename = ename; 29 } 30 public double getSal() { 31 return sal; 32 } 33 public void setSal(double sal) { 34 this.sal = sal; 35 } 36 public Dept getDept() { 37 return dept; 38 } 39 public void setDept(Dept dept) { 40 this.dept = dept; 41 } 42 43 /* @Override 44 public String toString() { 45 return "Emp [ empno=" + empno + ", ename=" + ename 46 + ", sal=" + sal + "]"; 47 }*/ 48 49 50 51 }
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 public static SqlSession getSeqlsession(boolean isCommit){ 23 return sqlSessionFactory.openSession(isCommit); 24 } 25 26 }
8.在src下com.mapper包下创建ScottMapper.java接口
1 package com.mapper; 2 3 import java.util.List; 4 5 import com.entity.Dept; 6 import com.entity.Emp; 7 8 /** 9 * 映射接口 10 * @author pc 11 * 12 */ 13 public interface ScottMapper { 14 /** 15 * 1.查询员工表是延迟加载部门信息 16 * @return 17 */ 18 List<Emp> getEmpLazyLoading(); 19 20 /** 21 * 2.根据部门编号查询某个部门信息 22 * @param deptno 23 * @return 24 */ 25 Dept findByDeptno(int deptno); 26 27 28 }
9.在src下com.mapper包下创建ScottMapper.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.ScottMapper"> 4 <!-- 1.查询从表数据Emp --> 5 <select id="getEmpLazyLoading" resultMap="EmpLazyLoading"> 6 select * from emp 7 </select> 8 9 <!-- 2.配置id=EmpLazyLoading的resultMap --> 10 <resultMap id="EmpLazyLoading" type="Emp"> 11 <id property="empno" column="empno"/> 12 <result property="ename" column="ename"/> 13 <result property="sal" column="sal"/> 14 15 <!-- 外键字段:多对一映射 --> 16 <association property="dept" javaType="Dept" column="deptno" select="findByDeptno"/> 17 </resultMap> 18 19 <!-- 3.根据部门编号查询部门信息 --> 20 <select id="findByDeptno" parameterType="int" resultType="Dept"> 21 select * from dept where deptno=#{deptno} 22 </select> 23 24 </mapper>
10.在src下com.mapper.impl包下创建ScottMapperImpl.java
1 package com.mapper.impl; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.entity.Dept; 8 import com.entity.Emp; 9 import com.mapper.ScottMapper; 10 import com.util.MyBatisUtil; 11 12 public class ScottMapperImpl implements ScottMapper { 13 /** 14 * 1.查询所有员工信息 15 */ 16 public List<Emp> getEmpLazyLoading() { 17 SqlSession sqlSession=MyBatisUtil.getSeqlsession(false); 18 ScottMapper mapper=sqlSession.getMapper(ScottMapper.class); 19 List<Emp> emp=mapper.getEmpLazyLoading(); 20 sqlSession.close(); 21 return emp; 22 } 23 /** 24 * 2.根据部门编号查询部门信息 25 */ 26 public Dept findByDeptno(int deptno) { 27 SqlSession sqlSession=MyBatisUtil.getSeqlsession(false); 28 ScottMapper mapper=sqlSession.getMapper(ScottMapper.class); 29 List<Emp> emp=mapper.getEmpLazyLoading(); 30 Dept dept=mapper.findByDeptno(deptno); 31 sqlSession.close(); 32 return dept; 33 } 34 35 36 }
11.在src下com.test包下创建Test.java
1 package com.test; 2 3 import java.util.List; 4 5 import com.entity.Emp; 6 import com.mapper.ScottMapper; 7 import com.mapper.impl.ScottMapperImpl; 8 9 public class Test { 10 public static void main(String[] args) { 11 ScottMapper mapper=new ScottMapperImpl(); 12 List<Emp> list=mapper.getEmpLazyLoading(); 13 for (Emp emp : list) { 14 System.out.println(emp.getDept().getDname()); 15 } 16 //System.out.println(mapper.findByDeptno(10));; 17 } 18 19 }