您好,登錄后才能下訂單哦!
什么是 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 中的一對一,一對多,多對多的配置原則示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。