思宇少卿 2017-12-31 16:14 采纳率: 100%
浏览 1879
已采纳

MyBatis高级association、collection 的使用

    <resultMap type="com.mybatis.bean.Customer" id="BaseResultMap">
        <id column="cust_id" property="custId"/>
        <result column="cust_name" property="custName"/>
        <result column="cust_age" property="custAge"/>  
        <collection property="orderSet" ofType="com.mybatis.bean.Order" column="cust_id"
     select="com.mybatis.mapper.OrderMapper.getOrderByCustId">          
        </collection>   
    </resultMap>

    <select id="getCustomerByCustId" resultMap="BaseResultMap">
        select cust_id,cust_name,cust_age
         from tbl_cust
        where cust_id=#{custId}
    </select>

    <select id="getCustomerAndOrederByCustId" resultMap="BaseResultMap">
        select cust_id,cust_name,cust_age
        from tbl_cust
        where cust_id=#{custId}
    </select>

在执行getCustomerByCustId方法时不需要关联查询,不执行resultMap中的collection,在执行getCustomerAndOrederByCustId方法是需要执行关联查询,我知道在getCustomerByCustId方法的查询语句中不查询cust_id可以解决这个问题,但是现在的需求是我要放回的结果中包含cust_id的,不知道这个该怎么处理?求大神指点迷津。。。

  • 写回答

5条回答 默认 最新

  • Seeker-Wu 2018-01-01 01:19
    关注

    建议你单独定义两个resultMap,其中一个有collection映射,一个没有colleaction映射,查询的时候分别映射不同的resultMap就行了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)