最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis中關(guān)于resultType和resultMap的區(qū)別介紹

 更新時(shí)間:2016年09月07日 10:37:56   作者:caolipeng_918  
MyBatis中在查詢進(jìn)行select映射的時(shí)候,返回類型可以用resultType,也可以用resultMap,那么MyBatis中關(guān)于resultType和resultMap的區(qū)別是什么呢?下面小編通過本文給大家解答下

MyBatis中在查詢進(jìn)行select映射的時(shí)候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的(對(duì)應(yīng)著我們的model對(duì)象中的實(shí)體),而resultMap則是對(duì)外部ResultMap的引用(提前定義了db和model之間的隱射key-->value關(guān)系),但是resultType跟resultMap不能同時(shí)存在。

在MyBatis進(jìn)行查詢映射時(shí),其實(shí)查詢出來的每一個(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ù)雜查詢中很有作用。

下面給出一個(gè)例子說明兩者的使用差別:

package com.clark.model; 
import java.util.Date; 
public class Goods { 
private Integer id; 
private Integer cateId; 
private String name; 
private double price; 
private String description; 
private Integer orderNo; 
private Date updateTime; 
public Goods(){ 
} 
public Goods(Integer id, Integer cateId, String name, double price, 
String description, Integer orderNo, Date updateTime) { 
super(); 
this.id = id; 
this.cateId = cateId; 
this.name = name; 
this.price = price; 
this.description = description; 
this.orderNo = orderNo; 
this.updateTime = updateTime; 
} 
public Integer getId() { 
return id; 
} 
public void setId(Integer id) { 
this.id = id; 
} 
public Integer getCateId() { 
return cateId; 
} 
public void setCateId(Integer cateId) { 
this.cateId = cateId; 
} 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
public double getPrice() { 
return price; 
} 
public void setPrice(double price) { 
this.price = price; 
} 
public String getDescription() { 
return description; 
} 
public void setDescription(String description) { 
this.description = description; 
} 
public Integer getOrderNo() { 
return orderNo; 
} 
public void setOrderNo(Integer orderNo) { 
this.orderNo = orderNo; 
} 
public Date getTimeStamp() { 
return updateTime; 
} 
public void setTimeStamp(Date updateTime) { 
this.updateTime = updateTime; 
} 
@Override 
public String toString() { 
return "[goods include:Id="+this.getId()+",name="+this.getName()+ 
",orderNo="+this.getOrderNo()+",cateId="+this.getCateId()+ 
",updateTime="+this.getTimeStamp()+"]"; 
} 
} 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
<typeAliases> 
<!-- give a alias for model --> 
<typeAlias alias="goods" type="com.clark.model.Goods"></typeAlias> 
</typeAliases> 
<environments default="development"> 
<environment id="development"> 
<transactionManager type="JDBC" /> 
<dataSource type="POOLED"> 
<property name="driver" value="oracle.jdbc.driver.OracleDriver" /> 
<property name="url" value="jdbc:oracle:thin:@172.30.0.125:1521:oradb01" /> 
<property name="username" value="settlement" /> 
<property name="password" value="settlement" /> 
</dataSource> 
</environment> 
</environments> 
<mappers> 
<mapper resource="com/clark/model/goodsMapper.xml" /> 
</mappers> 
</configuration></span> 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="clark"> 
<resultMap type="com.clark.model.Goods" id="t_good"> 
<id column="id" property="id"/> 
<result column="cate_id" property="cateId"/> 
<result column="name" property="name"/> 
<result column="price" property="price"/> 
<result column="description" property="description"/> 
<result column="order_no" property="orderNo"/> 
<result column="update_time" property="updateTime"/> 
</resultMap> 
<!--resultMap 和 resultType的使用區(qū)別--> 
<select id="selectGoodById" parameterType="int" resultType="goods"> 
select id,cate_id,name,price,description,order_no,update_time 
from goods where id = #{id} 
</select> 
<select id="selectAllGoods" resultMap="t_good"> 
select id,cate_id,name,price,description,order_no,update_time from goods 
</select> 
<insert id="insertGood" parameterType="goods"> 
insert into goods(id,cate_id,name,price,description,order_no,update_time) 
values(#{id},#{cateId},#{name},#{price},#{description},#{orderNo},#{updateTime}) 
</insert> 
</mapper> 
package com.clark.mybatis; 
import java.io.IOException; 
import java.io.Reader; 
import java.util.List; 
import org.apache.ibatis.io.Resources; 
import org.apache.ibatis.session.SqlSession; 
import org.apache.ibatis.session.SqlSessionFactory; 
import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
import com.clark.model.Goods; 
public class TestGoods { 
public static void main(String[] args) { 
String resource = "configuration.xml"; 
try { 
Reader reader = Resources.getResourceAsReader(resource); 
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 
SqlSession session = sessionFactory.openSession();</span> 
<span style="font-size:18px;"><span style="white-space:pre"> </span>//使用resultType的情況 
Goods goods = (Goods)session.selectOne("clark.selectGoodById", 4); 
System.out.println(goods.toString());</span> 
[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
<span style="font-size:18px;"><span style="white-space:pre"> </span>//使用resultMap的情況 
List<Goods> gs = session.selectList("clark.selectAllGoods"); 
for (Goods goods2 : gs) { 
System.out.println(goods2.toString()); 
} 
// Goods goods = new Goods(4, 12, "clark", 12.30, "test is ok", 5, new Date()); 
// session.insert("clark.insertGood", goods); 
// session.commit(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

結(jié)果輸出為:

<span style="color:#cc0000;">[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---使用resultType的結(jié)果</span> 
<span style="color:#33ff33;">-------使用resultMap的結(jié)果-----------------</span>

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

相關(guān)文章

  • 2020最新 idea下載、安裝與創(chuàng)建項(xiàng)目測試的教程圖解

    2020最新 idea下載、安裝與創(chuàng)建項(xiàng)目測試的教程圖解

    這篇文章主要介紹了2020最新 idea下載、安裝與創(chuàng)建項(xiàng)目測試的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • jdk7 中HashMap的知識(shí)點(diǎn)總結(jié)

    jdk7 中HashMap的知識(shí)點(diǎn)總結(jié)

    HashMap的原理是老生常談了,不作仔細(xì)解說。一句話概括為HashMap是一個(gè)散列表,它存儲(chǔ)的內(nèi)容是鍵值對(duì)(key-value)映射。這篇文章主要總結(jié)了關(guān)于jdk7 中HashMap的知識(shí)點(diǎn),需要的朋友可以參考借鑒,一起來看看吧。
    2017-01-01
  • Jenkins集成SonarQube的方法詳解

    Jenkins集成SonarQube的方法詳解

    這篇文章主要介紹了Jenkins集成SonarQube的方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java?SpringMvc中攔截器的應(yīng)用

    java?SpringMvc中攔截器的應(yīng)用

    大家好,本篇文章主要講的是java?SpringMvc中攔截器的應(yīng)用,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”的原因和解決

    IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”的原因和解決

    本文主要介紹了IDEA編寫SpringBoot項(xiàng)目時(shí)使用Lombok報(bào)錯(cuò)“找不到符號(hào)”,詳細(xì)介紹了幾種可能會(huì)出現(xiàn)的問題及其解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • 最新評(píng)論

    随州市| 金平| 丰都县| 巴林右旗| 靖州| 盖州市| 积石山| 广宗县| 沙洋县| 肇源县| 凤冈县| 四川省| 顺义区| 溧水县| 江华| 嘉义县| 平武县| 巨野县| 碌曲县| 运城市| 衡东县| 天气| 报价| 河西区| 汾阳市| 建始县| 炎陵县| 南丰县| 松溪县| 定陶县| 清丰县| 同德县| 乌苏市| 永登县| 烟台市| 边坝县| 方正县| 白河县| 小金县| 四会市| 汾西县|