One to many in MyBatis

One to many understanding:

  • A teacher has more than one student
  • If for teachers, it is a one to many phenomenon, that is, from a teacher to have a group of students (set)!

Database for reference Many to one.

Example:


The following example is built in the spring boot environment. There are two ways to do many to one queries.

Nesting by query is like a subquery in SQL
Nesting according to the results is like a join table query in SQL


For one thing, nesting results is like a join table query in SQL. It's easy to understand.

1. pom.xml dependency

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>


2. application.properties

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.cc.pojo
mybatis.config-location=classpath:mybatis-config.xml
 
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8


3. Log configuration (here, mybatis standard logs are used. STDOUT_LOGGING. )

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
</configuration>


4. Main code

1) The Student class and Teacher class under the pojo package.

package com.cc.pojo; 

package com.cc.pojo;

import lombok.Data;

public class Student {
    private int id;
    private String name;
    private int tid;
    //GET,SET,ToString, with and without parameters
}
package com.cc.pojo;

import java.util.List;

public class Teacher {
    private int id;
    private String name;
    //One teacher and many students
    private List<Student> students;
}

2) The interface under mapper.

package com.cc.mapper;
 

package com.cc.mapper;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper {

}
package com.cc.mapper;

import com.cc.pojo.Teacher;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TeacherMapper {
    //Obtain the designated Teacher and all students under the teacher
    public Teacher getTeacher(int id);
}

3) Write mapper.xml configuration file corresponding to Mapper interface

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

</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.cc.mapper.TeacherMapper">
        <!--
        Train of thought:
            1. Find out the student id, student name and teacher name from the student table and teacher table
            2. Do result set mapping for the queried operations
                1. For collection, use collection!
                    Both JavaType and ofType are used to specify the object type
                    JavaType is the type used to specify properties in pojo
                    ofType specifies the type mapped to pojo in the list collection property.
        -->
        <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=#{tid}
    </select>

        <resultMap id="TeacherStudent" type="Teacher">
            <result property="id" column="tid"/>
            <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>

4) test

package com.cc;

import com.cc.mapper.TeacherMapper;
import com.cc.pojo.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TeacherTest {

    @Autowired
    private TeacherMapper teacherMapper;

    @Test
    public void testTeacher() {
        Teacher teacher = teacherMapper.getTeacher(2);
        System.out.println(teacher);
    }
}

5) Operation results

Teacher{id=2, name='Big teacher', students=[Student(id=1, name=Xiao Ming, tid=2), Student(id=2, name=Xiaohong, tid=2), Student(id=3, name=Xiao Zhang, tid=2), Student(id=4, name=petty thief, tid=2), Student(id=5, name=Xiao Wang, tid=2)]}

 

Note: when mapping the resultMap, the attribute cannot be less, otherwise it cannot be output.

 

For many to one and one to many summary:

  1. association Association
  2. Collection collection
  3. So association is for one-to-one and many to one, while collection is for one to many relationships
  4. Both JavaType and ofType are used to specify the object type

    • JavaType is the type used to specify properties in pojo
    • ofType specifies the type mapped to pojo in the list collection property.

 


 

246 original articles published, 14 praised, 20000 visitors+
Private letter follow

Tags: Mybatis xml Spring Junit

Posted on Sat, 07 Mar 2020 22:00:26 -0800 by unidox