什么是 MyBatis ?
MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優(yōu)秀的持久層框架。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數(shù)以及獲取結果集。MyBatis 可以對配置和原生Map使用簡單的 XML 或注解,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數(shù)據(jù)庫中的記錄。
表:market_plan(營銷計劃(關聯(lián)了用戶))
market_plan_product(產品關聯(lián)營銷計劃)
market_plan_label(標簽關聯(lián)營銷計劃)
market_plan_ideadata(創(chuàng)意素材關聯(lián)營銷計劃)
user_ideadata_activity(活動關聯(lián)用戶,活動關聯(lián)創(chuàng)意素材表)
user(用戶表)
配置原則:A關聯(lián)B,將A的resultMap關聯(lián)(association)到B的resultMap中,這樣才能在寫關聯(lián)查詢sql語句的時候,查詢出A,B表中的屬性。多張表的關聯(lián)也是一樣的道理!
配置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關聯(lián)屬性 --> <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關聯(lián)屬性 --> <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 中的一對一,一對多,多對多的配置原則示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/qq_32071077/article/details/59483589