MyBatis Advanced Results Mapping | Many-to-One Query


〇, demand

Requirements: Query all student information and information about the corresponding teacher

1. Build the environment

Many-to-one is like multiple students corresponding to one teacher. Many-to-one query is based on multiple tables. First, set up the MySQLenvironment:

CREATE DATABASE MyBatis_DB;
USE MyBatis_DB;
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');

The results shown below in Table construction:
Insert picture description here
Open idea, a new mavenitem, and then pom.xmlinjected into the file mysql, MyBatis, Junit, LOG4Jdependent

Then create entity classes for two tables:

Student table entity class:

package com.wzq.pojo;

public class Student {
    
    

    private int id;
    private String name;
    private Teacher 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;
    }

    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 +
                '}';
    }
}

Teacher table entity class:

package com.wzq.pojo;

public class Teacher {
    
    

    private int id;
    private String name;

    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 + '\'' +
                '}';
    }
}

Then create StudentMapper, TeacherMapperinterface and their corresponding xmlfiles

2. Nested query according to the result

Nested query according to the result, first StudentMapperwrite the method in the interface:

List<Student> getStudent2();

Then StudentMapper.xmlwrite the code in:

    <select id="getStudent2" resultMap="Student_Teacher2">
        select
            s.id sid,
            s.name sname,
            t.name tname
        from
            student s,
            teacher t
        where
            s.tid = tid;
    </select>
    <resultMap id="Student_Teacher2" type="Student">
    	<!--
    	 	property:实体类中的属性 
    	 	column:表的列名
    	-->
        <id property="id" column="sid" />
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

Final test:

    @Test
    public void getStudent1Test(){
    
    
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent2();
        for (Student student : studentList) {
    
    
            System.out.println(student);
        }
        sqlSession.close();
    }

Output result:
Insert picture description here

Three, according to query nesting processing

First StudentMapperwrite the method in:

List<Student> getStudent1();

Then StudentMapper.xmlwrite sqland result mapping in:

    <select id="getStudent1" resultMap="Student_Teacher1">
        select * from student;
    </select>
    <resultMap id="Student_Teacher1" type="Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher" />
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{tid};
    </select>

Final test:

    @Test
    public void getStudent1Test(){
    
    
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent2();
        for (Student student : studentList) {
    
    
            System.out.println(student);
        }
        sqlSession.close();
    }

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/lesileqin/article/details/113095429