一:创建数据库脚本
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="true"/>
<!-- 设置延时加载 -->
<setting name="aggressiveLazyLoading" value="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="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/ProjectInfoMapper.xml"/>
</mappers>
</configuration>
六:在src的com.util包下创建MyBatisUtil.java

package com.util;
import java.io.IOException;
import java.io.InputStream;
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;
// 获取Sqlsessin的工具类
public class MyBatisUtil {
public static SqlSessionFactory sqlSessionFactory=null;
static{
try {
//解析xml
//InputStream is=MyBatisUtil.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
System.out.println("解析xml文件或创建sqlSessionFactory错误!");
e.printStackTrace();
}

}
//获取SqlSession对象
public static SqlSession getSqlSession(boolean isCommit){
return sqlSessionFactory.openSession(isCommit);

}
}

七:在src的com.pojo包下创建Status.java

package com.pojo;
//一方:申报类型表
//一对多:引入集合
public class Status {
private Integer id;
private String name;
public Status() {
super();
}
public Status(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Status [id=" + id + ", name=" + name + "]";
}
}

八:在src的com.pojo包下创建ProjectInfo.java

package com.pojo;

import java.util.Date;

//多方:项目表
//多对一:引入对象
public class ProjectInfo {
private Integer id;
private String project_name;
private Date start_date;
private Date end_date;
private Status status;//外建列引入对象
public ProjectInfo() {

}
public ProjectInfo(Integer id, String projectName, Date startDate,
Date endDate, Status status) {
super();
this.id = id;
project_name = projectName;
start_date = startDate;
end_date = endDate;
this.status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String projectName) {
project_name = projectName;
}
public Date getStart_date() {
return start_date;
}
public void setStart_date(Date startDate) {
start_date = startDate;
}
public Date getEnd_date() {
return end_date;
}
public void setEnd_date(Date endDate) {
end_date = endDate;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@Override
public String toString() {
return "ProjectInfo [end_date=" + end_date + ", id=" + id
+ ", project_name=" + project_name + ", start_date=" + start_date
+ ", status=" + status + "]";
}
}

九:在src的com.mapper包下创建ProjectInfoMapper.java

package com.mapper;

import java.util.List;

import com.pojo.ProjectInfo;
import com.pojo.Status;

//项目信息映射接口
public interface ProjectInfoMapper {
List<ProjectInfo> findProject();
Status findByStatusId(Integer id);
}

十:在src的com.mapper包下创建ProjectInfoMapper.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.ProjectInfoMapper">
<select id="findProject" resultMap="projectmap">
select * from project_info
</select>
<resultMap type="ProjectInfo" id="projectmap">
<id property="id" column="ID"/>
<result property="project_name" column="PROJECT_NAME"/>
<result property="start_date" column="START_DATE"/>
<result property="end_date" column="END_DATE"/>
<association property="status" column="STATUS_ID" javaType="Status" select="findByStatusId"/>
</resultMap>
<select id="findByStatusId" resultType="Status" parameterType="Integer">
select * from status where id=#{id}
</select>
</mapper>

十一:在src的com.mapper.impl包下创ProjectInfoMapperImpl.java

package com.mapper.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.mapper.ProjectInfoMapper;
import com.pojo.ProjectInfo;
import com.pojo.Status;
import com.util.MyBatisUtil;
//项目信息映射接口实现类
public class ProjectInfoMapperImpl implements ProjectInfoMapper{

public Status findByStatusId(Integer id) {
SqlSession sqlSession=MyBatisUtil.getSqlSession(false);
ProjectInfoMapper mapper=sqlSession.getMapper(ProjectInfoMapper.class);
Status status=mapper.findByStatusId(id);
sqlSession.close();
return status;
}

public List<ProjectInfo> findProject() {
SqlSession sqlSession=MyBatisUtil.getSqlSession(false);
ProjectInfoMapper mapper=sqlSession.getMapper(ProjectInfoMapper.class);
List<ProjectInfo> list=mapper.findProject();
sqlSession.close();
return list;
}

}

十二:在src的com.test包下创建Test.java

package com.test;

import java.util.List;

import com.mapper.ProjectInfoMapper;
import com.mapper.impl.ProjectInfoMapperImpl;
import com.pojo.ProjectInfo;

//测试类
public class Test {
public static void main(String[] args) {
ProjectInfoMapper mapper=new ProjectInfoMapperImpl();
System.out.println("延迟加载:");
List<ProjectInfo> list=mapper.findProject();
for(ProjectInfo projectInfo:list){
System.out.println(projectInfo);
System.out.println(projectInfo.getStatus());
}
System.out.println("根据id查询状态信息");
//System.out.println(mapper.findByStatusId(1));
}
}

MyBatis----延迟加载demo的更多相关文章

  1. mybatis源代码分析:mybatis延迟加载机制改进

    在上一篇博客<mybatis源代码分析:深入了解mybatis延迟加载机制>讲诉了mybatis延迟加载的具体机制及实现原理. 可以看出,如果查询结果对象中有一个属性是需要延迟加载的,那整 ...

  2. mybatis源代码分析:深入了解mybatis延迟加载机制

    下文从mybatis(3.2.7)延迟加载样例讲起,逐步深入其实现机制. 下面的例子是Student类关联一个Teacher对象,在访问Student对象时,不立即加载其关联的Teacher对象,而是 ...

  3. MyBatis使用DEMO及cache的使用心得

    下面是一个简单的MyBatis使用DEMO. 整体结构 整体代码大致如下: POM依赖 需要引用两个jar包,一个是mybatis,另一个是mysql-connector-java,如果是maven工 ...

  4. MyBatis的demo

    把以前写的关于mybatis的demo放在这边,以便查看. 目录结构: package com.test.mybatis.util; import java.io.IOException; impor ...

  5. 【MyBatis】MyBatis 延迟加载策略

    MyBatis 延迟加载策略 文章源码 什么是延迟加载 延迟加载,就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据,也被成为懒加载. 好处:先从单表查询,需要时再从关联表去关联查询,大大提 ...

  6. Mybatis【20】-- Mybatis延迟加载怎么处理?

    注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-16-lazyload,需要自取,需要配置mave ...

  7. MyBatis 延迟加载,一级缓存,二级缓存设置

    什么是延迟加载 resultMap中的association和collection标签具有延迟加载的功能. 延迟加载的意思是说,在关联查询时,利用延迟加载,先加载主信息.使用关联信息时再去加载关联信息 ...

  8. Mybatis入门DEMO

    下面将通过以下步骤说明如何使用MyBatis开发一个简单的DEMO: 步骤一:新建表STUDENTS 字段有: Stu_Id.Stu_Name.Stu_Age.Stu_Birthday CREATE ...

  9. mybatis——延迟加载

    ------------------------------------------------SqlMapConfig.xml------------------------------------ ...

  10. mybatis延迟加载

    配置完成后可能会报错Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath 是由于 ...

随机推荐

  1. mysql创建定时执行存储过程任务实现订单定时关闭

    CREATE PROCEDURE `tableName`.`procedureName`() LANGUAGE SQL NOT DETERMINISTIC CONTAINS SQL SQL SECUR ...

  2. HDFS-文件写入API

    package com.zhen.hdfs; import java.io.BufferedInputStream; import java.io.FileInputStream; import ja ...

  3. Java正则表达中Greedy Reluctant Possessive 的区别

    Java正则表达中Greedy Reluctant Possessive 的区别 分类: java2015-01-16 00:28 1280人阅读 评论(9) 收藏 举报 正则表达式Java   目录 ...

  4. Ceph简介

    最近偶尔接触到云计算,开始对云计算感兴趣,希望能够早日加入这个计算机领域的第三次革命中去. 估计这次革命要持续二十年,也就是这辈子一直干云计算都没问题. 先了解一下Ceph吧.本博文主要是根据文献[1 ...

  5. 安全的 ActiveMQ

    本章知识点 ActiveMQ 鉴权 ActiveMQ 授权 怎么创建一个自定义安全插件 使用基于证书的安全保证 简介 安全地访问消息代理以及它的 destinations 是公众关注的焦点.因此,Ac ...

  6. MySql简单分页存储过程

    BEGIN DECLARE startIndex int; select COUNT(*) INTO RecordCount from test; SET startIndex = (PageInde ...

  7. eclipse部署和启动guns

    eclipse部署guns: 1.import -> 搜索maven -> Existing Maven Projects -> 选择guns根目录 2.修改配置文件: spring ...

  8. 26 python 并发编程之多进程理论

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  9. JVM_总结_00_资源帖

    一.官方资料 Java Platform Standard Edition 8 Documentation The Java™ Tutorials Java 8 API 二.精选资料 发布<Ja ...

  10. Eclipse插件开发_学习_00_资源帖

    一.官方资料 1.eclipse api 2.GEF Developer's Guide 二. 精选资料 1.开发 Eclipse 插件 2.Eclipse, RCP, Plugin and OSGi ...