mybatis多表外键查询
程序员文章站
2022-07-03 11:44:40
<?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.ccy.dao.StudentMapper">
<!-- 第一种-->
<select id="getStudentList" resultMap="StudentTeacher">
select * from student;
</select>
<resultMap id="StudentTeacher" type="student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="teacher" select="getTeacherById"/>
</resultMap>
<select id="getTeacherById" resultType="teacher">
select * from teacher where id=#{id}
</select>
<!--第二种-->
<select id="getStudentList2" resultMap="StudentTeacher2">
SELECT
s.id sid,
s.NAME sname,
t.NAME tname,
t.id tid
FROM
student s,
teacher t
WHERE
s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
package com.ccy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
private int id;
private String name;
}
package com.ccy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
本文地址:https://blog.csdn.net/qq_42267491/article/details/107640955
推荐阅读
-
详解MySQL数据库--多表查询--内连接,外连接,子查询,相关子查询
-
.netcore2.1 ef 使用外键关联查询
-
Sql Server 查询外键对应的Table 的通用方法
-
Mybatis基于注解实现多表查询功能
-
MyBatis从入门到精通(三):MyBatis XML方式的基本用法之多表查询
-
基于SQL2005 SQL2008 表结构信息查询升级版的详解(含外键信息)
-
mybatis Plus 多表联合查询
-
Spring Boot+mybatis多表链接查询,重复外键
-
MyBatis 多表关联查询(手动映射)
-
Mybatis、Mybatis-Plus多表联合查询,通过注解实现,自定义SQL参考实现