MyBatis in many-to

Many-to-handle

Many-to-understand:

  • Corresponding to a plurality of student teacher
  • If a student here for, is a many-to-phenomenon that is associated with a teacher from a student here!

Database Design

1567059915539.png

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'); 

Example:

The following example is built in SpringBoot environment. There are two ways to carry out a many queries.

  • Like nested query processing in accordance with sub-query in SQL
  • Nested process in accordance with the results of SQL queries as inline table in

To cite just one kind, the nesting process in accordance with the result of contingency tables as SQL queries. It is better understood.

  1. pom.xml dependence

        <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. The configuration log (log standard used here mybatis .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 major code

1) Student under the category pojo package, Teacher class.

package com.cc.pojo;


public class Student {
    private int id;
    private String name;
    //多个学生可以是同一个老师,即多对一
    private Teacher teacher;

    //GET,SET,ToString,有参,无参构造
}
package com.cc.pojo;

public class Teacher {
    private int id;
    private String name;
    //GET,SET,ToString,有参,无参构造
}

2) at the interface mapper.

package com.cc.mapper;

import com.cc.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface StudentMapper {
    //获取所有学生及对应老师的信息
    public List<Student> getStudents();
}
@Mapper
public interface TeacherMapper {

}

Although there is no TeacherMapper method, but for subsequent write contingencies.

3) an interface corresponding to the write Mapper profile mapper.xml 

StudentMapper.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.cc.mapper.StudentMapper">
    <!--
    按查询结果嵌套处理
    思路:
        1. 直接查询出结果,进行结果集的映射
    -->
    <select id="getStudents" resultMap="StudentTeacher" >
        select s.id sid, s.name sname , t.name tname
        from student s,teacher t
        where s.tid = t.id
    </select>

    <resultMap id="StudentTeacher" 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>
</mapper>

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

</mapper>

4) Test

package com.cc;

import com.cc.mapper.StudentMapper;
import com.cc.pojo.Student;
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;

import java.util.List;

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

    @Autowired
    private StudentMapper studentMapper;

    @org.junit.Test
    public void testStudent() {
        List<Student> students = studentMapper.getStudents();

//        System.out.println(students.toString());
        for (Student student : students){
            System.out.println(
                    "学生名:"+ student.getName()
                            +"\t老师:"+student.getTeacher().getName());
        }
    }
}

5) operating results

 

 

 

Published 272 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/104728253