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]

				
时间: 2024-08-11 03:28:19

MyBatis中关于resultType和resultMap的区别的相关文章

Mybatis中的resultType和resultMap 区别

Mybatis中的resultType和resultMap 是mybatis 中返回类型一定用到的,但不会同时出现.mybatis返回类型肯定是map结构,然后根据返回类型是map还是对象类型,再转换. 在给对象设置属性的时候,两个方法肯定会调用. private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException { final ResultLoaderMap lazyLoader

Mybatis中的resultType和resultMap

一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值. ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给r

mybatis中resultType和resultMap的区别

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用但是resultType跟resultMap不能同时存在.在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值.当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultT

mybatis &lt;sql /&gt; 配置中 返回值 resultType 与resultMap的区别

mybatis的objectMapper.xml中, 1) 若<sql /> 查询语句中配置的是resultType=“实体类/DTO” ,则从mybatis返回的键值对结果集(Map)会直接赋值给该DTO(根据set()/get()方法,因此该DTO属性名要与表字段名一致,若不一致,可再sql查询语句中用as 更换查出来表字段名)中相映射的属性值,而与该mappper.xml文件配置<mapper/>里的<resultMap />无关. 2)若<sql />

Mybatis中输出映射resultType与resultMap的区别

(原文地址:http://blog.csdn.net/acmman/article/details/46509375) 1.resultType 使用resultType进行输出映射,只有查询出来的列名和pojo(实体bean)中的属性名一致,该列才可以映射成功. 如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象.只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象. 1.1输出pojo对象和pojo列表 不管是输出的pojo单个对象还是一个列表(list中

mybatis配置文件resultType和resultMap的区别以及mybatis自带的别名

returnType是自定义的类或者jdk自带的类 resultMap是在mapperXMl文件中通过resultMap节点定义出来的 例如: <resultMap id="BaseResultMap" type="com.sinosoft.reins.POJO.model.PrpMaxNo" >     <id column="GROUPNO" property="groupno" jdbcType=&qu

MyBatis Review——使用resultType和resultMap实现一对一查询

例如: 查询订单信息,关联查询创建订单的用户信息. 查询语句: SELECT orders.*, USER .username ,USER .sex, USER .address FROM orders, USER WHERE orders.user_id = USER .id 查询结果: 1,使用resultType接受输出结果 用户信息: 订单信息: 用于接收结果的pojo: 为了将查询结果映射到pojo中,pojo必须包括所有查询列名.在原始orders对象不能接受所有查询字段的时候,定义

mybatis和Dao映射的配置文件xml,中什么时候需要用resultType .什么时候用resultMap,及resultType和resultMap的区别

区别: 两者都可以用于映射文件中的<select>语句的返回值,但是两者在返回值上面是有区别的 如下面的两个例子: 使用resultType的 举个例子吧,例子以ibatis为例: 你有个User 对象, 拥有两个字段id,name. 1.你要获取id为123的name String name = (String) queryForObject("getUserNameByID", id); <select id="getUserNameByID"

Mybatis的mapper文件中#和$的区别 以及 resultType和resultMap的区别

一般#{}用于传递查询的参数,一般用于从dao层传递一个string或者其他的参数过来,mybatis对这个参数会进行加引号的操作,将参数转变为一个字符串. SELECT * FROM employee WHERE name="jack" 而$则不同,我们一般用于ORDER BY的后面. SELECT * FROM employee ORDER BY salary MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,