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不能非常好表示领域模型,就须要自己再进一步的把它转化为相应的对象,这经常在复杂查询中非常有作用。

  以下给出一个样例说明两者的使用区别:
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的使用差别-->
<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>
<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();
}
}
}

结果输出为:

<span style="color:#cc0000;">[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---使用resultType的结果</span>
<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]


版权声明:本文博客原创文章。博客,未经同意,不得转载。

最新文章

  1. 单色半透明-兼容IE7
  2. 使用IntelliJ IDEA 配置Maven(入门)(转)
  3. windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境
  4. Android Toast效果设置
  5. OLAT &amp; OLTP
  6. centos时间同步方法
  7. Delphi NativeXML 乱码的问题
  8. COJ 1003 WZJ的数据结构(三)ST表
  9. SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
  10. GIS制图课程目录(持续整理)
  11. JavaWeb开发环境搭建Eclipse配置Tomcat
  12. Linux:sheel脚本for的用法,及日期参数+1day用法
  13. 2-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案安全篇(监听Wi-Fi和APP的数据)
  14. __init__和__new__的区别
  15. chrome的console功能
  16. 原码、补码、反码的概念和java数的存储方式
  17. KADEMLIA算法
  18. 时间戳转中国人能看得懂的日期格式 yy-mm-dd
  19. python自动化开发-6-常用模块-续
  20. Github学习心得体会

热门文章

  1. yield return
  2. 洛谷 P1984 [SDOI2008]烧水问题
  3. ios开发之坐标系转换
  4. Django之分页显示文章
  5. ### Hibernate中的事务与并发 ###
  6. poj 2689 Prime Distance(大区间筛素数)
  7. 在nginx中使用lua直接訪问mysql和memcaced达到数据接口的统一
  8. [React] Render Basic SVG Components in React
  9. java nio 缓冲区(一)
  10. c#代码01--控制台的简单输入与输出及日期的格式输出