MyBatis 级联

72次阅读

共计 4629 个字符,预计需要花费 12 分钟才能阅读完成。

数据库关系模型中有一对一, 一对多, 关系, 首先建立如下表 继续用最经典的学生管理系统
建表
模型图如图所示学生表为中心学生证表和其为一对一关系学生表和课程表为一对多
一对一关系
因为上图中, 学生和学生关系为一对一关系. 由于是数据库的一对一关系, 那么对应的 bean 中, 也应该是一对一关系举栗子的表为学生表和学生证表其中学生证表的中的学生编号的外键为学生表的主键那么在学生表的 POJO 对象中, 应该有学生证表的 POJO 的映射. 即
package com.ming.MyBatis.POJO;

/**
* @author ming
*/
public class Student {
private int uid;
private String studentName;
private String gender;
private String studentIdNumber;
private String remarks;
private StudentCard studentCard;

public void setStudentCard(StudentCard studentCard) {
this.studentCard = studentCard;
}

public StudentCard getStudentCard() {
return studentCard;
}

public void setUid(int uid) {
this.uid = uid;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public void setGender(String gender) {
this.gender = gender;
}

public void setStudentIdNumber(String studentIdNumber) {
this.studentIdNumber = studentIdNumber;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

public int getUid() {
return uid;
}

public String getStudentName() {
return studentName;
}

public String getGender() {
return gender;
}

public String getStudentIdNumber() {
return studentIdNumber;
}

public String getRemarks() {
return remarks;
}
}

对于 card 的 POJO 如下所示
package com.ming.MyBatis.POJO;

import java.util.Date;

/**
* @author ming
*/
public class StudentCard {
private int uid;
private String studentNumber;
private String birthplace;
private Date dateOfIssue;
private Date endDate;
private String remarks;

public void setUid(int uid) {
this.uid = uid;
}

public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}

public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}

public void setDateOfIssue(Date dateOfIssue) {
this.dateOfIssue = dateOfIssue;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

public int getUid() {
return uid;
}

public String getStudentNumber() {
return studentNumber;
}

public String getBirthplace() {
return birthplace;
}

public Date getDateOfIssue() {
return dateOfIssue;
}

public Date getEndDate() {
return endDate;
}

public String getRemarks() {
return remarks;
}
}

先增加 StudentCard 的配置
<resultMap id=”studentSelfCardMap” type=”com.ming.MyBatis.POJO.StudentCard”>
<id column=”uid” property=”uid”/>
<result column=”student_number” property=”studentNumber”/>
<result column=”birthplace” property=”birthplace” />
<result column=”date_of_issue” property=”dateOfIssue” jdbcType=”DATE” javaType=”java.util.Date”/>
<result column=”end_date” property=”endDate” jdbcType=”DATE” javaType=”java.util.Date”/>
<result column=”remarks” property=”remarks” />
</resultMap>

<select id=”findStudentSelfCardByStudentId” parameterType=”int” resultMap=”studentSelfCardMap”>
SELECT student_card.uid, student_card.student_number, student_card.remarks,
student_card.end_date, student_card.date_of_issue, student_card.birthplace
FROM student_card WHERE student_card.uid = #{studentId};
</select>
再增加 Student 的映射
<resultMap id=”studentMap” type=”com.ming.MyBatis.POJO.Student”>
<id column=”uid” property=”uid”/>
<result column=”student_name” property=”studentName”/>
<result column=”gender” property=”gender”/>
<result column=”student_id_number” property=”studentIdNumber”/>
<result column=”remarks” property=”remarks”/>
<!– 将会调用接口代表的 SQL 进行执行查询 –>
<association property=”studentCard” column=”uid” select=”com.ming.MyBatis.RoleMapper.findStudentSelfCardByStudentId”/>
</resultMap>

<select id=”getStudent” parameterType=”int” resultMap=”studentMap”>
SELECT student.uid, student.gender, student.remarks, student.student_id_number,
student.student_name
FROM student
WHERE student.uid = 1;
</select>
可以看到当查询到 studentCard 的时候, 将会调用 com.ming.MyBatis.RoleMapper.findStudentSelfCardByStudentId sql 语句的执行
对于数据展示层来说
<%@ page import=”org.apache.ibatis.session.SqlSession” %>
<%@ page import=”com.ming.Util.SqlSessionFactoryUtil” %>
<%@ page import=”com.ming.MyBatis.RoleMapper” %>
<%@ page import=”java.util.List” %>
<%@ page import=”java.util.Iterator” %>
<%@ page import=”com.ming.MyBatis.POJO.Student” %>
<html>
<body>
<h2>Hello World!</h2>

<%
long startTime = System.currentTimeMillis(); // 获取开始时间
SqlSession sqlSession = null;
List<Student> students = null;
for(int i = 0; i < 10; i++) {
try {
sqlSession = SqlSessionFactoryUtil.openSqlSesion();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
students = roleMapper.getStudent(1);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
long endTime = System.currentTimeMillis(); // 获取结束时间

%>

<%
Iterator iterator = students.iterator();
while(iterator.hasNext()){
%>
<%=((Student)iterator.next()).getStudentCard().getEndDate()%>

<%
}
%>
</body>
</html>

一对多
这个和上方同理, 跳过
鉴别器
根据学生信息去关联男性或者女性的健康指标
两个类只需要继承同一个类, 使用 case 语句即可. 跳过
性能问题
在使用级联的时候, 会造成多条 sql 语句的执行, 此时, 使用延迟加载
一开始不使用 sql 语句, 只有当使用了 sql 才去发送数据
在 setting 这里设置, 可以降低 sql 语句的执行问题
<settings>
<setting name=”lazyLoadingEnabled” value=”true”/>
</settings>
一般情况下是层级加载, 取消层级加载, 使用 aggressiveLazyLoading 值设置为 false 即可
另一种
写原生 sql 这个很简单, 跳过

正文完
 0