Mybatis 多对一查询

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis 多对一查询

小小代码熊   2022-05-23 我要评论

架构

在这里插入图片描述

这里从学生的角度来说就是多对一的场景

那么在Java中是怎么样的呢?

环境搭建

数据库

CREATE TABLE `student`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(20) DEFAULT NULL 
)ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO student VALUES (1,"李老师");
CREATE TABLE `student` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(20) DEFAULT NULL ,
`tid` INT(4) NOT NULL,
KEY `fk_tid` (`tid`),
CONSTRAINT fk_tid FOREIGN KEY(`tid`) REFERENCES `teacher`(`id`)
INSERT INTO student VALUES
(1,"小红",1),
(2,"小绿",1),
(3,"小灯",1),
(4,"小勿",1),
(5,"小闯",1);

在这里插入图片描述

实体类

Student

package pojo;

import lombok.Data;
@Data
public class Student {
    private int id;
    private String name;
    private Teacher teacher;//这里因为我们有一个对应的关系,学生要对应到一个老师,所以要使用Teacher类型
}

Teacher

package pojo;

import lombok.Data;
@Data
public class Teacher {
    private int id;
    private String name;
}

接口

(因为这里是多对一,所以我就没有用到TeacherMapper)
StudentMapper

package dao;

import pojo.Student;
import java.util.List;
public interface StudentMapper {
    List<Student> getStudents();
    List<Student> getStudents2();
}

TeacherMapper

package dao;
public interface TeacherMapper {
}

Mapper配置文件

TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.TeacherMapper">
</mapper>

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentMapper">
<!--  第二种  -->
    <select id="getStudents2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid=t.id;
    </select>
    <resultMap id="StudentTeacher2" type="Student" >
        <result column="sid" property="id" />
        <result column="sname" property="name" />
        <association property="teacher" javaType="Teacher">
            <result property="id" column="tid" />
            <result property="name" column="tname" />
        </association>
    </resultMap>
<!--  第一种  -->
    <select id="getStudents" resultMap="StudentTeacher">
        select * from student;
    </select>
    <resultMap id="StudentTeacher" type="Student" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <association property="teacher" column="tid" javaType="Teacher" select="getTeachers" />
    </resultMap>
    <select id="getTeachers" resultType="Teacher">
        select * from teacher where id=#{id};
    </select>
</mapper>

这里说明一下

  • association 是相对于对象的
  • collection是相对于集合的

然后这里有两种方法,我们一种一种看

子查询方法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentMapper">
<!--  第一种  -->
    <select id="getStudents" resultMap="StudentTeacher">
        select * from student;
    </select>
    <resultMap id="StudentTeacher" type="Student" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <association property="teacher" column="tid" javaType="Teacher" select="getTeachers" />
    </resultMap>

    <select id="getTeachers" resultType="Teacher">
        select * from teacher where id=#{id};
</mapper>

也就是这种方法,它其实是相当于我们Sql里面的子查询,也就是一层查询里面嵌套一层查询

按结果集查询

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.StudentMapper">
<!--  第二种  -->
<select id="getStudents2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid=t.id;
    </select>
    <resultMap id="StudentTeacher2" type="Student" >
        <result column="sid" property="id" />
        <result column="sname" property="name" />
        <association property="teacher" javaType="Teacher">
            <result property="id" column="tid" />
            <result property="name" column="tname" />
        </association>
    </resultMap>
</mapper>

这种方法就是我们一次性把结果查询出来然后对结果集做一下处理
这里特别说明一下column这个属性,这个可不是数据库中你需要的字段名,而是你查询出来的那张表的字段名,这里起了别名之后就不再是原来teacher数据库表中的字段名了

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们