其他分享
首页 > 其他分享> > Mybatis09:XML 映射器(多对一的处理)

Mybatis09:XML 映射器(多对一的处理)

作者:互联网

多对一的理解:

1、数据库设计

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

2、搭建测试环境

2.1、编写实体类

Student

package com.study.pojo;

public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}

Teacher

package com.study.pojo;

public class Teacher {
    private int id;
    private String name;
}

2.2、编写实体类对应的Mapper接口

// Student
public interface StudentMapper {
}
// Teache
public interface TeacherMapper {
}

2.3、编写Mapper接口对应的 mapper.xml配置文件

Student

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

Teacher

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

</mapper>

3、按结果嵌套处理

  1. 接口方法编写

    public List<Student> getStudents2();
    
  2. 编写对应的mapper文件

    <!--
    按查询结果嵌套处理
    思路:
        1. 直接查询出结果,进行结果集的映射
    -->
    <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>
    
  3. 测试

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

4、按查询嵌套处理

  1. 接口方法编写

    public List<Student> getStudents();
    
  2. 编写对应的mapper文件

    <resultMap id="StudentTeacher" type="Student">
        <!--association关联属性  property属性名 javaType属性类型 column在多的一方的表中的列名-->
        <association property="teacher"  column="{id=tid,name=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} and name = #{name}
    </select>
    
  3. 测试

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

5、小结

【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂

标签:XML,name,映射器,public,VALUES,student,Mybatis09,tid,id
来源: https://blog.csdn.net/qq_43568728/article/details/121316650