在求和中,我们使用mysql的sum函数。
java代码
<select id="getShieldHitCountLate7Day" resultType="com.webank.ims.operation.bean.ComPolicyHitCountBean"> SELECT policy_id policyId, sum(hit_count) count FROM com_shield_policy_hit_count WHERE date > #{date} group by policy_id </select>
public class ComPolicyHitCountBean implements Serializable { private static final long serialVersionUID = 2405225637233164110L; private Integer policyId; private Integer count = 0; public Integer getPolicyId() { return policyId; } public void setPolicyId(Integer policyId) { this.policyId = policyId; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } }
可以看到count的类型为Integer.运行程序报错
mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.webank.ims.operation.bean.ComPolicyHitCountBean matching [java.lang.Integer, java.math.BigDecimal] at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75) at org.mybatis.spring.SqlSessionTemplate$ SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)需要实现错误信息中的构造函数。才可以
public ComPolicyHitCountBean(Integer policyId, BigDecimal count) { super(); this.policyId = policyId; this.count = count.intValue(); }
其实是因为mysql的sum函数返回的类型的问题。
The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE). (Before MySQL 5.0.3, SUM() and AVG() return DOUBLE for all numeric arguments.)
参考:http://bylijinnan.iteye.com/blog/1984472