Mybatis中的<association>用法

这篇文章我们将来学习一些 association用法

表结构

  1.  
    DROP TABLE IF EXISTS `student`; CREATE TABLE `student` (
  2.  
     `id` int(11) NOT NULL auto_increment,
  3.  
     `name` varchar(10) NOT NULL,
  4.  
     `gender` char(1) NOT NULL,
  5.  
     `major` varchar(20) NOT NULL,
  6.  
     `grade` char(4) NOT NULL,
  7.  
     `supervisor_id` int(11) NOT NULL,
  8.  
     PRIMARY KEY  (`id`)
  9.  
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', 'zhangsan', '男', '计算机科学与技术', '2011', '1'); -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` (
  10.  
     `id` int(11) NOT NULL auto_increment,
  11.  
     `name` varchar(10) NOT NULL,
  12.  
     `gender` char(1) NOT NULL,
  13.  
     `research_area` varchar(20) NOT NULL,
  14.  
     PRIMARY KEY  (`id`)
  15.  
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', 'laowu', '男', '软件工程');

对应的java bean

  1.  
    public class Student {
  2.  
    private int id;  
  3.  
    private String name;     // 姓名
  4.  
    private String gender;   // 性别
  5.  
    private String major;    // 专业
  6.  
    private String grade;    // 年级
  7.  
    private Teacher supervisor; //指导教师  
  8.  
     
  9.  
    public int getId() {
  10.  
     return id;
  11.  
    }
  12.  
    public void setId(int id) {
  13.  
     this.id = id;
  14.  
    }
  15.  
    public String getName() {
  16.  
     return name;
  17.  
    }
  18.  
    public void setName(String name) {
  19.  
     this.name = name;
  20.  
    }
  21.  
    public String getGender() {
  22.  
     return gender;
  23.  
    }
  24.  
    public void setGender(String gender) {
  25.  
     this.gender = gender;
  26.  
    }
  27.  
    public String getMajor() {
  28.  
     return major;
  29.  
    }
  30.  
    public void setMajor(String major) {
  31.  
     this.major = major;
  32.  
    }
  33.  
    public String getGrade() {
  34.  
     return grade;
  35.  
    }
  36.  
    public void setGrade(String grade) {
  37.  
     this.grade = grade;
  38.  
    }
  39.  
    public Teacher getSupervisor() {
  40.  
     return supervisor;
  41.  
    }
  42.  
    public void setSupervisor(Teacher supervisor) {
  43.  
     this.supervisor = supervisor;
  44.  
    }
  45.  
    }
  46.  
    public class Teacher {
  47.  
    private int id;    
  48.  
    private String name;               // 教师姓名
  49.  
    private String gender;        // 教师性别
  50.  
    private String researchArea;    // 研究领域
  51.  
    private List<Student> supStudents; // 指导学生
  52.  
     
  53.  
    public int getId() {
  54.  
     return id;
  55.  
    }
  56.  
    public void setId(int id) {
  57.  
     this.id = id;
  58.  
    }
  59.  
    public String getName() {
  60.  
     return name;
  61.  
    }
  62.  
    public void setName(String name) {
  63.  
     this.name = name;
  64.  
    }
  65.  
    public String getGender() {
  66.  
     return gender;
  67.  
    }
  68.  
    public void setGender(String gender) {
  69.  
     this.gender = gender;
  70.  
    }
  71.  
    public String getResearchArea() {
  72.  
     return researchArea;
  73.  
    }
  74.  
    public void setResearchArea(String researchArea) {
  75.  
     this.researchArea = researchArea;
  76.  
    }
  77.  
    public List<Student> getSupStudents() {
  78.  
     return supStudents;
  79.  
    }
  80.  
    public void setSupStudents(List<Student> supStudents) {
  81.  
     this.supStudents = supStudents;
  82.  
    }
  83.  
    }

mapper

  1.  
    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  2.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="net.zaodk.mybatis.mapper.StudentOperationMapper">
  3.  
    <!-- 定义java Bean的属性与数据库的列之间的映射 -->
  4.  
    <resultMap type="Student" id="studentResultMap">
  5.  
     <id column="id" property="id" />
  6.  
     <result column="name" property="name" />
  7.  
     <result column="gender" property="gender" />
  8.  
     <result column="major" property="major" />
  9.  
     <result column="grade" property="grade"/>
  10.  
     <!-- association处理has-a关系 -->
  11.  
     <!--注意:这种方式javaType必须指定,表示supervisor的类型是Teacher,否则会报错 -->
  12.  
     <association property="supervisor" javaType="Teacher">
  13.  
      <!-- 教师自身的属性与数据库字段的映射 -->
  14.  
      <id property="id" column="t_id"/>
  15.  
      <result property="name" column="t_name"/>
  16.  
      <result property="gender" column="t_gender"/>
  17.  
      <result property="researchArea" column="research_area"/>
  18.  
     </association>
  19.  
    </resultMap>
  20.  
     
  21.  
    <!-- SQL语句中以"#{}"的形式引用参数 -->
  22.  
    <select id="getById" parameterType="int" resultMap="studentResultMap">
  23.  
     SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
  24.  
     t.gender t_gender,t.research_area  
  25.  
     FROM student st, teacher t
  26.  
     WHERE st.supervisor_id = t.id
  27.  
     AND st.id=#{id}
  28.  
    </select> </mapper>

测试

  1.  
    public void testAssociation(){
  2.  
     SqlSession session = sqlSessionFactory.openSession();
  3.  
     StudentOperationMapper studentOperationMapper = session.getMapper(StudentOperationMapper.class);
  4.  
     Student student = studentOperationMapper.getById(1);
  5.  
     System.out.println(student.getName()+"的指导老师是:"+student.getSupervisor().getName());
  6.  
    }

myBatis association的两种形式

嵌套的resultMap

这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后association元素再引用这个resultMap元素。

  1.  
    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  2.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="net.zaodk.mybatis.mapper.StudentOperationMapper">
  3.  
    <!-- 定义java Bean的属性与数据库的列之间的映射 -->
  4.  
    <resultMap type="Teacher" id="teacherResultMap">
  5.  
     <id property="id" column="t_id"/>
  6.  
      <result property="name" column="t_name"/>
  7.  
      <result property="gender" column="t_gender"/>
  8.  
      <result property="researchArea" column="research_area"/>
  9.  
    </resultMap>
  10.  
     
  11.  
    <resultMap type="Student" id="studentResultMap">
  12.  
     <id column="id" property="id" />
  13.  
     <result column="name" property="name" />
  14.  
     <result column="gender" property="gender" />
  15.  
     <result column="major" property="major" />
  16.  
     <result column="grade" property="grade"/>
  17.  
     <!-- 引用teacherResultMap -->
  18.  
     <association property="supervisor" resultMap="teacherResultMap"/>
  19.  
    </resultMap>
  20.  
     
  21.  
    <!-- SQL语句中以"#{}"的形式引用参数 -->
  22.  
    <select id="getById" parameterType="int" resultMap="studentResultMap">
  23.  
     SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
  24.  
     t.gender t_gender,t.research_area  
  25.  
     FROM student st, teacher t
  26.  
     WHERE st.supervisor_id = t.id
  27.  
     AND st.id=#{id}
  28.  
    </select> </mapper>

嵌套的select语句

这种方式是使用一条单独的select语句来加载关联的实体(本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,尽量不要用嵌套的select语句)

  1.  
    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  2.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="net.zaodk.mybatis.mapper.StudentOperationMapper">
  3.  
    <!-- 定义java Bean的属性与数据库的列之间的映射 -->
  4.  
    <resultMap type="Teacher" id="supervisorResultMap">
  5.  
     <id property="id" column="t_id"/>
  6.  
      <result property="name" column="t_name"/>
  7.  
      <result property="gender" column="t_gender"/>
  8.  
      <result property="researchArea" column="research_area"/>
  9.  
    </resultMap>
  10.  
     
  11.  
    <resultMap type="Student" id="studentResultMap">
  12.  
     <id column="id" property="id" />
  13.  
     <result column="name" property="name" />
  14.  
     <result column="gender" property="gender" />
  15.  
     <result column="major" property="major" />
  16.  
     <result column="grade" property="grade"/>
  17.  
     <!-- 引用teacherResultMap -->
  18.  
     <association property="supervisor" column="supervisor_id" select="selectSupervisor"/>
  19.  
    </resultMap>
  20.  
     
  21.  
    <!-- SQL语句中以"#{}"的形式引用参数 -->
  22.  
    <select id="getById" parameterType="int" resultMap="studentResultMap">
  23.  
     select id,name,gender,major,grade,supervisor_id from student where id =#{id}
  24.  
    </select>
  25.  
     
  26.  
    <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap">
  27.  
     select id,name,gender,research_area
  28.  
     from teacher where id = #{id}
  29.  
    </select> </mapper
posted @ 2022-05-19 22:19  飞蚊  阅读(344)  评论(0编辑  收藏  举报