继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

MyBatis 级联

mySoul__
关注TA
已关注
手记 103
粉丝 68
获赞 279

数据库关系模型中有一对一,一对多,关系,
首先建立如下表 继续用最经典的学生管理系统

建表

模型图如图所示
Diagram-1
学生表为中心
学生证表和其为一对一关系
学生表和课程表为一对多

一对一关系

因为上图中,学生和学生关系为一对一关系.
由于是数据库的一对一关系,那么对应的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 这个很简单,跳过

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP