一:创建数据库脚本
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));
}
}