欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 18:14:57
mybatis中在查询进行select映射的时候,返回类型可以用resulttype,也可以用resultmap,resulttype是直接表示返回类型的(对应着我们的mo...

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的区别介绍,希望对大家有所帮助