Mybatis一个对象关联两个对象(多对一)

<mapper namespace="com.casic.smart.model.TopicNews">
    <!-- 项目专区信息 -->
    <resultMap id="ItemTopic" type="com.casic.smart.model.ItemTopic">
        <id property="id" column="topic_id" jdbcType="NUMERIC"/>
        <result property="name" column="topic_name" jdbcType="VARCHAR"/>
    </resultMap>
    <!-- 活动专区信息 -->
    <resultMap id="ThemeTopic" type="com.casic.smart.model.ThemeTopic">
        <id property="id" column="topic_id" jdbcType="NUMERIC"/>
        <result property="name" column="topic_name" jdbcType="VARCHAR"/>
    </resultMap>
    <!-- 项目新闻信息 -->     
    <resultMap id="TopicNews" type="com.casic.smart.model.TopicNews">
        <id property="id" column="news_id" jdbcType="NUMERIC"/>
        <result property="title" column="news_title" jdbcType="VARCHAR"/>
        <result property="author" column="news_author" jdbcType="VARCHAR"/>
        <result property="content" column="news_content" jdbcType="VARCHAR"/>
        <result property="publishTime" column="publish_time" jdbcType="DATE"/>
        <result property="relateItem" column="relate_item" jdbcType="NUMERIC"/>
        <result property="relateTheme" column="relate_theme" jdbcType="NUMERIC"/>
        <result property="userId" column="userId" jdbcType="NUMERIC"/>      
        <result property="orgId" column="orgId" jdbcType="NUMERIC"/>        
        <result property="advice" column="advice" jdbcType="VARCHAR"/>
        <result property="auditor" column="auditor" jdbcType="VARCHAR"/>
        <result property="groupAdvice" column="groupAdvice" jdbcType="VARCHAR"/>
        <result property="groupAuditor" column="groupAuditor" jdbcType="VARCHAR"/>      
        <result property="state" column="state" jdbcType="VARCHAR"/>

        <!-- 定义多对一关联信息 --> 
        <!-- 关联的第一个对象信息:项目专区对象 select属性是关键,嵌套查询--> 
        <association property="relateItemTopic" column="relate_item" javaType="com.casic.smart.model.ItemTopic" select="selectRelateItemTopic"/> 
        <!-- 关联的第二个对象信息:活动专区对象 -->  
        <association property="relateThemeTopic" column="relate_theme" javaType="com.casic.smart.model.ThemeTopic" select="selectRelateThemeTopic"/>  
    </resultMap>

    <!-- resultMap属性是关键,最初就是因为没有写这个属性,一直无法成功封装--> 
    <select id="selectRelateItemTopic" resultType="com.casic.smart.model.ItemTopic" resultMap="ItemTopic">  
    SELECT topic_id,topic_name,topic_intro  
    FROM zh_item_topic WHERE topic_id = #{relateItem}  
    </select>  
    <select id="selectRelateThemeTopic" resultType="com.casic.smart.model.ThemeTopic" resultMap="ThemeTopic">  
    SELECT topic_id,topic_name,topic_intro  
    FROM zh_theme_topic WHERE topic_id = #{relateTheme}  
    </select> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值