Mybatis 中的一对一,一对多,多对多的配置原则示例代码

所属分类: 软件编程 / java 阅读数: 93
收藏 0 赞 0 分享

什么是 MyBatis ?

MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

表:market_plan(营销计划(关联了用户))

market_plan_product(产品关联营销计划)
market_plan_label(标签关联营销计划)
market_plan_ideadata(创意素材关联营销计划)
user_ideadata_activity(活动关联用户,活动关联创意素材表)
user(用户表)

配置原则:A关联B,将A的resultMap关联(association)到B的resultMap中,这样才能在写关联查询sql语句的时候,查询出A,B表中的属性。多张表的关联也是一样的道理!

配置xml示例:

<resultMap id="userResultMap" type="com.mchuan.fastmarketplat.b.entity.User">
<id column="USER_ID" property="id" />
<result column="ACCOUNT" property="account" />
<result column="NAME" property="name" />
<result column="MOBILE" property="mobile" />
</resultMap>
<resultMap id="labelResultMap"
type="com.mchuan.fastmarketplat.b.entity.MarketPlanLabel">
<id property="id" column="LABEL_ID" />
<result property="gender" column="GENDER" />
<result property="area" column="AREA" />
<result property="age" column="AGE" />
<result property="deviceType" column="DEVICE_TYPE" />
<result property="communicationFee" column="COMMUNICATION_FEE" />
<result property="actionLabels" column="ACTION_LABELS" />
<result property="netScene" column="NET_SCENE" />
</resultMap>
<resultMap id="productResultMap"
type="com.mchuan.fastmarketplat.b.entity.MarketPlanProduct">
<id property="id" column="PRODUCT_ID" />
<result property="coverAmount" column="COVER_AMOUNT" />
</resultMap>
<resultMap id="activityResultMap"
type="com.mchuan.fastmarketplat.b.entity.UserActivity">
<id column="AC_ID" property="id" />
<result column="ACTIVITY_ID" property="activityId" jdbcType="INTEGER" />
<result column="COVER_URL" property="coverUrl" jdbcType="VARCHAR" />
<result column="ACTIVITY_URL" property="activityUrl" jdbcType="VARCHAR" />
<result column="VIEW_URL" property="viewUrl" jdbcType="VARCHAR" />
<result column="CREATE_TIME" property="createTime" />
<result column="UPDATE_TIME" property="updateTime" />
</resultMap>
<resultMap id="ideaDataResultMap"
type="com.mchuan.fastmarketplat.b.entity.MarketPlanIdeaData">
<id property="id" column="IDEA_DATA_ID" />
<result property="content" column="CONTENT" />
<result property="linkUrl" column="LINK_URL" />
<!-- ideaData关联属性 -->
<association property="userActivity"
javaType="com.mchuan.fastmarketplat.b.entity.UserActivity" resultMap="activityResultMap" />
</resultMap>
<resultMap id="BaseResultMap" type="com.mchuan.fastmarketplat.b.entity.MarketPlan">
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="PLAN_NAME" property="planName" jdbcType="VARCHAR" />
<result column="BUDGET" property="budget" jdbcType="DECIMAL" />
<result column="ACTUAL_BUDGET" property="actualBudget"
jdbcType="DECIMAL" />
<result column="DEMAND_TYPE" property="demandType" jdbcType="VARCHAR" />
<result column="START_DATE" property="startDate" />
<result column="END_DATE" property="endDate" />
<result column="CREATE_STATUS" property="createStatus"
jdbcType="INTEGER" />
<result column="CREATE_TIME" property="createTime" />
<result column="UPDATE_TIME" property="updateTime" />
<result column="NOTE" property="note" jdbcType="VARCHAR" />
<result column="STATUS" property="status" jdbcType="INTEGER" />
<result column="DESTPAGE_URL" property="destpageUrl" jdbcType="VARCHAR" />
<result column="IS_AWARD" property="isAward" jdbcType="INTEGER" />
<result column="AWARD_MONEY" property="awardMoney" jdbcType="DECIMAL" />
<result column="ADVERTISER" property="advertiser" jdbcType="VARCHAR" />
<result column="INDUSTRY" property="industry" jdbcType="INTEGER" />
<!-- marketPlan关联属性 -->
<association property="user"
javaType="com.mchuan.fastmarketplat.b.entity.User" resultMap="userResultMap" />
<association property="marketPlanLabel"
javaType="com.mchuan.fastmarketplat.b.entity.MarketPlanLabel"
resultMap="labelResultMap" />
<association property="marketPlanProduct"
javaType="com.mchuan.fastmarketplat.b.entity.MarketPlanProduct"
resultMap="productResultMap" />
<association property="marketPlanIdeaData"
javaType="com.mchuan.fastmarketplat.b.entity.MarketPlanIdeaData"
resultMap="ideaDataResultMap" />
</resultMap>

以上所述是小编给大家介绍的 Mybatis 中的一对一,一对多,多对多的配置原则示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

spring事务Propagation及其实现原理详解

这篇文章主要介绍了spring事务Propagation及其实现原理详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring Boot创建可执行jar包的实例教程

这篇文章主要介绍了Spring Boot创建可执行jar包的实例教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Spring Boot集成Mybatis的实例代码(简洁版)

这篇文章主要介绍了Spring Boot集成Mybatis简洁版的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring Boot创建非可执行jar包的实例教程

这篇文章主要介绍了Spring Boot创建非可执行jar包的实例教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

spring boot jar的启动原理解析

这篇文章主要介绍了 spring boot jar的启动原理分析,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot整合UEditor的示例代码

本篇文章主要介绍了SpringBoot整合UEditor的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java线程池FutureTask实现原理详解

这篇文章主要介绍了Java线程池FutureTask实现原理详解,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java并发之传统线程同步通信技术代码详解

这篇文章主要介绍了Java并发之传统线程同步通信技术代码详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java文件读写工具类分享

这篇文章主要为大家详细介绍了java文件读写工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

使用Spring Boot集成FastDFS的示例代码

本篇文章主要介绍了使用Spring Boot集成FastDFS的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多