//多对一 方法一
<!--按结果嵌套-->
<select id="getStudentAndTeacher2" resultMap="StudentTeacher2"> select s.id as sid,s.name as sname,t.name as tname,s.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>
//多对一 方法二 子查询
<resultMap id="StudentTeacher" type="Student"> <!--复杂的属性需要单独处理 association:对象 collection:集合 --> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getStudentAndTeacher" resultMap="StudentTeacher"> select * from student </select> <select id="getTeacher" resultType="Teacher"> select * from teacher where id = #{id} </select>
一对多
<select id="getTeacherList" resultType="com.my.pojo.Teacher"> select * from teacher </select> <!--按结果嵌套--> <select id="getTeacherAndStudents" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid = t.id and t.id = #{id} </select> <resultMap id="TeacherStudent" type="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!--javaType:指定属性的类型 ofType:指定集合中的泛型信息--> <collection property="studentList" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--按查询嵌套--> <select id="getTeacherAndStudents2" resultMap="TeacherStudent2"> select * from teacher where id = #{id} </select> <resultMap id="TeacherStudent2" type="Teacher"> <result property="id" column="id"/> <result property="name" column="name"/> <collection column="id" property="studentList" javaType="ArrayList" ofType="Student" select="getStudents"/> </resultMap> <select id="getStudents" resultType="Student"> select * from student where tid = #{id} </select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!