Mybatis 多(一)对一 关系查询

一、创建表

学生表:

 教师表:

 二、分步查询

SELECT s.id,s.Sname,t.Tname FROM student s,teacher t where s.t_id = t.id

设置实体类

复制代码
public class Student {
    private Integer id;
    private String Sname;
    private String sex;
    private Integer age;
    private Integer t_id;
    //这个是重点
    private Teacher teacher;
}
复制代码
public class Teacher {
    private Integer id;
    private String Tname;
}

查询语句;在studentMapper.xml当中设置分步查询

复制代码
<select id = "getStudent"  resultMap="StudentTeacher">
    select * from student;
</select>
<resultMap id="StudentTeacher" type="com.qcby.entity.Student"> <result property="id" column="id"/> <result property="Sname" column="Sname"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <result property="t_id" column="t_id"/> <association property="teacher" column="t_id"
      javaType="com.qcby.entity.Teacher"
      select="com.qcby.mapper.TeacherMapper.getTeacher"/> </resultMap>
复制代码
<select id="getTeacher" resultType="com.qcby.entity.Teacher" parameterType="java.lang.Integer">
    select  * from  teacher where id = #{t_id};
</select>

 https://blog.csdn.net/qq_53860947/article/details/123743724

posted @   Bonnie_ξ  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示