Mybatis中resultType与resultMap的区别

编程

概述

resultType和resultMap功能类似 ,都是返回对象信息 ,但是resultMap要更强大一些 ,可自定义。因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名和你的实体类的属性名不一样也没关系,都会给你映射出来,但是,resultType就比较鸡肋了,必须字段名一样,比如说 dataId和data_id 这种的都不能映射。

错误案例

XML文件实现:

<sql id="selectDataInfoByIdField">

${alias}.DATA_ID,${alias}.NAME,${alias}.SH_IMG,${alias}.TYP,${alias}.INDUSTRY,${alias}.RANGE,${alias}.OWNERSHIP,${alias}.TRADING,${alias}.LABELS,${alias}.LINK_MAN,${alias}.LINK_PHONE,${alias}.PRICE,${alias}.HAS_SPEC,${alias}.SPEC,${alias}.WDESC,${alias}.PUB_TIME,${alias}.COMMENT_SCORE,${alias}.SALED_NUM,${alias}.VIEW_NUM,${alias}.FAVORITY_NUM,${alias}.WSTATE,${alias}.MBR_ID,${alias}.DEFAULT_WI

</sql>

<select id="selectDataInfoById" resultType="com.bjbde.trade.model.PsmDataBase">

select

<include refid="selectDataInfoByIdField">

<property name="alias" value="t1"></property>

</include>

from PSM_DATA_BASE t1

where t1.DATA_ID = #{dataId}

</select>

此处使用了resultType,指向的实体类如下:

package com.bjbde.trade.model;

import java.math.BigDecimal;

import java.util.Date;

import javax.persistence.*;

@Table(name = "PSM_DATA_BASE")

public class PsmDataBase {

@Id

@Column(name = "DATA_ID")

private String dataId;

@Column(name = "NAME")

private String name;

@Column(name = "SH_IMG")

private String shImg;

@Column(name = "TYP")

private KeyValDTO typ;

/*@Column(name = "TYP")

private KeyValDTO productTyp;*/

@Column(name = "INDUSTRY")

private KeyValDTO industry;

@Column(name = "RANGE")

private KeyValListDTO range;

@Column(name = "OWNERSHIP")

private KeyValListDTO ownership;

@Column(name = "TRADING")

private KeyValDTO trading;

@Column(name = "LABELS")

private String labels;

@Column(name = "LINK_MAN")

private String linkMan;

@Column(name = "LINK_PHONE")

private String linkPhone;

@Column(name = "PRICE")

private BigDecimal price;

@Column(name = "HAS_SPEC")

private String hasSpec;

@Column(name = "SPEC")

private String spec;

@Column(name = "WDESC")

private String wdesc;

@Column(name = "PUB_TIME")

private Date pubTime;

@Column(name = "COMMENT_SCORE")

private BigDecimal commentScore;

@Column(name = "SALED_NUM")

private BigDecimal saledNum;

@Column(name = "VIEW_NUM")

private BigDecimal viewNum;

@Column(name = "FAVORITY_NUM")

private BigDecimal favorityNum;

@Column(name = "WSTATE")

private String wstate;

@Column(name = "MBR_ID")

private String mbrId;

@Column(name = "DEFAULT_WI")

private BigDecimal defaultWi;

/**

* @return DATA_ID

*/

public String getDataId() {

return dataId;

}

/**

* @param dataId

*/

public void setDataId(String dataId) {

this.dataId = dataId;

}

/**

* @return NAME

*/

public String getName() {

return name;

}

/**

* @param name

*/

public void setName(String name) {

this.name = name;

}

/**

* @return SH_IMG

*/

public String getShImg() {

return shImg;

}

/**

* @param shImg

*/

public void setShImg(String shImg) {

this.shImg = shImg;

}

public KeyValDTO getIndustry() {

return industry;

}

public void setIndustry(KeyValDTO industry) {

this.industry = industry;

}

public KeyValDTO getTyp() {

return typ;

}

public void setTyp(KeyValDTO typ) {

this.typ = typ;

}

public KeyValListDTO getRange() {

return range;

}

public void setRange(KeyValListDTO range) {

this.range = range;

}

public KeyValListDTO getOwnership() {

return ownership;

}

public void setOwnership(KeyValListDTO ownership) {

this.ownership = ownership;

}

public KeyValDTO getTrading() {

return trading;

}

public void setTrading(KeyValDTO trading) {

this.trading = trading;

}

/**

* @return LABELS

*/

public String getLabels() {

return labels;

}

/**

* @param labels

*/

public void setLabels(String labels) {

this.labels = labels;

}

/**

* @return LINK_MAN

*/

public String getLinkMan() {

return linkMan;

}

/**

* @param linkMan

*/

public void setLinkMan(String linkMan) {

this.linkMan = linkMan;

}

/**

* @return LINK_PHONE

*/

public String getLinkPhone() {

return linkPhone;

}

/**

* @param linkPhone

*/

public void setLinkPhone(String linkPhone) {

this.linkPhone = linkPhone;

}

/**

* @return PRICE

*/

public BigDecimal getPrice() {

return price;

}

/**

* @param price

*/

public void setPrice(BigDecimal price) {

this.price = price;

}

/**

* @return HAS_SPEC

*/

public String getHasSpec() {

return hasSpec;

}

/**

* @param hasSpec

*/

public void setHasSpec(String hasSpec) {

this.hasSpec = hasSpec;

}

/**

* @return SPEC

*/

public String getSpec() {

return spec;

}

/**

* @param spec

*/

public void setSpec(String spec) {

this.spec = spec;

}

/**

* @return WDESC

*/

public String getWdesc() {

return wdesc;

}

/**

* @param wdesc

*/

public void setWdesc(String wdesc) {

this.wdesc = wdesc;

}

/**

* @return PUB_TIME

*/

public Date getPubTime() {

return pubTime;

}

/**

* @param pubTime

*/

public void setPubTime(Date pubTime) {

this.pubTime = pubTime;

}

/**

* @return COMMENT_SCORE

*/

public BigDecimal getCommentScore() {

return commentScore;

}

/**

* @param commentScore

*/

public void setCommentScore(BigDecimal commentScore) {

this.commentScore = commentScore;

}

/**

* @return SALED_NUM

*/

public BigDecimal getSaledNum() {

return saledNum;

}

/**

* @param saledNum

*/

public void setSaledNum(BigDecimal saledNum) {

this.saledNum = saledNum;

}

/**

* @return VIEW_NUM

*/

public BigDecimal getViewNum() {

return viewNum;

}

/**

* @param viewNum

*/

public void setViewNum(BigDecimal viewNum) {

this.viewNum = viewNum;

}

/**

* @return FAVORITY_NUM

*/

public BigDecimal getFavorityNum() {

return favorityNum;

}

/**

* @param favorityNum

*/

public void setFavorityNum(BigDecimal favorityNum) {

this.favorityNum = favorityNum;

}

/**

* @return WSTATE

*/

public String getWstate() {

return wstate;

}

/**

* @param wstate

*/

public void setWstate(String wstate) {

this.wstate = wstate;

}

/**

* @return MBR_ID

*/

public String getMbrId() {

return mbrId;

}

/**

* @param mbrId

*/

public void setMbrId(String mbrId) {

this.mbrId = mbrId;

}

/**

* @return DEFAULT_WI

*/

public BigDecimal getDefaultWi() {

return defaultWi;

}

/**

* @param defaultWi

*/

public void setDefaultWi(BigDecimal defaultWi) {

this.defaultWi = defaultWi;

}

/* public KeyValDTO getProductTyp() {

return productTyp;

}

public void setProductTyp(KeyValDTO productTyp) {

this.productTyp = productTyp;

}*/

}

此处尽管字段上加了@Column注解,但是由于使用了resultType,所得的DATA_ID及其他中间带有”_“的字段依然不能匹配。

需要使用ResultMap,使用后可以正常显示数据。

以上是 Mybatis中resultType与resultMap的区别 的全部内容, 来源链接: utcz.com/z/511780.html

回到顶部