ibatis一对一、一对多实现以及性能分析

来源:互联网 发布:能写作的软件 编辑:程序博客网 时间:2024/06/08 06:08

    在数据库中比较常见一对一、一对多的数据,本文将叙述如何用多种方式实现这些关联关系,并对这些方法进行比对和分析。

    例子假设的场景如下:

    有两张表:product(产品)、category(种类),一个产品只属于某一个种类,但某一个种类下可以有多个产品。

一对一

    场景:

    查询某个产品,并列示出该产品所属的种类。   

    实现一对一的关系有两种方式。

    方式一:

<sqlMap namespace="one-to-one"><resultMap class="com.ibatis.example.domain.Product" id="get-product-result"><result property="id" column="PRD_ID" /><result property="description" column="PRD_DESCRIPTION" /><result property="category" column="PRD_CAT_ID" select="one-to-one.getCategory" /></resultMap><resultMap class="com.ibatis.example.domain.Category" id="get-category-result"><result property="id" column="CAT_ID" /><result property="description" column="CAT_DESCRIPTION" /></resultMap><select id="getProduct" parameterClass="int" resultMap="get-product-result">select *from PRODUCT where PRD_ID = #value#</select><select id="getCategory" parameterClass="int" resultMap="get-category-result">select *from CATEGORY where CAT_ID = #value#</select></sqlMap>

    方式二:

<sqlMap namespace="one-to-one-v2"><resultMap class="com.ibatis.example.domain.Product" id="get-product-result"><result property="id" column="PRD_ID" /><result property="description" column="PRD_DESCRIPTION" /><result property="category.id" column="CAT_ID" /><result property="category.description" column="CAT_DESCRIPTION" /></resultMap><select id="getProduct" parameterClass="int" resultMap="get-product-result">selectt1.prd_id, t1.prd_description, t2.cat_id, t2.cat_descriptionfromproduct t1, category t2 where t1.prd_cat_id = t2.cat_id and t1.prd_id= #value#</select></sqlMap>

    比对:

    1、方式一结构划分更加清晰,可重用性更好。

    2、方式二性能更高,实际上是方式一的改进。因为在方式一中,查询一次product表,会额外地查询一次category,这样实际上访问了数据库两次。但方式二利用联合查询,很好地避免了这个问题。

一对多

    场景:

    查询某个种类,并列出该种类下的所有产品。

    实现一对多的关系有两种方式。

    方式一:

<sqlMap namespace="one-to-many"><resultMap class="com.ibatis.example.domain.Category" id="get-category-result"><result property="id" column="CAT_ID" /><result property="description" column="CAT_DESCRIPTION" /><result property="products" column="CAT_ID" select="one-to-many.getProductByCatId" /></resultMap><resultMap class="com.ibatis.example.domain.Product" id="get-product-result"><result property="id" column="PRD_ID" /><result property="description" column="PRD_DESCRIPTION" /></resultMap><select id="getProductByCatId" parameterClass="int"resultMap="get-product-result">select *from PRODUCT where PRD_CAT_ID = #value#</select><select id="getCategory" parameterClass="int" resultMap="get-category-result">select *from CATEGORY where CAT_ID = #value#</select></sqlMap>

    方式二:

<sqlMap namespace="one-to-many-v2"><resultMap class="com.ibatis.example.domain.Category" id="get-category-result"groupBy="id"><result property="id" column="CAT_ID" /><result property="description" column="CAT_DESCRIPTION" /><result property="products" column="CAT_ID"resultMap="one-to-many-v2.get-product-result" /></resultMap><resultMap class="com.ibatis.example.domain.Product" id="get-product-result"><result property="id" column="PRD_ID" /><result property="description" column="PRD_DESCRIPTION" /></resultMap><select id="getCategory" parameterClass="int" resultMap="get-category-result">selectt1.prd_id, t1.prd_description, t2.cat_id, t2.cat_description fromproduct t1, category t2 where t1.prd_cat_id = t2.cat_id and t2.CAT_ID= #value#</select></sqlMap>

    比对:

    1、方式一结构更好,但是在查询时除了查询种类表,还会去查询产品表。如果是有多个种类表,则会多次去查询产品表。

    2、方式二是方式一的一种改进,通常来讲性能会更好一些。它是执行联合查询,将数据查询出来后,再根据groupBy关键字对数据进行归类。

    ibatis支持延迟加载,如果对种类下的产品不是特别关注,可以设置延迟加载,在使用到种类对应的产品时再去查询数据库。如果极少使用到种类对应的产品,很明显方式一会更加高效。

    在具体使用时,需要根据实际情况来决定采用何种方式。

    实例代码可参考:

http://download.csdn.net/detail/duwenchao1986/5048568
 

原创粉丝点击