欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-31 18:33:22
什么是 mybatis ? mybatis 是支持定制化 sql、存储过程以及高级映射的优秀的持久层框架。mybatis 避免了几乎所有的 jdbc 代码和手动设置参...

什么是 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 中的一对一,一对多,多对多的配置原则示例代码,希望对大家有所帮助