java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis 多对一查询

Mybatis 多对一查询的实现方法

作者:小小代码熊

这篇文章主要介绍了Mybatis 多对一查询,本文通过场景分析示例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

架构

在这里插入图片描述

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

那么在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>

这里说明一下

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

子查询方法

<?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数据库表中的字段名了

到此这篇关于Mybatis 多对一查询的文章就介绍到这了,更多相关Mybatis 多对一查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文