MyBatis about Resulttype and Resultmap differences

Source: Internet
Author: User

MyBatis about Resulttype and Resultmap differencesIn MyBatis, the return type can be used with resulttype or RESULTMAP when the query is in a select map. Resulttype is a direct representation of the return type (corresponding to the entity in our model object), whereas Resultmap is a reference to an external resultmap (pre-defined insinuate key-->value relationship between DB and model). But Resulttype and Resultmap cannot exist at the same time.


  When the query map is mybatis. In fact, every property that is queried is placed in a corresponding map. The key is the property name, and the value is its corresponding value.
  ① when the provided return type property is Resulttype, MyBatis takes the key-value pair out of the map and assigns the corresponding property to the object specified by Resulttype. So in fact, the return type of each query map of MyBatis is Resultmap, just when the return type property provided is Resulttype, MyBatis gives itself the property of assigning the corresponding value to the object specified by Resulttype.
  ② when the return type provided is RESULTMAP, since map does not represent the domain model very well, it is necessary to further convert it to the corresponding object, which is often very useful in complex queries.   The following gives a sample explanation of the difference in use:

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 updatetim e) {super (); this.id = Id;this.cateid = Cateid;this.name = Name;this.price = Price;this.description = Description;this.ord Erno = 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;} @Overridepublic 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 and Resulttype use differences--><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.xm L "; 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) Session.selectone (" Clark.selectgoodbyid ", 4); System.out.println (Goods.tostring ());</span>
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>//use resultmap list<goods> 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]---Results of using resulttype </span>
<span style= "color: #33ff33;" >-------Results of using Resultmap-----------------</span>
[Goods include:id=4,name=clark,orderno=5,cateid=12,updatetime=wed Sep 15:29:58 CST 2014][goods include:Id=1, name= Nokia n85,orderno=1,cateid=1,updatetime=wed Sep 13:52:51 CST 2014]
[Goods include:id=2,name= a30,orderno=2,cateid=1,updatetime=wed Sep 13:53:11 CST 2014] [Goods include:id=3,name= a30,orderno=3,cateid=2,updatetime=wed Sep 15:07:38 CST]



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

MyBatis about Resulttype and Resultmap differences

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.