MyBatis on Resulttype and Resultmap in the paper _java

Source: Internet
Author: User

In MyBatis, the return type can be resulttype in a query for select mapping, or it can be resultmap,resulttype directly representing the return type (corresponding to the entity in our model object). Resultmap is a reference to the external resultmap (the insinuate key-->value relationship between DB and model is defined in advance), but Resulttype and Resultmap cannot exist at the same time.

In MyBatis query mapping, in fact, every query out of the attributes are placed in a corresponding map, where the key is the property name, the value is its corresponding value.

① when the provided return type property is Resulttype, MyBatis assigns the key value pairs in the map to the properties corresponding to the object specified by Resulttype. So in fact, the return type of each query map of MyBatis is Resultmap, but when the return type attribute provided is Resulttype, MyBatis automatically assigns the corresponding value to resulttype the property of the object specified.

② when the return type provided is Resultmap, because the map does not represent the domain model well, it needs to be further transformed into the corresponding object, which is often useful in complex queries.

Here is an example to illustrate the difference between the two uses:

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 order 
No, 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; 
The public void SetId (Integer id) {this.id = ID; 
Public Integer Getcateid () {return cateid; 
The 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; 
The public void Setorderno (Integer orderNo) {this.orderno = OrderNo; 
Public Date Gettimestamp () {return updatetime; 
The public void Settimestamp (Date updatetime) {this.updatetime = UpdateTime; @Override public String toString () {return "[Goods include:id=" +this.getid () + ", name=" +this.getname () + ", orderno=" +t 
His.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" > & Lt;configuration> <typeAliases> <!--give a alias for model--> <typealias alias= "goods" type= "com.cl" Ark.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 re  Source= "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 C olumn= "Price" property= "Price"/> <result column= "description" property= "description"/> <result "column=" Order_no "property=" OrderNo "/> <result column=" update_time "property=" UpdateTime "/> </resultMap> < !--Resultmap and Resulttype using the distinction--> <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= "Selec Tallgoods "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>//use resulttype goods 
= (goods) Goods ( "Clark.selectgoodbyid", 4); 
System.out.println (Goods.tostring ());</span> 
[HTML] View plain copy on the code to view the snippet derived from my Code slice
<span Style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" > </span>//use resultmap 
GS = Session.selectlist ("Clark.selectallgoods"); 
for (goods goods2:gs) { 
System.out.println (goods2.tostring ()); 
} 
Goods goods = new Goods (4, "Clark", 12.30, "Test is OK", 5, New Date ()); 
Session.insert ("Clark.insertgood", goods); 
Session.commit (); 
} catch (IOException e) { 
e.printstacktrace (); 
} 
} 

The result output is:

<span style= "color: #cc0000;" >[goods Include:id=4,name=clark,orderno=null,cateid=null,updatetime=null]---using the results of Resulttype </span> 
<span style= "color: #33ff33;" >-------The results of using Resultmap-----------------</span>

The above is a small set to introduce the MyBatis in the Resulttype and resultmap of the difference, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.