思宇少卿 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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样