MyBatis中关于resultType和resultMap的区别介绍_java

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>
[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();
}
}
} 

结果输出为:

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

 以上所述是小编给大家介绍的MyBatis中关于resultType和resultMap的区别介绍,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索mybatis
, resulttype和resultmap
mybatis中resulttype
mybatis resulttype、mybatis的resulttype、mybatis中resulttype、mybatis3 resulttype、mybatis resultmap,以便于您获取更多的相关知识。

时间: 2024-08-03 10:22:38

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

Mybatis中的resultType和resultMap查询操作实例详解_java

resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比如:列名和对象属性名不一致时可以使用resultMap来配置:还有查询的对象中包含其他的对象等. MyBatisConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configura

Mybatis中的resultType和resultMap

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

深入理解Mybatis中的resultType和resultMap_java

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

Laravel模板引擎Blade中section的一些标签的区别介绍

 这篇文章主要介绍了Laravel模板引擎Blade中section的一些标签的区别介绍,本文讲解了@yield 与 @section.@show 与 @stop.@append 和 @override的区别,需要的朋友可以参考下     Laravel 框架中的 Blade 模板引擎,很好用,但是在官方文档中有关 Blade 的介绍并不详细,有些东西没有写出来,而有些则是没有说清楚.比如,使用中可能会遇到这样的问题: 1.@yield 和 @section 都可以预定义可替代的区块,这两者有什

jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍_jquery

insertBefore():a.insertBefore(b) a在前,b在后, a:是一个选择器,b:也是一个选择器 <!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>jqu</title> <script type="text/javascript" src='jquery-2.2.0.min.js'></script&g

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

      例如:                  查询订单信息,关联查询创建订单的用户信息.      查询语句:              SELECT orders.*, USER .username ,USER .sex, USER .address FROM orders, USER WHERE orders.user_id = USER .id        查询结果:     1,使用resultType接受输出结果                  用户信息:         

js正则表达式中test,exec,match方法的区别介绍

 本篇文章主要是对js正则表达式中test,exec,match方法的区别进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 js正则表达式中test,exec,match方法的区别说明   test  test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c"; var reg = new RegExp("1.", ""); alert(reg.test(str)); // true     e

Java中for、while、do while三种循环语句的区别介绍_java

本文通过实例讲解给大家介绍Java中for.while.do while三种循环语句的区别,具体详情如下所示: 第一种:for循环 循环结构for语句的格式: for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } eg: class Dome_For2{ public static void main(String[] args) { //System.out.println("Hello World!"); //求1-10的偶数的和 int sum = 0; fo

Java线程池的几种实现方法和区别介绍_java

Java线程池的几种实现方法和区别介绍 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.E