• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MyBatis如何实现多表查询(多对一、一对多)

mybatis 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

MyBatis实现多表查询

 一、多对一查询

数据库的准备

创建两张表,一张老师表,一张学生表

将老师主键id关联学生外键tid

创建sql的语句

create table teacher(
   id int  primary key,
	teacher_name varchar(30) not null
)

insert into teacher(id,teacher_name) values (1,'毛老师')

create table student(
  id int  primary key,
	student_name varchar(30) not null,
	tid int default null
)

//建立主外键关联
alter table student add constraint teacher_student_id foreign key (tid) references teacher(id)

insert into student values (1,'小明',1)
insert into student values (2,'小毛',1)
insert into student values (3,'小红',1)
insert into student values (4,'大黄',1)
insert into student values (5,'超儿',1)

项目结构

使用Lombok插件,创建实体类。

(提高整洁度,主要想toulan)

@Data
public class Student {
    private int id;
    private String name;
    //学生需要关联一个老师
    private Teacher teacher;
}
@Data
public class Teacher {
    private int id;
    private String name;
}

1、嵌套查询处理

编写接口

public interface StudentMapper {
    //查询所有学生的信息以及对应老师的信息
    public List<Student> getStudent();
}

2. 编写StudentMapper.xml的查询语句(重点)

<mapper namespace="dao.StudentMapper">
<!--    思路:
        1. 查询所有学生的信息
        根据查询出来的学生tid,寻找对应的老师

-->
    <select id="getStudent" resultMap="StudentTeacher">
    select * from student
    </select>
    <resultMap id="StudentTeacher" type="pojo.Student">
<!--        复杂的属性需要单独处理 是对象就使用association,是集合就使用collection-->
<!--    select 子查询    -->
        <result property="name" column="student_name"/>
        <association property="teacher" column="tid" javaType="pojo.Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="pojo.Teacher">
        select * from teacher where id=#{id}
    </select>

测试类

    @Test
    public void getStudent(){

        SqlSession sqlSession = Mybatisutil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudent();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }

测试结果

2、联合查询处理

编写接口

//按照结果嵌套查询
public List<Student> getStudent2();

2. 编写StudentMapper.xml的查询语句(重点)

<!--    按照结果嵌套处理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid,s.student_name sname,t.teacher_name tname
    from student s,teacher t
    where s.tid=t.id
    </select>
    <resultMap id="StudentTeacher2" type="pojo.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="pojo.Teacher<em style="color:transparent">本文来源gao.dai.ma.com搞@代*码#网</em>">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MyBatis如何实现多表查询(多对一、一对多)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址