1. 程式人生 > 其它 >mybatis學習日記-連表查詢

mybatis學習日記-連表查詢

按照結果集巢狀查詢

dao層:

 1 package com.fu.dao;
 2 
 3 import com.fu.pojo.Student;
 4 
 5 
 6 import java.util.List;
 7 
 8 public interface StudentMapper {
 9 
10     //查詢所有的學生資訊以及其老師的資訊
11     List<Student> getStudentList();
12 
13 
14 }
1 package com.fu.dao;
2 
3 public interface TeacherMapper {
4 }

pojo層:

package com.fu.pojo;

import org.apache.ibatis.type.Alias;

@Alias("student")
public class Student {
    private int id;
    private String name;
    private Teacher teacher;

    public Student(int id, String name, Teacher teacher) {
        this.id = id;
        this.name = name;
        
this.teacher = teacher; } public Student() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }
public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + '}'; } }
package com.fu.pojo;

import org.apache.ibatis.type.Alias;

@Alias("teacher")
public class Teacher {
    private int id;
    private String name;

    public Teacher(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Teacher() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

dao層對應的student的xml:

<?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.fu.dao.StudentMapper">


    <!--    方式一:按照結果巢狀處理-->
    <resultMap id="StudentTeacher" type="student">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>

        <!--       association:pojo中為物件,其中property對應的是實體類的欄位,此處為學生表中的teacher,
                   javaType對應的是實體類的中屬性的型別,比如在:private Teacher teacher;的型別就為Teacher

                   collection:pojo為集合,在一對多的時候使用
        -->
        <association property="teacher" javaType="teacher">
            <result column="tid" property="id"/>
            <result column="tname" property="name"/>
        </association>

    </resultMap>

    <select id="getStudentList" resultMap="StudentTeacher">
        SELECT student.`id` AS sid,student.`name` AS sname,student.`tid`AS stid,teacher.`id` AS tid,teacher.`name` AS tname
        FROM student,teacher
        WHERE student.`tid`=teacher.`id`
    </select>





</mapper>

測試類:

package com.fu.dao;

import com.fu.pojo.Student;
import com.fu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;


import java.util.List;

public class StudentMapperTest {

    @Test
    public void getStudentListTest(){
        //獲取sqlSession物件
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<Student> studentList = mapper.getStudentList();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }
}

測試結果: