jdbctype,oracle resulttype,MyBatis中關于resultType和resultMap的區別介紹

 2023-11-19 阅读 35 评论 0

摘要:MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的(對應著我們的model對象中的實體),而resultMap則是對外部ResultMap的引用(提前定義了db和model之間的隱射key-->value關系)&

MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的(對應著我們的model對象中的實體),而resultMap則是對外部ResultMap的引用(提前定義了db和model之間的隱射key-->value關系),但是resultType跟resultMap不能同時存在。

jdbctype、在MyBatis進行查詢映射時,其實查詢出來的每一個屬性都是放在一個對應的Map里面的,其中鍵是屬性名,值則是其對應的值。

①當提供的返回類型屬性是resultType時,MyBatis會將Map里面的鍵值對取出賦給resultType所指定的對象對應的屬性。所以其實MyBatis的每一個查詢映射的返回類型都是ResultMap,只是當提供的返回類型屬性是resultType的時候,MyBatis對自動的給把對應的值賦給resultType所指定對象的屬性。

Mybatis?②當提供的返回類型是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()+"]";

}

}

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select id,cate_id,name,price,description,order_no,update_time

from goods where id = #{id}

select id,cate_id,name,price,description,order_no,update_time from goods

insert into goods(id,cate_id,name,price,description,order_no,update_time)

values(#{id},#{cateId},#{name},#{price},#{description},#{orderNo},#{updateTime})

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();

//使用resultType的情況

Goods goods = (Goods)session.selectOne("clark.selectGoodById", 4);

System.out.println(goods.toString());

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

//使用resultMap的情況

List 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();

}

}

}

結果輸出為:

[goods include:Id=4,name=clark,orderNo=null,cateId=null,updateTime=null]---使用resultType的結果

-------使用resultMap的結果-----------------

以上所述是小編給大家介紹的MyBatis中關于resultType和resultMap的區別介紹,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/185019.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息