首页 > 代码库 > MyBatis----延迟加载demo

MyBatis----延迟加载demo

一:创建数据库脚本
drop table project_info; drop table status; create table status( id number(10) primary key, name varchar2(20) not null ); create table project_info( id number(10) primary key, project_name varchar2(100) not null, start_date date not null, end_date date not null, status_id number(10) references status(id) ); drop sequence seq_status; drop sequence seq_projectinfo; create sequence seq_status; create sequence seq_projectinfo; insert into status(id,name)values(seq_status.nextval,‘已申报‘); insert into status(id,name)values(seq_status.nextval,‘审核中‘); insert into status(id,name)values(seq_status.nextval,‘已审核‘); insert into project_info(id,project_name,start_date,end_date,status_id)values(seq_projectinfo.nextval,‘北京社会科学院2017年申报‘,to_date(‘2017=04-02‘,‘yyyy-MM-dd‘),to_date(‘2017=04-03‘,‘yyyy-MM-dd‘),1); insert into project_info(id,project_name,start_date,end_date,status_id)values(seq_projectinfo.nextval,‘福建社会科学院2017年申报‘,to_date(‘2017=04-04‘,‘yyyy-MM-dd‘),to_date(‘2017=04-05‘,‘yyyy-MM-dd‘),1); insert into project_info(id,project_name,start_date,end_date,status_id)values(seq_projectinfo.nextval,‘上海社会科学院2017年申报‘,to_date(‘2017=04-06‘,‘yyyy-MM-dd‘),to_date(‘2017=04-07‘,‘yyyy-MM-dd‘),1);

  二:项目目录结构

技术分享

三:在src下创建jdbc.properties

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=system
password=orcl

四:在src下创建log4j.properties

log4j.rootLogger=DEBUG, Console  
  
#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%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   

五:在src下创建mybatis-config.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>
  <properties resource="jdbc.properties"/>
  <settings>
  <!--  开启延时加载开关 -->
    <setting name="lazyLoadingEnabled" value="http://www.mamicode.com/true"/>
    <!-- 设置延时加载 -->
    <setting name="aggressiveLazyLoading" value="http://www.mamicode.com/false"/>
  </settings>
  <typeAliases>
    <typeAlias type="com.pojo.ProjectInfo" alias="ProjectInfo"/>
    <typeAlias type="com.pojo.Status" alias="Status"/>
  </typeAliases>
  <environments default="developement">
     <environment id="developement">
       <transactionManager type="JDBC"/>
       <dataSource type="POOLED">
         <property name="driver" value="http://www.mamicode.com/${driver}"/>
         <property name="url" value="http://www.mamicode.com/${url}"/>
         <property name="username" value="http://www.mamicode.com/${username}"/>
         <property name="password" value="http://www.mamicode.com/${password}"/>
       </dataSource>
     </environment>
  </environments>
  <mappers>
     <mapper resource="com/mapper/ProjectInfoMapper.xml"/>
  </mappers>
</configuration>

  

MyBatis----延迟加载demo