我是靠谱客的博主 外向钢笔,最近开发中收集的这篇文章主要介绍Mybatis自定义ResultMap使用总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前段时间因为需求变更,使用Mybatis写SQL的时候需要自定义ResultMap,在使用的过程中也遇到了很多奇怪的问题,搞不懂是什么原因,各路大神有知道的还请指导指导奋斗


在同一个映射文件里,我的第一个ResultMap对应的是一个DAO,是这么写的:

<resultMap id="BaseResultMap" type="com.tracy.gd.domain.Expense" >
<id column="e_id" property="eId" jdbcType="INTEGER" />
<result column="e_la_id" property="eLaId" jdbcType="INTEGER" />
<result column="e_la_cpt_id" property="eLaCptId" jdbcType="INTEGER" />
<result column="e_la_user_id" property="eLaUserId" jdbcType="INTEGER" />
<result column="e_lend_time" property="eLendTime" jdbcType="TIMESTAMP" />
<result column="e_sreturn_time" property="eSreturnTime" jdbcType="TIMESTAMP" />
<result column="e_areturn_time" property="eAreturnTime" jdbcType="TIMESTAMP" />
<result column="e_is_returned" property="eIsReturned" jdbcType="VARCHAR" />
<result column="e_days" property="eDays" jdbcType="INTEGER" />
<result column="e_expense" property="eExpense" jdbcType="DECIMAL" />
<result column="attribute1" property="attribute1" jdbcType="VARCHAR" />
<result column="attribute2" property="attribute2" jdbcType="VARCHAR" />
<result column="attribute3" property="attribute3" jdbcType="VARCHAR" />
</resultMap>


当我需要从前端传送过来的数据格式和这个不同的时候,我定义了一个dto,我没写ResultMap,直接写ReturnType = 那个dto:


public class Expense {
private Integer eId;
private Integer eLaId;
private Integer eLaCptId;
private Integer eLaUserId;
private Date eLendTime;
private Date eSreturnTime;
private Date eAreturnTime;
private String eIsReturned;
private Integer eDays;
private BigDecimal eExpense;
private String attribute1;
private String attribute2;
private String attribute3;
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public Integer geteLaId() {
return eLaId;
}
public void seteLaId(Integer eLaId) {
this.eLaId = eLaId;
}
public Integer geteLaCptId() {
return eLaCptId;
}
public void seteLaCptId(Integer eLaCptId) {
this.eLaCptId = eLaCptId;
}
public Integer geteLaUserId() {
return eLaUserId;
}
public void seteLaUserId(Integer eLaUserId) {
this.eLaUserId = eLaUserId;
}
public Date geteLendTime() {
return eLendTime;
}
public void seteLendTime(Date eLendTime) {
this.eLendTime = eLendTime;
}
public Date geteSreturnTime() {
return eSreturnTime;
}
public void seteSreturnTime(Date eSreturnTime) {
this.eSreturnTime = eSreturnTime;
}
public Date geteAreturnTime() {
return eAreturnTime;
}
public void seteAreturnTime(Date eAreturnTime) {
this.eAreturnTime = eAreturnTime;
}
public String geteIsReturned() {
return eIsReturned;
}
public void seteIsReturned(String eIsReturned) {
this.eIsReturned = eIsReturned == null ? null : eIsReturned.trim();
}
public Integer geteDays() {
return eDays;
}
public void seteDays(Integer eDays) {
this.eDays = eDays;
}
public BigDecimal geteExpense() {
return eExpense;
}
public void seteExpense(BigDecimal eExpense) {
this.eExpense = eExpense;
}
public String getAttribute1() {
return attribute1;
}
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1 == null ? null : attribute1.trim();
}
public String getAttribute2() {
return attribute2;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2 == null ? null : attribute2.trim();
}
public String getAttribute3() {
return attribute3;
}
public void setAttribute3(String attribute3) {
this.attribute3 = attribute3 == null ? null : attribute3.trim();
}
}


以下是修改过的dto;和上面说的不是同一个,修改了名字,具体请看下面:

/**
* Created by trcay on 2017/12/24.
* 缴费管理页面响应的dto
*/
public class updateExpense {
private Integer laId;
private String userName;
private String cptName;
private Date LendTime;
private Date AreturnTime;
private String IsReturned;
private Integer Days;
private BigDecimal Expense;
private String IsPay;
public Integer getLaId() {
return laId;
}
public void setLaId(Integer laId) {
this.laId = laId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCptName() {
return cptName;
}
public void setCptName(String cptName) {
this.cptName = cptName;
}
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
public Date getLendTime() {
return LendTime;
}
public void setLendTime(Date lendTime) {
LendTime = lendTime;
}
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
public Date getAreturnTime() {
return AreturnTime;
}
public void setAreturnTime(Date areturnTime) {
AreturnTime = areturnTime;
}
public String getIsReturned() {
return IsReturned;
}
public void setIsReturned(String isReturned) {
IsReturned = isReturned;
}
public Integer getDays() {
return Days;
}
public void setDays(Integer days) {
Days = days;
}
public BigDecimal getExpense() {
return Expense;
}
public void setExpense(BigDecimal expense) {
Expense = expense;
}
public String getIsPay() {
return IsPay;
}
public void setIsPay(String isPay) {
IsPay = isPay;
}
}


后来已经测试,发现不行,只能获得attribute1的值,也就是只有这个属性能获取,其他都是null,但是又没有报不匹配的异常,我就很奇怪,后来实在不行,我就尝试以一下重新写了一个ResultMap:

和之前的上面的ResultMap一样,只是将两个对应的不同实体的属性给改了,后来发现还是不行,我就找规律,发现两个ResultMap之间名字一样的属性获取不了,名字不一样的可以获取?


后来我把对应的ResultMap改成:


<resultMap id="ResultMap1" type="com.tracy.gd.dto.updateExpense" >
<id column="la_id" property="laId" jdbcType="INTEGER" />
<result column="e_la_id" property="eLaId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="cpt_name" property="cptName" jdbcType="VARCHAR" />
<result column="lend_time" property="LendTime" jdbcType="TIMESTAMP" />
<result column="areturn_time" property="AreturnTime" jdbcType="TIMESTAMP" />
<result column="is_returned" property="IsReturned" jdbcType="VARCHAR" />
<result column="days" property="Days" jdbcType="INTEGER" />
<result column="expense" property="Expense" jdbcType="DECIMAL" />
<result column="isPay" property="IsPay" jdbcType="VARCHAR" />
</resultMap>

就可以正常取了,它就可以正常的给我塞到updateExpense这个对象中,


 <select id="findAllExpenseRecordFilter" resultMap="ResultMap1">
SELECT
a.la_id,
u.user_name user_name,
c.cpt_name cpt_name,
e.e_is_returned is_returned,
e.e_days days,
e.e_expense expense,
e.e_lend_time lend_time,
e.e_areturn_time areturn_time,
e.attribute1 isPay
FROM
gd_lending_apply a,
gd_computers c,
gd_expense e,
gd_users u
WHERE
a.la_cpt_id = c.cpt_id
AND a.la_id = e.e_la_id
AND a.attribute1 = 'Y'
AND a.la_is_check = 'Y'
AND a.la_user_id = u.user_id
<if test="userName != null and userName != ''" >
AND u.user_id IN (
SELECT
u1.user_id
FROM
gd_users u1
WHERE
u1.user_id = u.user_id
AND u1.user_name LIKE
CONCAT('%',#{userName},'%')
)
</if>
<if test="cptName != null and cptName != ''" >
AND c.cpt_name LIKE CONCAT('%',#{cptName},'%')
</if>
<if test="isPay != null and isPay != ''" >
AND e.attribute1 = #{isPay}
</if>
LIMIT #{start},#{offset}
</select>



好像是同一个映射文件中<result>标签里名字还是属性不能冲突吧?原因我也暂时不明,立个flag,



最后

以上就是外向钢笔为你收集整理的Mybatis自定义ResultMap使用总结的全部内容,希望文章能够帮你解决Mybatis自定义ResultMap使用总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部