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
故乡明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!