简单分析级联查询:
关系模型中:表与表只有主外键关联
领域模型中:实体类与实体类这间关联,只有一和多的关系。
一是指别一个实体类以对象属性存在当前实体类中。
多是指别一个实体类以集合对象属性存在当前实体类中
下面我以例子的形式给大家说明:
数据库脚本如下:
drop table student;
drop table classes;
drop table teacher;
create table student
(
stuId number(4) primary key,
stuName varchar2(20) not null,
stuSex varchar2(4),
stuBirthday date,
classId number(4)
);
create table classes
(
clsId number(4) primary key,
clsName varchar2(20) not null,
teacherId number(4)
);
create table teacher
(
teacherId number(4) primary key,
teacherName varchar2(20) not null,
workYear number(2),
professional varchar2(20)
);
insert into student values(1001, ‘张三‘, ‘男‘, to_date(‘1989-12-2‘, ‘yyyy-mm-dd‘), 3001);
insert into student values(1002, ‘王五‘, ‘女‘, to_date(‘1991-6-21‘, ‘yyyy-mm-dd‘), 3001);
insert into student values(1003, ‘赵六‘, ‘女‘, to_date(‘1990-1-15‘, ‘yyyy-mm-dd‘), 3001);
insert into student values(1004, ‘田七‘, ‘男‘, to_date(‘1992-11-25‘, ‘yyyy-mm-dd‘), 3002);
insert into classes values(3001, ‘计本1103‘, 5001);
insert into classes values(3002, ‘信本1102‘, 5002);
insert into teacher values(5001, ‘李四‘, 3, ‘计算机科学与技术‘);
insert into teacher values(5002, ‘王八‘, 5, ‘计算机信息‘);
commit;
mybatis.xml配制文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.yc.mybatis.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<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/yc/mybatis/entity/ClassesMapper.xml"/>
<mapper resource="com/yc/mybatis/entity/StudentMapper.xml"/>
<mapper resource="com/yc/mybatis/entity/TeacherMapper.xml"/>
</mappers>
</configuration>
mybatis工具类
package com.yc.mybatis.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil
{
private static SqlSessionFactory sqlSessionFactory;
static
{
try
{
InputStream in = Resources.getResourceAsStream("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
catch (IOException e)
{
throw new RuntimeException("读取mybatis.xml配制失败", e);
}
}
public static SqlSession getSession()
{
SqlSession session = null;
if (sqlSessionFactory != null)
{
session = sqlSessionFactory.openSession();
}
return session;
}
public static SqlSession getSession(boolean isAuto)
{
SqlSession session = null;
if (sqlSessionFactory != null)
{
session = sqlSessionFactory.openSession(isAuto);
}
return session;
}
public static void close(SqlSession session)
{
if (session != null)
{
session.close();
}
}
}
MyBatis 级联查询一对一实体类(Classes与Teacher):
Classes类
package com.yc.mybatis.entity;
public class Classes
{
private String clsId;
private String clsName;
private Teacher teacher;
public String getClsId()
{
return clsId;
}
public void setClsId(String clsId)
{
this.clsId = clsId;
}
public String getClsName()
{
return clsName;
}
public void setClsName(String clsName)
{
this.clsName = clsName;
}
public Teacher getTeacher()
{
return teacher;
}
public void setTeacher(Teacher teacher)
{
this.teacher = teacher;
}
@Override
public String toString()
{
return "Classes [clsId=" + clsId + ", clsName=" + clsName + ", teacher=" + teacher + "]";
}
}
Teacher类
return "Teacher [teacherId=" + teacherId + ", teacherName=" + teacherName + ", workYear=" + workYear + ", professional=" + professional + "]";