mybatis多表外键查询
程序员文章站
2022-03-24 09:44:22
<?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
推荐阅读