MyBatis中關於resultType和resultMap的差別引見。本站提示廣大學習愛好者:(MyBatis中關於resultType和resultMap的差別引見)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis中關於resultType和resultMap的差別引見正文
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 ><span > </span>//應用resultType的情形 Goods goods = (Goods)session.selectOne("clark.selectGoodById", 4); System.out.println(goods.toString());</span> [html] view plain copy 在CODE上檢查代碼片派生到我的代碼片 <span ><span > </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 >[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---應用resultType的成果</span> <span >-------應用resultMap的成果-----------------</span>
以上所述是小編給年夜家引見的MyBatis中關於resultType和resultMap的差別引見,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!