MyBatis中关于resultType和resultMap的区别

MyBatis中关于resultType和resultMap的区别
  MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的(对应着我们的model对象中的实体),而resultMap则是对外部ResultMap的引用(提前定义了db和model之间的隐射key-->value关系),但是resultType跟resultMap不能同时存在。
  在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。
  ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性
  ②当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。
  下面给出一个例子说明两者的使用差别:
[html]  view plain  copy
  1. package com.clark.model;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Goods {  
  6.     private Integer id;  
  7.     private Integer cateId;  
  8.     private String name;  
  9.     private double price;  
  10.     private String description;  
  11.     private Integer orderNo;  
  12.     private Date updateTime;  
  13.       
  14.     public Goods(){  
  15.           
  16.     }  
  17.       
  18.     public Goods(Integer id, Integer cateId, String name, double price,  
  19.             String description, Integer orderNo, Date updateTime) {  
  20.         super();  
  21.         this.id = id;  
  22.         this.cateId = cateId;  
  23.         this.name = name;  
  24.         this.price = price;  
  25.         this.description = description;  
  26.         this.orderNo = orderNo;  
  27.         this.updateTime = updateTime;  
  28.     }  
  29.   
  30.   
  31.     public Integer getId() {  
  32.         return id;  
  33.     }  
  34.   
  35.   
  36.     public void setId(Integer id) {  
  37.         this.id = id;  
  38.     }  
  39.   
  40.   
  41.     public Integer getCateId() {  
  42.         return cateId;  
  43.     }  
  44.   
  45.   
  46.     public void setCateId(Integer cateId) {  
  47.         this.cateId = cateId;  
  48.     }  
  49.   
  50.   
  51.     public String getName() {  
  52.         return name;  
  53.     }  
  54.   
  55.   
  56.     public void setName(String name) {  
  57.         this.name = name;  
  58.     }  
  59.   
  60.   
  61.     public double getPrice() {  
  62.         return price;  
  63.     }  
  64.   
  65.   
  66.     public void setPrice(double price) {  
  67.         this.price = price;  
  68.     }  
  69.   
  70.   
  71.     public String getDescription() {  
  72.         return description;  
  73.     }  
  74.   
  75.   
  76.     public void setDescription(String description) {  
  77.         this.description = description;  
  78.     }  
  79.   
  80.   
  81.     public Integer getOrderNo() {  
  82.         return orderNo;  
  83.     }  
  84.   
  85.   
  86.     public void setOrderNo(Integer orderNo) {  
  87.         this.orderNo = orderNo;  
  88.     }  
  89.   
  90.   
  91.     public Date getTimeStamp() {  
  92.         return updateTime;  
  93.     }  
  94.   
  95.   
  96.     public void setTimeStamp(Date updateTime) {  
  97.         this.updateTime = updateTime;  
  98.     }  
  99.   
  100.   
  101.     @Override  
  102.     public String toString() {  
  103.         return "[goods include:Id="+this.getId()+",name="+this.getName()+  
  104.                 ",orderNo="+this.getOrderNo()+",cateId="+this.getCateId()+  
  105.                 ",updateTime="+this.getTimeStamp()+"]";  
  106.     }  
  107. }  
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3.     "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4. <configuration>  
  5.     <typeAliases>  
  6.         <!-- give a alias for model -->  
  7.         <typeAlias alias="goods" type="com.clark.model.Goods"></typeAlias>  
  8.     </typeAliases>  
  9.     <environments default="development">  
  10.         <environment id="development">  
  11.             <transactionManager type="JDBC" />  
  12.             <dataSource type="POOLED">  
  13.                 <property name="driver" value="oracle.jdbc.driver.OracleDriver" />  
  14.                 <property name="url" value="jdbc:oracle:thin:@172.30.0.125:1521:oradb01" />  
  15.                 <property name="username" value="settlement" />  
  16.                 <property name="password" value="settlement" />  
  17.             </dataSource>  
  18.         </environment>  
  19.     </environments>  
  20.     <mappers>  
  21.         <mapper resource="com/clark/model/goodsMapper.xml" />  
  22.     </mappers>  
  23. </configuration></span>  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="clark">  
  5.     <resultMap type="com.clark.model.Goods" id="t_good">  
  6.         <id column="id" property="id"/>  
  7.         <result column="cate_id" property="cateId"/>  
  8.         <result column="name" property="name"/>  
  9.         <result column="price" property="price"/>  
  10.         <result column="description" property="description"/>  
  11.         <result column="order_no" property="orderNo"/>  
  12.         <result column="update_time" property="updateTime"/>  
  13.     </resultMap>  
  14.     <!--resultMap 和   resultType的使用区别-->  
  15.     <select id="selectGoodById" parameterType="int" resultType="goods">  
  16.         select id,cate_id,name,price,description,order_no,update_time   
  17.         from goods where id = #{id}  
  18.     </select>  
  19.       
  20.     <select id="selectAllGoods" resultMap="t_good">  
  21.         select id,cate_id,name,price,description,order_no,update_time from goods  
  22.     </select>  
  23.       
  24.     <insert id="insertGood" parameterType="goods">  
  25.         insert into goods(id,cate_id,name,price,description,order_no,update_time)    
  26.         values(#{id},#{cateId},#{name},#{price},#{description},#{orderNo},#{updateTime})  
  27.     </insert>  
  28. </mapper>  

[html]  view plain  copy
  1. package com.clark.mybatis;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5. import java.util.List;  
  6.   
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  11.   
  12. import com.clark.model.Goods;  
  13.   
  14. public class TestGoods {  
  15.     public static void main(String[] args) {  
  16.         String resource = "configuration.xml";  
  17.         try {  
  18.             Reader reader = Resources.getResourceAsReader(resource);  
  19.             SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);  
  20.             SqlSession session = sessionFactory.openSession();</span>  
[html]  view plain  copy
  1. <span style="font-size:18px;"><span style="white-space:pre">            </span>//使用resultType的情况  
  2.             Goods goods = (Goods)session.selectOne("clark.selectGoodById", 4);  
  3.             System.out.println(goods.toString());</span>  
[html]  view plain  copy
  1. <span style="font-size:18px;"><span style="white-space:pre">            </span>//使用resultMap的情况  
  2.             List<Goods> gs = session.selectList("clark.selectAllGoods");  
  3.             for (Goods goods2 : gs) {  
  4.                 System.out.println(goods2.toString());  
  5.             }  
  6. //          Goods goods = new Goods(4, 12, "clark", 12.30, "test is ok", 5, new Date());  
  7. //          session.insert("clark.insertGood", goods);  
  8. //          session.commit();  
  9.         } catch (IOException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  
  13. }  

结果输出为:
[html]  view plain  copy
  1. <span style="color:#cc0000;">[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---使用resultType的结果</span>  
[html]  view plain  copy
  1. <span style="color:#33ff33;">-------使用resultMap的结果-----------------</span>  
[goods include:Id=4,name=clark,orderNo=5,cateId=12,updateTime=Wed Sep 17 15:29:58 CST 2014][goods include:Id=1,name=诺基亚N85,orderNo=1,cateId=1,updateTime=Wed Sep 17 13:52:51 CST 2014]
[goods include:Id=2,name=金立 A30,orderNo=2,cateId=1,updateTime=Wed Sep 17 13:53:11 CST 2014][goods include:Id=3,name=金立 A30,orderNo=3,cateId=2,updateTime=Wed Sep 17 15:07:38 CST 2014]
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值