复制代码
//多对一 方法一
<!--按结果嵌套-->

<
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>
复制代码

 

posted on   upupup-999  阅读(22)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!



点击右上角即可分享
微信分享提示