欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解Mybatis中的resultType和resultMap

 更新時(shí)間:2016年09月07日 10:28:56   作者:woshixuye  
這篇文章給大家介紹了mybatis中的resultType和resultMap的用法實(shí)例講解,MyBatis中在查詢進(jìn)行select映射的時(shí)候,返回類型可以用resultType,也可以用resultMap,至于兩種用法區(qū)別,通過(guò)本文一起學(xué)習(xí)吧

 一、概述

MyBatis中在查詢進(jìn)行select映射的時(shí)候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的,而resultMap則是對(duì)外部ResultMap的引用,但是resultType跟resultMap不能同時(shí)存在。

在MyBatis進(jìn)行查詢映射時(shí),其實(shí)查詢出來(lái)的每一個(gè)屬性都是放在一個(gè)對(duì)應(yīng)的Map里面的,其中鍵是屬性名,值則是其對(duì)應(yīng)的值。

①當(dāng)提供的返回類型屬性是resultType時(shí),MyBatis會(huì)將Map里面的鍵值對(duì)取出賦給resultType所指定的對(duì)象對(duì)應(yīng)的屬性。所以其實(shí)MyBatis的每一個(gè)查詢映射的返回類型都是ResultMap,只是
當(dāng)提供的返回類型屬性是resultType的時(shí)候,MyBatis對(duì)自動(dòng)的給把對(duì)應(yīng)的值賦給resultType所指定對(duì)象的屬性。

②當(dāng)提供的返回類型是resultMap時(shí),因?yàn)镸ap不能很好表示領(lǐng)域模型,就需要自己再進(jìn)一步的把它轉(zhuǎn)化為對(duì)應(yīng)的對(duì)象,這常常在復(fù)雜查詢中很有作用。

二、ResultType

Blog.java
public class Blog {
private int id;
private String title;
private String content;
private String owner;
private List<Comment> comments;
}

其所對(duì)應(yīng)的數(shù)據(jù)庫(kù)表中存儲(chǔ)有id、title、Content、Owner屬性。

<typeAlias alias="Blog" type="com.tiantian.mybatis.model.Blog"/>
<select id="selectBlog" parameterType="int" resultType="Blog">
select * from t_blog where id = #{id}
</select>

MyBatis會(huì)自動(dòng)創(chuàng)建一個(gè)ResultMap對(duì)象,然后基于查找出來(lái)的屬性名進(jìn)行鍵值對(duì)封裝,然后再看到返回類型是Blog對(duì)象,再?gòu)腞esultMap中取出與Blog對(duì)象對(duì)應(yīng)的鍵值對(duì)進(jìn)行賦值。

三、ResultMap

當(dāng)返回類型直接是一個(gè)ResultMap的時(shí)候也是非常有用的,這主要用在進(jìn)行復(fù)雜聯(lián)合查詢上,因?yàn)檫M(jìn)行簡(jiǎn)單查詢是沒(méi)有什么必要的。先看看一個(gè)返回類型為ResultMap的簡(jiǎn)單查詢,再看看復(fù)雜查詢的用法。

①簡(jiǎn)單查詢的寫(xiě)法

<resultMap type="Blog" id="BlogResult">
<id column="id" property="id"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
<result column="owner" property="owner"/>
</resultMap>
<select id="selectBlog" parameterType="int" resultMap="BlogResult">
select * from t_blog where id = #{id}
</select>

select映射中resultMap的值是一個(gè)外部resultMap的id,表示返回結(jié)果映射到哪一個(gè)resultMap上,外部resultMap的type屬性表示該resultMap的結(jié)果是一個(gè)什么樣的類型,這里是Blog類型,那么MyBatis就會(huì)把它當(dāng)作一個(gè)Blog對(duì)象取出。resultMap節(jié)點(diǎn)的子節(jié)點(diǎn)id是用于標(biāo)識(shí)該對(duì)象的id的,而result子節(jié)點(diǎn)則是用于標(biāo)識(shí)一些簡(jiǎn)單屬性的,其中的Column屬性表示從數(shù)據(jù)庫(kù)中查詢的屬性,Property則表示查詢出來(lái)的屬性對(duì)應(yīng)的值賦給實(shí)體對(duì)象的哪個(gè)屬性。簡(jiǎn)單查詢的resultMap的寫(xiě)法就是這樣的。

②復(fù)雜查詢

有一個(gè)Comment類,其中有一個(gè)Blog的引用,表示是對(duì)哪個(gè)Blog的Comment,那么在查詢Comment的時(shí)候把其對(duì)應(yīng)的Blog也要查出來(lái)賦給其blog屬性。

public class Comment {
private int id;
private String content;
private Date commentDate = new Date();
private Blog blog;
}
<!--來(lái)自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>

先是請(qǐng)求id為selectComment的select映射,然后得到一個(gè)id為CommentResult的ResultMap對(duì)象,可以看到在對(duì)應(yīng)的resultMap的返回類型是一個(gè)Comment對(duì)象,其中只有一個(gè)association節(jié)點(diǎn),而沒(méi)有像前面說(shuō)的簡(jiǎn)單查詢所對(duì)應(yīng)的id、result子節(jié)點(diǎn),但是其仍會(huì)把對(duì)應(yīng)的id等屬性賦給Comment對(duì)象,這就是前面所說(shuō)的MyBatis擁有自動(dòng)封裝功能,只要提供了返回類型,MyBatis會(huì)根據(jù)自己的判斷來(lái)利用查詢結(jié)果封裝對(duì)應(yīng)的對(duì)象,所以前面的簡(jiǎn)單查詢中,如果不在resultMap中明確的指出id對(duì)應(yīng)哪個(gè)字段,title對(duì)應(yīng)哪個(gè)字段,MyBatis也會(huì)根據(jù)自身的判斷來(lái)幫封裝,MyBatis的自身判斷是把查詢的field或其對(duì)應(yīng)的別名與返回對(duì)象的屬性進(jìn)行比較,如果相匹配且類型也相匹配,MyBatis則會(huì)對(duì)其進(jìn)行賦值。在上面對(duì)應(yīng)的resultMap中關(guān)聯(lián)了一個(gè)blog屬性,其對(duì)應(yīng)的java類型為Blog,在上述的寫(xiě)法中,關(guān)聯(lián)對(duì)象是通過(guò)子查詢來(lái)進(jìn)行關(guān)聯(lián)的,當(dāng)然也可以直接通過(guò)關(guān)聯(lián)查詢來(lái)進(jìn)行關(guān)聯(lián)。上面的association子節(jié)點(diǎn)中,Property屬性表示是resultMap返回類型的哪個(gè)關(guān)聯(lián)屬性,對(duì)于上面的例子就是Comment管理的blog屬性;select表示進(jìn)行哪個(gè)select映射來(lái)映射對(duì)應(yīng)的關(guān)聯(lián)屬性,即會(huì)去請(qǐng)求id為select所對(duì)應(yīng)的值的select映射 來(lái)查詢出其所關(guān)聯(lián)的屬性對(duì)象;Column表示當(dāng)前關(guān)聯(lián)對(duì)象在id為CommentResult的resultMap中所對(duì)應(yīng)的鍵值對(duì),該鍵值對(duì)將作為對(duì)關(guān)聯(lián)對(duì)象子查詢的參數(shù),即將把在selectComment中查詢出來(lái)的blog屬性的值作為參數(shù)傳給進(jìn)行關(guān)聯(lián)對(duì)象blog的子查詢selectBlog的參數(shù);javaType表示當(dāng)前關(guān)聯(lián)對(duì)象在JAVA中是什么類型。

上述介紹的是一對(duì)一或一對(duì)多的情況下,對(duì)一對(duì)一方的關(guān)聯(lián)的查詢。在實(shí)際應(yīng)用中還有一個(gè)用的比較多的應(yīng)用是通過(guò)一對(duì)一方查出對(duì)應(yīng)的多的一方,在拿出多的一方的時(shí)候也同樣要把一對(duì)一方關(guān)聯(lián)上:在拿出Blog對(duì)象時(shí),就把其對(duì)應(yīng)的Comment全部拿出來(lái),在拿出Comment的時(shí)候也還是需要把其對(duì)應(yīng)的Blog拿出來(lái),這是在java中通過(guò)一次請(qǐng)求就拿出來(lái)的。

<!-- 來(lái)自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>
<select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">
select * from t_Comment where blog = #{blogId}
</select>

上述請(qǐng)求的入口是id為selectBlog的select映射,返回結(jié)果為id為BlogResult的resultMap,id為BlogResult的類型為Blog,其中指定了id的屬性和字段,指定id將對(duì)MyBatis內(nèi)部的構(gòu)造作用非常大。其中關(guān)聯(lián)了一個(gè)comments對(duì)象,因?yàn)橐粋€(gè)Blog可以有很多Comment,該comments為一個(gè)集合,所以用集合collection進(jìn)行映射,其中的select還是表示進(jìn)行哪個(gè)子查詢來(lái)查詢對(duì)應(yīng)的comments,column表示把上述查出來(lái)的哪個(gè)字段值當(dāng)作參數(shù)傳給子查詢,ofType也是表示返回類型,這里的返回類型是集合內(nèi)部的類型,之所以用ofType而不是用type是MyBatis內(nèi)部為了和關(guān)聯(lián)association進(jìn)行區(qū)別。

public void selectCommentsByBlogTest() {
SqlSession session = Util.getSqlSessionFactory().openSession();
CommentMapper commentMapper = session.getMapper(CommentMapper.class);
List<Comment> comments = commentMapper.selectCommentsByBlog(6);
for (Comment comment : comments)
System.out.println(comment);
session.close();
}
public void testSelectOne() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
Blog blog = blogMapper.selectBlog(6);
List<Comment> comments = blog.getComments();
if (comments != null) {
for (Comment comment : comments)
System.out.println(comment);
}
session.close();
}

以上所述是小編給大家介紹的Mybatis中的resultType和resultMap,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論