When select mapping in MyBatis, the return type can be used with resultType or resultMap. resultType directly represents the return type (corresponding to the entity in our model object), while resultMap is a reference to the external ResultMap (the hidden key-->value relationship between db and model is defined in advance), but resultType and resultMap cannot exist at the same time.
When MyBatis is querying mapping, in fact, each attribute query is placed in a corresponding map, where the key is the attribute name and the value is its corresponding value.
① When the provided return type attribute is resultType, MyBatis will take out the key value pairs in the map and assign them to the corresponding attributes of the object specified by resultType. So in fact, the return type of each query map of MyBatis is ResultMap. However, when the provided return type attribute is resultType, MyBatis automatically assigns the corresponding value to the attributes of the object specified by resultType.
② When the provided return type is resultMap, because the Map cannot represent the domain model well, it needs to further convert it into the corresponding object itself, which is often very useful in complex queries.
Here is an example to illustrate the difference between the two:
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> <!--The difference between resultMap and 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>//Scenarios using resultType Goods = (Goods)session.selectOne("clark.selectGoodById", 4); System.out.println(goods.toString());</span> [html] view plain copy View code fragments derived from my code fragments on CODE <span style="font-size:18px;"><span style="white-space:pre"> </span>//Scenarios using 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(); } } }The result output is:
<span style="color:#cc0000;">[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---Result using resultType</span> <span style="color:#33ff33;">---Result using resultMap------------------</span>
The above is an introduction to the difference between resultType and resultMap in MyBatis introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!