首页 > 其他 > 详细

多对一处理-Mybatis

时间:2021-07-08 18:10:29      阅读:23      评论:0      收藏:0      [点我收藏+]

按照查询嵌套处理

   <!--思路:
		1. 查询所有的学生信息
		2. 根据查询出来的学生的tid 寻找对应的老师 子查询
	-->

<select id="getAllStudents" resultMap="StudentMap">
        select * from mybatis.student;
    </select>

    <resultMap id="StudentMap" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid"  javaType="Teacher" 				select="getTeachers"/>
    </resultMap>

    <select id="getTeachers" resultType="teacher">
        select * from mybatis.teacher where id =#{tid};
    </select>
    

安装结果嵌套处理

<!--第二种按查询结果嵌套处理-->
    <select id="getAllStudents2" resultMap="StudentMap2" >
        select s.id sid,s.name sname,t.name tname, t.id tid
        from mybatis.student s, mybatis.teacher t
        where s.tid = t.id;
    </select>

    <resultMap id="StudentMap2" 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>

多对一处理-Mybatis

原文:https://www.cnblogs.com/withLevi/p/14985661.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!