mybatis关于<collection>标签的使用

collection关于表一对多时候的使用

有时候我们在查询数据库时,需要以查询结果为查询条件进行关联查询。

在mybatis 中通过 association 标签(一对一查询,collection 一对多 查询) 实现延迟加载子查询
例子如下:

    <resultMap type="com.softetone.cloud.business.base.wechat.model.SysWechatMessageDtl" id="SysWechatMessageDtlMap">
        <id property="fid" column="fid"/>
        <result property="messageId" column="message_id"/>
        <result property="templateId" column="template_id"/>
        <result property="appId" column="app_id"/>
        <result property="isDefault" column="is_default"/>
        <result property="createId" column="create_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateId" column="update_id"/>
        <result property="updateTime" column="update_time"/>
        <collection property="wechatMessageOrgList" javaType="list" ofType="com.softetone.cloud.business.base.wechat.model.SysWechatMessageOrg"  select="querySysWechatMessageOrgByMessageId"
                    column="fid">
            <id property="fid" column="fid"/>
            <result property="dtlId" column="dtl_id"/>
            <result property="orgId" column="org_id"/>
            <result property="createId" column="create_id"/>
            <result property="createTime" column="create_time"/>
            <result property="updateId" column="update_id"/>
            <result property="updateTime" column="update_time"/>
        </collection>
    </resultMap>

column=“fid” 即是你传入 select=“querySysWechatMessageOrgByMessageId” 的参数列名

    <!--查询消息下的组织-->
    <select id="querySysWechatMessageOrgByMessageId" resultMap="SysWechatMessageOrgMap">
        select a.fid,
               a.dtl_id,
               a.org_id,
               b.org_name,
               a.create_id,
               a.create_time,
               a.update_id,
               a.update_time
        from cloud_server.sys_wechat_message_org a
        left join cloud_server.sys_org b on a.org_id = b.fid
        where a.dtl_id = #{fid}
    </select>

#{fid} 这里的 fid 要和 column=“fid” 列名保持一致才会正确

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis中的<resultMap>标签用于将查询结果集中的列与Java对象属性之间进行映射。使用<resultMap>标签可以灵活地对查询结果进行映射,可以将一个查询结果映射到多个Java对象中,也可以将多个查询结果映射到一个Java对象中。 <resultMap>标签的基本语法如下: ```xml <resultMap id="resultMap" type="javaType"> <!-- Result property --> <result property="propertyName" column="columnName" /> <!-- Other Result property element --> </resultMap> ``` 其中,id属性为<resultMap>标签的唯一标识符,type属性指定映射的Java类型。 <result>标签用于指定一个Java对象属性和查询结果集中的一列之间的映射关系。其中,property属性指定Java对象属性的名称,column属性指定查询结果集中的列名。 <resultMap>标签还支持一些高级属性,例如:继承其他<resultMap>标签、自动映射、高级映射等。 继承其他<resultMap>标签可以使得<resultMap>之间形成继承关系,从而减少重复的映射定义。 自动映射可以根据Java对象属性的名称和查询结果集中的列名之间的相似度自动生成映射关系。 高级映射可以将查询结果集中的一列映射到Java对象的一个属性中,并且该属性的值是由多个列合并而成的,可以使用<association>和<collection>标签实现。 总的来说,<resultMap>标签Mybatis中非常重要的一个标签,掌握其基本语法和高级用法对于开发高效的Mybatis应用非常有帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值