您的位置:首页 > 其它

mybatis初步----查询之resultMap和resultType

2013-10-12 13:28 459 查看
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。

1、resultType

返回单个实例

<select id="selectUser" parameterType="int" resultType="User">

select * from user where id = #{id}

</select>
返回List集合

<select id="selectUserAll" resultType="User" > <!-- resultMap="userMap" -->
select * from user
</select>

2、resultMap

简单查询:

<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
column:数据库中列名称,property:类中属性名称

复制查询:以下转自:http://haohaoxuexi.iteye.com/blog/1337009

Xml代码



<!--来自CommentMapper.xml文件 -->

<resultMap type="Comment" id="CommentResult">

<association property="blog" select="selectBlog" column="blog" javaType="Blog"/>

</resultMap>

<select id="selectComment" parameterType="int" resultMap="CommentResult">

select * from t_Comment where id = #{id}

</select>

<select id="selectBlog" parameterType="int" resultType="Blog">

select * from t_Blog where id = #{id}

</select>

其访问情况是这样的,先是请求id为selectComment的select映射,然后得到一个id为CommentResult的 ResultMap对象,我们可以看到在对应的resultMap的返回类型是一个Comment对象,其中只有一个association节点,而没有 像前面说的简单查询所对应的id,result子节点,但是其仍会把对应的id等属性赋给Comment对象,这就是前面所说的MyBatis拥有自动封 装功能,只要你提供了返回类型,MyBatis会根据自己的判断来利用查询结果封装对应的对象,所以前面的简单查询中,如果你不在resultMap中明 确的指出id对应哪个字段,title对应哪个字段,MyBatis也会根据自身的判断来帮你封装,MyBatis的自身判断是把查询的field或其对 应的别名与返回对象的属性进行比较,如果相匹配且类型也相匹配,MyBatis则会对其进行赋值。在上面对应的resultMap中关联了一个blog属 性,其对应的JAVA类型为Blog,在上述的写法中,关联对象是通过子查询来进行关联的,当然也可以直接通过关联查询来进行关联。上面的 association子节点中,Property属性表示是resultMap返回类型的哪个关联属性,对于上面的例子就是Comment管理的 blog属性;select表示进行哪个select映射来映射对应的关联属性,即会去请求id为select所对应的值的select映射 来查询出其所关联的属性对象;Column表示当前关联对象在id为CommentResult的resultMap中所对应的键值对,该键值对将作为对 关联对象子查询的参数,即将把在selectComment中查询出来的blog属性的值作为参数传给进行关联对象blog的子查询selectBlog 的参数;javaType表示当前关联对象在JAVA中是什么类型。

上述介绍的是一对一或一对多的情况下,对一的一方的关联的查询。在实际应用中还有一个用的比较多的应用是通过一的一方查出对应的多的一方,在拿出多 的一方的时候也同样要把一的一方关联上,即在上述例子中,在拿出Blog对象的时候,就把其对应的Comment全部拿出来,在拿出Comment的时候 也还是需要把其对应的Blog拿出来,这是在JAVA中通过一次请求就拿出来的。写法如下:

Xml代码



<!-- 来自BlogMapper.xml文件 -->

<resultMap type="Blog" id="BlogResult">

<id column="id" property="id"/>

<collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"></collection>

</resultMap>

<resultMap type="Comment" id="CommentResult">

<association property="blog" javaType="Blog" column="blog" select="selectBlog"/>

</resultMap>

<select id="selectBlog" parameterType="int" resultMap="BlogResult">

select * from t_blog where id = #{id}

</select>

<!-- 通过Blog来查找Comment -->

<select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">

select * from t_Comment where blog = #{blogId}

</select>

上述请求的入口是id为selectBlog的select映射,返回结果为id为BlogResult的resultMap,id为 BlogResult的类型为Blog,其中指定了id的属性和字段,指定id将对MyBatis内部的构造作用非常大。其中关联了一个comments 对象,因为一个Blog可以有很多Comment,该comments为一个集合,所以用集合collection进行映射,其中的select还是表示 进行哪个子查询来查询对应的comments,column表示把上述查出来的哪个字段值当作参数传给子查询,ofType也是表示返回类型,这里的返回 类型是集合内部的类型,之所以用ofType而不是用type是MyBatis内部为了和关联association进行区别。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: