Mybatis之collection與association標籤

collection與association標籤的功能就是爲了解決查詢條件映射到一個類或一個集合上,適用於對於多對一,一對多的映射結果,如今咱們就探究其具體使用吧。java


 

環境搭建:sql

  • 數據庫搭建
CREATE TABLE teacher (
  id INT(10) NOT NULL,
  name VARCHAR(30) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO teacher(id, name) VALUES (1, '秦老師'); 

CREATE TABLE student (
  id INT(10) NOT NULL,
  name VARCHAR(30) DEFAULT NULL,
  tid INT(10) DEFAULT NULL,
  PRIMARY KEY (id),
  KEY fktid (tid),
  CONSTRAINT fktid FOREIGN KEY (tid) REFERENCES teacher (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8


INSERT INTO student (id, name, tid) VALUES ('1', '小明', '1'); 
INSERT INTO student (id, name, tid) VALUES ('2', '小紅', '1'); 
INSERT INTO student (id, name, tid) VALUES ('3', '小張', '1'); 
INSERT INTO student (id, name, tid) VALUES ('4', '小李', '1'); 
INSERT INTO student (id, name, tid) VALUES ('5', '小王', '1'); 
  • pojo
//GET,SET,ToString,有參,無參構造
public class Teacher {
    private int id;
    private String name;
}
//Get,Set,ToString, 有參,無參構造
public class Student {
    private int id;
    private String name;
    //多個學生能夠是同一個老師,即多對一
    private Teacher teacher;
}

 


 

多對一的理解:數據庫

  • 多個學生對應一個老師
  • 若是對於學生這邊,就是一個多對一的現象,即從學生這邊關聯一個老師!

接口編寫方法:session

//獲取全部學生及對應老師的信息
public List<Student> getStudents();

 

配置文件:mybatis

方式一:按查詢嵌套查詢app

<?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.ttt.mapper.StudentMapper">

    <select id="getStudents" resultMap="StudentTeacher">
      select * from student
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <!--association關聯屬性  property屬性名 javaType屬性類型 column在多的一方的表中的列名-->
        <association property="teacher"  column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <!--
    這裏傳遞過來的id,只有一個屬性的時候,下面能夠寫任何值
    association中column多參數配置:
        column="{key=value,key=value}"
        其實就是鍵值對的形式,key是傳給下個sql的取值名稱,value是片斷一中sql查詢的字段名。
    -->
    <select id="getTeacher" resultType="teacher">
        select * from teacher where id = #{id}
    </select>

</mapper>

 

方式二:按結果嵌套查詢ide

<select id="getStudents2" resultMap="StudentTeacher2" >
    select s.id sid, s.name sname , t.name tname
    from student s,teacher t
    where s.tid = t.id
</select>

<resultMap id="StudentTeacher2" type="Student">
    <id property="id" column="sid"/>
    <result property="name" column="sname"/>
    <!--關聯對象property 關聯對象在Student實體類中的屬性-->
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>

 

在覈心配置文件註冊。測試

測試:spa

 

 1 @Test
 2 public void testGetStudents(){
 3     SqlSession session = MybatisUtils.getSession();
 4     StudentMapper mapper = session.getMapper(StudentMapper.class);
 5 
 6     List<Student> students = mapper.getStudents();
 7 
 8     for (Student student : students){
 9         System.out.println(
10                 "學生名:"+ student.getName()
11                         +"\t老師:"+student.getTeacher().getName());
12     }
13 }
testGetStudents

 

 


 

 

一對多的理解:code

  • 一個老師擁有多個學生
  • 若是對於老師這邊,就是一個一對多的現象。

接口編寫方法:

//獲取指定老師,及老師下的全部學生
public Teacher getTeacher(int id);

 

mapper配置文件:

方式一:按查詢嵌套查詢

<select id="getTeacher2" resultMap="TeacherStudent2">
  select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
    <!--column是一對多的外鍵 , 寫的是一的主鍵的列名-->
    <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
    select * from student where tid = #{id}
</select>

 

方式二:按結果嵌套查詢

<mapper namespace="com.ttt.mapper.TeacherMapper">
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid, s.name sname , t.name tname, t.id tid
        from student s,teacher t
        where s.tid = t.id and t.id=#{id}
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <result  property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid" />
            <result property="name" column="sname" />
            <result property="tid" column="tid" />
        </collection>
    </resultMap>
</mapper>

 

在覈心配置文件註冊。

測試:

 

1 @Test
2 public void testGetTeacher2(){
3     SqlSession session = MybatisUtils.getSession();
4     TeacherMapper mapper = session.getMapper(TeacherMapper.class);
5     Teacher teacher = mapper.getTeacher2(1);
6     System.out.println(teacher.getName());
7     System.out.println(teacher.getStudents());
8 }
testGetTeacher

 

 

 


 

 

小結:

 

一、關聯-association  集合-collection

二、因此association是用於一對一和多對一,而collection是用於一對多的關係

三、JavaType和ofType都是用來指定對象類型的

  • JavaType是用來指定pojo中屬性的類型
  • ofType指定的是映射到list集合屬性中pojo的類型。
相關文章
相關標籤/搜索