您的位置:首页 > 其它

MyBatis查询嵌套对象

2015-12-29 09:46 561 查看

前言

MyBatis支持查询时查询查出非基本类型的对象。

The association element deals with a “has-one” type relationship. For example, in our example, a Blog has one Author. An association mapping works mostly like any other result.

三种方式

Nested Select for Association

1.当一个select查询结束之后,再执行一次select,通过第二次的查询给Association赋值。

2.在association节点里使用column和select属性实现。

3.当需要传递多个参数给查询字句时,可以使用如下方式

column="{prop1=col1,prop2=col2}"


代码如下:

<resultMap id="blogResult" type="Blog">
<association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
SELECT * FROM BLOG WHERE ID = #{id}
</select>
<select id="selectAuthor" resultType="Author">
SELECT * FROM AUTHOR WHERE ID = #{id}
</select>


Nested Results for Association

查询的时候是联合了多张表一起查询,在association节点里使用resultMap属性配置给如何给对象属性映射。

代码如下:

<select id="selectBlog" resultMap="blogResult">
select
B.id            as blog_id,
B.title         as blog_title,
B.author_id     as blog_author_id,
A.id            as author_id,
A.username      as author_username,
A.password      as author_password,
A.email         as author_email,
A.bio           as author_bio
from Blog B left outer join Author A on B.author_id = A.id
where B.id = #{id}
</select>
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" resultMap="authorResult" />
</resultMap>
<resultMap id="authorResult" type="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</resultMap>


columnPrefix

association标签里包含columnPrefix这个属性,当使用方式二时,可以配合columnPrefix属性,在映射时,给resultMap里column对应的值添加前缀再去匹配。

<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author"
resultMap="authorResult" />
<association property="coAuthor"
resultMap="authorResult"
columnPrefix="co_" />
</resultMap>


Multiple ResultSets for Association(不建议使用)

一次访问数据库,执行两次select、返回两个resultSet,再处理这两个resultSet。

代码如下:

SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM AUTHOR WHERE ID = #{id}
<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult" statementType="CALLABLE">
{call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}
</select>
<resultMap id="blogResult" type="Blog">
<id property="id" column="id" />
<result property="title" column="title"/>
<association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="bio" column="bio"/>
</association>
</resultMap>


三种实现方式比较

方式一

劣势:这种方式会出现N+1次查询的情况,当查询的数据量较大时,效率会非常非常查,但是MyBatis有延迟加载,如果查询后就使用内部的对象,延迟加载也没有作用。

优势:使用比较方便。

方式二

劣势:使用不太方便。当联合的表很多时,使用起来非常麻烦。

优势:不会出现N+1次查询的情况。

方式三

劣势:Starting from version 3.2.3。并不是所有数据库都支持。使用不方便。

优势:不会出现N+1次查询的情况。

最后

查询多个非基本类型对象使用collection,用法和association有很小区别,如果需要,你可以通过下面的网站自行补充。

该文章参考自:http://www.mybatis.org/mybatis-3/sqlmap-xml.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: