什么是 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示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<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" />
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- 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 中的一对一,一对多,多对多的配置原则示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/qq_32071077/article/details/59483589