mybatis在关联映射中,引入外部xml文件中定义的对象

本文介绍MyBatis中如何使用association标签实现关联映射,通过Lottery2Rule与Lottery2Activity两个对象的XML配置文件示例,展示了如何定义和使用resultMap进行实体类与数据库字段之间的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

association标签

在mybatis中,关联映射是通过association标签实现的。

Lottery2Rule对象的Lottery2RuleDao.xml文件

<mapper namespace="cn.com.magicwifi.bz.betting.dao.Lottery2RuleDao">
    <!-- 返回结果映射 -->
    <resultMap id="BaseResultMap" type="cn.com.magicwifi.bz.betting.entity.Lottery2Rule">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="activity_id" property="activityId" jdbcType="INTEGER"/>
        <result column="prize_id" property="prizeId" jdbcType="INTEGER"/>
        <result column="rate" property="rate" jdbcType="INTEGER"/>
        <result column="count" property="count" jdbcType="INTEGER"/>
        <result column="remain_count" property="remainCount" jdbcType="INTEGER"/>
        <result column="create_at" property="createAt" jdbcType="TIMESTAMP"/>
    </resultMap>
</mapper>

Lottery2Activity对象的Lottery2ActivityDao.xml文件

<mapper namespace="cn.com.magicwifi.bz.betting.dao.Lottery2ActivityDao">
    <!-- 返回结果映射 -->
    <resultMap id="BaseResultMap" type="cn.com.magicwifi.bz.betting.entity.Lottery2Activity">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="start_date" property="startDate" jdbcType="TIMESTAMP"/>
        <result column="end_date" property="endDate" jdbcType="TIMESTAMP"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="create_at" property="createAt" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
     <resultMap id="selectContainPrize" extends="BaseResultMap"  type="cn.com.magicwifi.bz.betting.entity.Lottery2Rule" >
        <!-- 映射奖品  -->
        <!-- 在association 标签中,定义resultMap属性,属性值的前缀是奖品的命名空间,后面接定义的Id -->
        <association property="lottery2Prize" resultMap="cn.com.magicwifi.bz.betting.dao.Lottery2PrizeDao.BaseResultMap" javaType="cn.com.magicwifi.bz.betting.entity.Lottery2Prize">
           
        </association>
    </resultMap>

</mapper>

说明 cn.com.magicwifi.bz.betting.dao.Lottery2PrizeDao 这部分是Lottery2RuleDao.xml文件中的命名空间 BaseResultMap这个是Lottery2RuleDao.xml文件中定义的Id

转载于:https://my.oschina.net/u/1396185/blog/860160

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值