在Mybatis中的关联查询(一对多和多对一)的问题

一对一、多对一

<mapper namespace="com.lh.dao.StudentMapper">
    <!--结果嵌套-->
    <!--id必须唯一,resultType的值是结果集映射的表对应的实体类的全路径(此处在mabatis-config.xml中配置了别名),id自己设置-->
    <resultMap type="Student" id="StudentMap">
        <!-- id标签对应主键 -->
        <id property="id" column="id"/>
        <!-- 类的普通属性对应表的普通字段使用result标签 -->
        <result property="name" column="name"/>
        <result property="tid" column="tid"/>
    </resultMap>
<!--    为避免重复,resultMap可以继承其它resultMap
    查询的字段出现相同,要给其中一个设置别名,
    加了别名的字段,要在resultMap中修改属性与字段的映射-->
    <resultMap id="StudentTeacherMap" type="Student" extends="StudentMap">
        <!--在子resultMap中重新配置父的属性映射,
			子的映射可以覆盖父的映射,但只在子resultMap中生效,不影响父的resultMap的属性
        	此标签中的id属性映射sid字段
        	但在StudentMap中,id属性映射id字段-->
        <id property="id" column="sid"/>
        <id property="name" column="sname"/>
        <!--property是Student类中Teacher的属性名
        	column是关联查询的条件
        	javaType是Student类中Teacher的属性的类型
        	column="id"
        	resultMap是Student类中Teacher的属性对应的resultMap
        	如果这个resultMap不在当前的配置文件中,值就要写成resultMap所在的配置文件的namespace.resultMap的id
		例如:
		    id是TeacherMap的resultMap在namespace是com.xx.dao.TeacherMapper.xml配置文件中
			resultMap="com.lh.dao.TeacherMapper.TeacherMap" -->
        <association property="teacher" javaType="Teacher" column="id"
                     resultMap="com.lh.dao.TeacherMapper.TeacherMap">
             <!--在association 标签中配置了resultMap="com.lh.dao.TeacherMapper.TeacherMap。就不用配置result 标签-->
			<!--<result property="id" column="id"/>
            <result property="name" column="tname"/>-->
        </association>
    </resultMap>
    <!-- 在关联查询中,把查询写在主表的xml配置文件中
		如果在查询的过程中,查询的字段出现相同。那么要给其中的一个设置别名,保证没有重名的字段。
		加了别名的字段,要在resultMap中修改属性与字段的映射-->
    <select id="select" resultMap="StudentTeacherMap">
        select s.id sid,s.name sname,s.tid,
               t.id,t.name
        from student s LEFT JOIN teacher t on s.tid = t.id
    </select>
</mapper>

还有一种查询方式:

<!--查询嵌套-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher"
                     select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{id}
    </select><!--查询嵌套-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher"
                     select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{id}
    </select>

一对多、多对多

<mapper namespace="com.lh.dao.TeacherMapper">
    <!--resultType的值是实体类的全名称-->
    <resultMap type="Teacher" id="TeacherMap">
        <!-- id标签对应主键 -->
        <id property="id" column="id"/>
        <!-- 类的普通属性对应表的普通字段使用result标签 -->
        <result property="name" column="name"/>
    </resultMap>

    <resultMap id="TeacherStudentMap" type="Teacher" extends="TeacherMap">
   		<!-- 覆盖父的resultMap关于id的映射配置 -->
        <id property="id" column="tid"/>
        <id property="name" column="tname"/>
		<!-- collection 是用来配置实体类的集合型的属性
			property 实体类的集合的属性的属性名
			ofType 实体类的list集合的属性的泛型
			resultMap 实体类的集合型的属性的泛型对应的resultMap
			值要写成resultMap所在的配置文件的namespace.resultMap的id -->
        <collection property="students" ofType="Student" resultMap="com.lh.dao.StudentMapper.StudentMap">
        </collection>
    </resultMap>

    <select id="getTeacher1" resultMap="TeacherStudentMap" >
        select t.id tid,t.name tname,s.id,s.name
        from teacher t left join student s on t.id = s.tid
    </select>

</mapper>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值