MyBatis 一对一、一对多的

时间:2021-02-21 11:54:59

一、介绍:假设有两张表:学生表、教师表。其关系是一个学生对一个老师,一个老师对多个学生。


二、类 model


Student.java

package com.baidu.model;

import java.io.Serializable;


public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8757446338746075582L;
/**学生ID*/
private int id;//IDNUMBERY学生的ID
/**学生名字*/
private String name;//NAMEVARHCAR2(20)Y学生的名字
/**学生性别*/
private String sex;//SEX VARCHAR(20)Y学生的性别
/**学生老师的ID*/
private int teacherId; //TEACHERIDNUMBERY学生的老师的ID
/**一个学生对一个老师,把老师放这里*/
private Teacher teacher;
    //getters and setters
}


Teacher.java

package com.baidu.model;
import java.io.Serializable;
import java.util.List;

public class Teacher implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1527016488888036967L;
/**教师ID*/
private Integer teacherId;
/**教师名字*/
private String teacherName;
/**教师课程*/
private String subject;
/**获得学生集合*/
private List<Student> list;
    //getters and setters
}

三、配置文件

<?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">
<!--
Student.java 和table STUDENT
private Integer id; //学生ID(主键) ID NUMBER
private String name; //学生姓名 NAME VARCHAR2(20)
private String sex; //学生性别(男or女) SEX VARCHAR2(20) private
Integer teacherId; //教师ID(外键) TEACHERID NUMBER
-->

<!--
Teacher.java 和table TEACHER
private Integer teacherId; //教师主键id TEACHERID NUMBER
private String teacherName; //教师姓名 TEACHER_NAME VARCHAR
private String subject; //教学科目 SUBJECT VARCHAR
-->

<mapper namespace="com.baidu.StudentDAO">

<!-- 根据学生查教师信息 -->
<select id="selectTeacherByStudent" resultMap="StudentTeacherMap"
parameterType="com.baidu.Student">
SELECT T.TEACHERID,
T.TEACHER_NAME,
T.SUBJECT,
S.ID,
S.NAME,
S.SEX
FROM TEACHER T,STUDENT S
WHERE T.TEACHERID=S.TEACHERID
AND S.TEACHERID IN
(SELECT TEACHERID FROM STUDENT WHERE S.ID=#{id,jdbcType=INTEGER})
</select>
<!-- 根据教师查找学生信息 -->
<select id="selectStudentByTeacher" resultMap="teacherStudentMap"
parameterType="com.baidu.model.Teacher">
SELECT S.ID,
S.NAME,
S.SEX,
S.TEACHERID,
T.TEACHER_NAME,
T.SUBJECT
FROM STUDENT S, TEACHER T
WHERE (S.TEACHERID = T.TEACHERID)
and S.TEACHERID IN
(SELECT T.TEACHERID FROM TEACHER WHERE T.TEACHER_NAME =
#{teacherName,jdbcType=VARCHAR})
</select>


<!-- resultMap -->
<!-- 这个studentMap:学生返回(学生数据库的字段对实体) -->
<resultMap type="com.baidu.model.Student"
id="studentResultMap">
<id column="ID" jdbcType="INTEGER" property="id" />
<result column="NAME" jdbcType="VARCHAR" property="name" />
<result column="SEX" jdbcType="VARCHAR" property="sex" />
<result column="TEACHERID" jdbcType="INTEGER" property="teacherId" />
</resultMap>

<!-- teacherMap 教师返回 (教师数据库字段对实体)-->
<resultMap type="com.baidu.model.Teacher"
id="teacherMap">
<id column="TEACHERID" jdbcType="INTEGER" property="teacherId" />
<result column="TEACHER_NAME" jdbcType="VARCHAR" property="teacherName" />
<result column="SUBJECT" jdbcType="VARCHAR" property="subject" />
</resultMap>

<!-- StudentTeacherMap:学生查找教师返回 (返回的是学生类里面成员变量:老师实体)-->
<resultMap type="com.baidu.model.Student"
id="StudentTeacherMap">
<id column="ID" jdbcType="INTEGER" property="id" />
<result column="NAME" jdbcType="VARCHAR" property="name" />
<result column="SEX" jdbcType="VARCHAR" property="sex" />
<result column="TEACHERID" jdbcType="INTEGER" property="teacherId" />
<!-- 一对一 -->
<association property="teacher" javaType="com.baidu.model.Teacher"
resultMap="teacherMap" />
</resultMap>

<!--teacherStudentMap: 教师查找学生返回 (返回的是教师类里面成员变量:学生实体数组)-->
<resultMap type="com.baidu.model.Teacher"
id="teacherStudentMap">
<id column="TEACHERID" jdbcType="INTEGER" property="teacherId" />
<result column="TEACHER_NAME" jdbcType="VARCHAR" property="teacherName" />
<result column="SUBJECT" jdbcType="VARCHAR" property="subject" />
<!--一对多 -->
<collection property="list" javaType="java.util.List"
resultMap="defaultResultMap" />
</resultMap>
</mapper>

PS:本人初学者,如有错误可以提醒下。仅供参考,大神勿喷。