MyBatis复杂数据类型处理

一、多对一处理(association)

1、

<mapper namespace="dao.StudentMapper">

    <select id="getStudent" resultMap="studentAndclass">
        select c.no,s.classid,s.NAME from tbstudent as s,tbclass as c where c.NO=s.CLASSID
    </select>
    <resultMap id="studentAndclass" type="pojo.Student">
        <result property="no" column="no"/>
        <result property="classid" column="classid"/>
        <result property="name" column="name"/>
        <association property="tbClass"  javaType="pojo.TbClass">
        <result property="no" column="no"/>
        </association>
    </resultMap>

2、

<mapper namespace="dao.StudentMapper">

    <select id="getStudent" resultMap="studentAndclass">
        select CLASSID from tbstudent
    </select>
    <resultMap id="studentAndclass" type="pojo.Student">
        <result property="no" column="no"/>
        <result property="classid" column="classid"/>
        <result property="name" column="name"/>
        <association property="tbClass" column="classid"  javaType="pojo.TbClass" select="getTbClass"/>
    </resultMap>
<select id="getTbClass" resultType="pojo.TbClass">
    select no from tbclass where NO=#{no}
</select>
</mapper>

二、一对多处理(collection)

1、

<mapper namespace="dao.TbClassMapper">
    <select id="getTbClass2" resultMap="getTbClassAndStudent" resultType="pojo.TbClass" >
        select s.classid,c.no,s.name,c.TID from tbclass as c,tbstudent as s where s.CLASSID=c.NO and c.no=#{no}
    </select>
    <resultMap id="getTbClassAndStudent" type="pojo.TbClass">
        <result property="no" column="no"/>
        <result property="name" column="name"/>
        <result property="tid" column="tid"/>
        <collection property="student" column="classid" ofType="pojo.Student">
            <result property="classid" column="classid"/>
            <result property="name" column="name"/>
            <result property="hometown" column="hometown"/>
        </collection>
    </resultMap>
</mapper>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis提供了多种方式来处理复杂的结果集映射。以下是一些常见的技术和方法: 1. 嵌套查询(Nested Queries:可以使用嵌套来处理一对一、一对多和多对多的关联关系。通过定义嵌套的resultMap,可以将结果集映射到包含嵌套对象的复杂数据结构中。 2. 关联查询(Association):通过使用association标签,可以将关联对象映射到主对象中。这适用于一对一和多对一的关联关系。 3. 集合查询(Collection):通过使用collection标签,可以将集合对象映射到主对象中。这适用于一对多和多对多的关联关系。 4. resultMap继承(ResultMap Inheritance):可以通过定义一个基础的resultMap,并在子resultMap中使用继承关系来重用已定义的映射规则。这样可以减少重复的配置。 5. 枚举型映射(Enum Mapping):如果结果集中包含枚举型的字段,可以使用处理器或者自定义的处理器来将其映射为对应的Java枚举型。 6. 自定义映射器(Custom Mappers):如果默认的映射方式无法满足需求,可以通过自定义映射器来实现复杂的结果集映射。自定义映射器可以通过实现ResultMapResolver接口来定义自己的映射规则。 这些只是一些常见的技术和方法,MyBatis在结果集映射方面提供了很多灵活的功能和选项,可以根据具体的需求选择适合的方式来处理复杂的结果集映射。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值