异常描述:
The error occurred in sqlmap/empdailyreport.xml.
--- The error occurred while applying a result map.
--- Check the EmpDailyReport.ReportResult.
--- The error happened while setting a property on the result object.
--- Cause: net.sf.cglib.beans.BulkBeanException
resultMap定义:
<resultMap id="ReportResult" class="com.jd.sns.chat.common.domain.empdailyreport.ReportResult">
<result column="pin" jdbcType="VARCHAR" property="pin" />
<result column="count" jdbcType="INTEGER" property="count" />
<result column="orgId" jdbcType="INTEGER" property="orgId" />
</resultMap>
ReportResult定义:
package com.jd.sns.chat.common.domain.empdailyreport;
/**
* 查询统计返回辅助类
* @author cdlitao1
* @2012-9-27
*/
public class ReportResult {
/**
* 客服pin
*/
private String pin;
/**
* 统计值
*/
private int count=0;
/**
* 该统计数据所属pin所在的技能组id
* @return
*/
private int orgId;
public int getOrgId() {
return orgId;
}
public void setOrgId(int orgId) {
this.orgId = orgId;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
sql查询语句:
<select id="findReceptionNo" parameterClass="ReportProfile" resultMap="ReportResult">
<![CDATA[
SELECT service AS pin,COUNT(1) AS count,orgId
FROM analysis_session
WHERE created>=#startTime:VARCHAR# AND created<#endTime:VARCHAR#
AND responseTime IS NOT NULL
GROUP BY service
]]>
</select>
异常原因:
eg 1:sql查询语中返回的字段有NULL值,当映射给int等基本类型时就会出错
解决方案1:resultMap定义映射关系时进行修改-----<result column="orgId" jdbcType="INTEGER" property="orgId" nullvalue="0"/>
方案分析:增加的nullvalue="0"的作用是将字段orgId的返回值中的null替换为0,避免了null与int型属性的映射
eg 2:NULL映射给封装类型Integer时不会出错!!!
注:Integer in = null;对(此in变量实际上也是个java对象)
int in = null;错!(此in实际仅是java类中的一个属性)
解决方案2:将resultMap映射的java类ReportResult中的属性都定义为封装类,此例中将orgId的类型由int改为Integer,异常解决
方案分析:null值与封装类可以进行映射,而int属于java基本类型,与null映射会出错!
建议:在使用resultMap进行返回值映射时,建议将对应的java类中的属性的类型定义为封装类型,避免此异常
涛小菜20121012
[email protected]