MyBatis有关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]
版权声明:本文博客原创文章。博客,未经同意,不得转载。
MyBatis有关resultType和resultMap差异的更多相关文章
- MyBatis中resultType和resultMap的区别
resultType和resultMap功能类似 ,都是返回对象信息 ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...
- [转]MyBatis中resultType与resultMap区别
MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...
- Mybatis中resultType和resultMap
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- mybatis中resultType和resultMap的联系
在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 比如,我们平时使用的单表查 ...
- mybatis 的 resulttype 和resultMap
resultType适合返回值比较简单的,比如一个数据类型,或者一个对象.比如对象的情况,是将表的列名和对象的属性一一对应的. 但是resultType无法处理返回值比较复杂的,特别是连接查询,需要用 ...
- Mybatis源码分析--返回值ResultType和ResultMap
这一篇博客我们来介绍一下Mybatis执行sql语句返回的结果值的到实体对象的映射机制.首先ResultType和ResultMap的使用方式是不同的. ResultType的使用方式: result ...
- 【mybatis笔记】 resultType与resultMap的区别
序言: 昨天做一个项目,看到很多刚开始用mybatis的同事对于resultType和resultMap的理解与使用含糊不清,这里我试图用最好理解的说法写一写,欢迎大家勘误. 两者异同: 相同点:re ...
- Mybatis笔记四:Mybatis中的resultType和resultMap查询操作实例详解
resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比 ...
- Mybatis中输出映射resultType与resultMap的区别
Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...
随机推荐
- angular风格指南
原文 https://www.jianshu.com/p/1a0a0a74769a 大纲 综述 1.单一职责 2.命名 3.LIFT-D应用程序结构 4.组件 综述 以下说的准则是根据angular官 ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [SCSS] Write similar classes with the SCSS @for Control Directive
Writing similar classes with minor variations, like utility classes, can be a pain to write and upda ...
- [Now] Deploy a Node project with Zeit’s Now
Use Zeit’s now to deploy a node application from your local machine to a remote cloud service in mom ...
- LinearLayout的一些注意事项 分类: H1_ANDROID 2013-10-26 23:01 856人阅读 评论(0) 收藏
1.orientation的默认值为horizontal,即从左向右排列.由于一般从上向下排列,所以必须指定orientation属性. 2.layout_gravity与gravity的区别: (1 ...
- TF-IDF模型
TF-IDF模型 1. 理论基础 由于数据挖掘所有数据都要以数字形式存在,而文本是以字符串形式存在.所以进行文本挖掘时需要先对字符串进行数字化,从而能够进行计算.TF-IDF就是这样一种技术,能够将字 ...
- ios开发级联菜单(利用父子控制器--两个菜单封装为两个子控制器来实现)
一:1:级联菜单可以使用两个tableView来实现,也可以利用父子控制器,两个控制器来实现,根视图控制器作为两个控制器的父控制器,来管理两个子控制器.2:将左右菜单分别交给两个控制器去管理,对于一些 ...
- php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错)
php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错) 一.总结 分类(那就可以把问题分小而逐步完成每个板块,这样是很简单的) 分类还有助于查错 二.php实现求扑克牌顺子 题目描 ...
- AJAX 相关笔记
AJAX (Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)). 平时工作中使用ajax的频率挺高的,这里整理了一些ajax相关的小知识,后续 ...
- 第二篇:_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
注意下文标红字段: #pragma mark- 输入改变时 - (void) textFieldDidChanged:(UITextField *) TextField{ //搜索关键字一旦改变,将重 ...