首页 > 代码库 > mybatis学习笔记三(关联关系)

mybatis学习笔记三(关联关系)

  学习mybatis的关联关系,主要注解在代码上,这里不做解释。配置文件一样的就不贴了

1.关联关系表创建(学生对应老师 多对一)

  学生老师表

技术分享

技术分享

技术分享

 

 2.表对应的实体类

package com.home.entity;

/**
 * 此类是:学生
 * @author hpc
 * @2017年1月14日下午7:06:33
 */
public class Student {
    private Integer student_id;//学生id
    private String student_name;//学生名字
    private Integer student_number;//学生编号
    private Teacher student_teacher;//是哪个教师的学生
    public Integer getStudent_id() {
        return student_id;
    }
    public void setStudent_id(Integer student_id) {
        this.student_id = student_id;
    }
    public String getStudent_name() {
        return student_name;
    }
    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }
    public Integer getStudent_number() {
        return student_number;
    }
    public void setStudent_number(Integer student_number) {
        this.student_number = student_number;
    }
    public Teacher getStudent_teacher() {
        return student_teacher;
    }
    public void setStudent_teacher(Teacher student_teacher) {
        this.student_teacher = student_teacher;
    }
    @Override
    public String toString() {
        return "Student [student_id=" + student_id + ", student_name=" + student_name + ", student_number="
                + student_number + ", student_teacher=" + student_teacher + "]";
    }
}
package com.home.entity;

import java.util.HashSet;
import java.util.Set;

/**
 * 此类是:老师
 * @author hpc
 * @2017年1月14日下午7:06:47
 */
public class Teacher {
    private Integer teacher_id;// 老师id
    private String teacher_name;// 老师名字
    private Integer teacher_number;// 老师编号
    private Set<Student> teacher_student = new HashSet<Student>();// 老师教过哪些学生
    
    public Integer getTeacher_id() {
        return teacher_id;
    }

    public void setTeacher_id(Integer teacher_id) {
        this.teacher_id = teacher_id;
    }

    public String getTeacher_name() {
        return teacher_name;
    }

    public void setTeacher_name(String teacher_name) {
        this.teacher_name = teacher_name;
    }

    public Integer getTeacher_number() {
        return teacher_number;
    }

    public void setTeacher_number(Integer teacher_number) {
        this.teacher_number = teacher_number;
    }

    public Set<Student> getTeacher_student() {
        return teacher_student;
    }

    public void setTeacher_student(Set<Student> teacher_student) {
        this.teacher_student = teacher_student;
    }

    @Override
    public String toString() {
        return "Teacher [teacher_id=" + teacher_id + ", teacher_name=" + teacher_name + ", teacher_number="
                + teacher_number + ", teacher_student=" + teacher_student + "]";
    }
    
}

3.核心主要是配置文件

<?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.home.entity.studentMapper"><!-- 命名空间,一定要是唯一的,只有这样才能定位到这个文件来 -->
    <!-- 多对一查询(多个学生对应一个老师) -->
    <!-- select的属性
        id:这个查找标签的唯一标识,通过id找到这条sql语句
        parameterType:传进来的参数类型
        resultMap:这条语句执行完,以什么形式返回结果集
     -->
    <select id="loadStudent" parameterType="int" resultMap="studentMap">
        select *
        from students as s join teachers as t
        on t.teacher_id=s.student_teacher
        where s.student_id=#{id}
    </select>
    <!-- 学生对象映射关系 -->
    <!--resultMap的属性
        type:以这个类型进行封装返回
        id:找到该resultMap标签的唯一标识
        resultMap的内部的标签介绍
        <id>专门用于映射主键的
        <result>普通属性映射
        <collection>集合属性映射
        <association>关系映射(1-1,1-n,n-1,n-n)
        <constructor>专门用于映射构造函数的标签
        <discriminator>鉴别器非常容易理 解,因为它的表现很像 Java 语言中的 switch 语句。
    -->
    <resultMap type="student" id="studentMap" >
        <!-- 主键映射 -->
        <id property="student_id" column="student_id" javaType="int"/>
        <!-- 普通属性映射 -->
        <!--  
        property:映射在这个属性上
        column:以这个列上的为参照
        javaType:以什么类型映射
        -->
        <result property="student_name" column="student_name" javaType="String"/>
        <result property="student_number" column="student_number" javaType="int"/>
        <!-- 关联关系映射 -->
        <association property="student_teacher" column="teacher_id" resultMap="teacherMap"/>
    </resultMap>
    <!-- 学生对象映射关系 -->
    <resultMap type="teacher" id="teacherMap">
        <id property="teacher_id" column="teacher_id" javaType="int"/>
        <result property="teacher_name" column="teacher_name" javaType="String"/>
        <result property="teacher_number" column="teacher_number" javaType="int"/>
    </resultMap>
</mapper>
<?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.home.entity.teacherMapper">
    <!-- teacher sql -->
    <select id="loadTeacher" parameterType="int" resultMap="teacherMapper">
        select *
        from teachers as t join students as s
        on s.student_teacher=t.teacher_id
        where teacher_id=#{teacher_id}
    </select>
    <resultMap type="teacher" id="teacherMapper">
        <id property="teacher_id" column="teacher_id" />
        <result property="teacher_name" column="teacher_name" />
        <result property="teacher_number" column="teacher_number" />
        <collection property="teacher_student" ofType="student">
            <id property="student_id" column="student_id" />
            <result property="student_name" column="student_name"
                javaType="String" />
            <result property="student_number" column="student_number"
                javaType="int" />
        </collection>
    </resultMap>
</mapper>

 4. 测试

package com.home.mybatis;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.home.entity.Student;
import com.home.entity.Teacher;

public class TestApp {
    public static void main(String[] args) throws IOException {
        // 我将获取session的代码封装成了一个工具类,直接用工具类来获取session
        SqlSession session = SessionUtils.getSession("mybatis.xml");
        String loadTeacher="com.home.entity.teacherMapper.loadTeacher";
        String loadStudent="com.home.entity.studentMapper.loadStudent";
        Teacher teacher = session.selectOne(loadTeacher,1);
        Student student = session.selectOne(loadStudent,1);
        System.out.println(student);
        System.out.println(teacher);
        // 将数据属性到数据中去
        session.commit();
        // 关闭session
        session.close();
    } 
}

5.结果

Student [student_id=4, student_name=hpc, student_number=1, student_teacher=Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[]]]
Teacher [teacher_id=1, teacher_name=王老师, teacher_number=1, teacher_student=[Student [student_id=5, student_name=zll, student_number=2, student_teacher=null], Student [student_id=4, student_name=hpc, student_number=1, student_teacher=null]]]

 

mybatis学习笔记三(关联关系)