需求:Mybatis查询SQL将Double保留小数点后两位
实现思路:首先创建一个自定义类继承org.apache.ibatis.type.DoubleTypeHandler类,重写方法,实现数据类型转换,逻辑处理,这里只做保留2位小数。(PS:当然大家想转换其他类型,也是同样的方法,继承org.apache.ibatis.type下的对应的类即可,方法大同小异)
1.自定义TypeHander继承DoubleTypeHandler。
public class TypeHander extends DoubleTypeHandler{
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Double parameter, JdbcType jdbcType)
throws SQLException {
ps.setDouble(i, parameter);
}
@Override
public Double getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return toDoubleTwoFormat(rs.getDouble(columnName));
}
@Override
public Double getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return toDoubleTwoFormat(rs.getDouble(columnIndex));
}
@Override
public Double getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return toDoubleTwoFormat(cs.getDouble(columnIndex));
}
private Double toDoubleTwoFormat(double d) {
DecimalFormat decimalFormat=new DecimalFormat(".00");
return Double.parseDouble(decimalFormat.format(d));
}
}
2.mybatis配置文件中引入自定义TypeHander 类,其中taxAmount、notTaxAmount、invoiceAmountSum字段设置了小数点后保留2位。
<mapper namespace="com.richwit.invoice.mapper.InvInvoiceReceiptMapper">
<resultMap type="InvInvoiceReceipt" id="InvInvoiceReceiptResult">
<result property="invoiceId" column="invoiceId" />
<result property="invoiceState" column="invoiceState" />
<result property="invoiceType" column="invoiceType" />
<result property="buyName" column="buyName" />
<result property="buyTaxNo" column="buyTaxNo" />
<result property="buyAccountBankNo" column="buyAccountBankNo" />
<result property="buyAddTel" column="buyAddTel" />
<result property="sellTaxNo" column="sellTaxNo" />
<result property="sellName" column="sellName" />
<result property="sellAccountBankNo" column="sellAccountBankNo" />
<result property="sellAddTel" column="sellAddTel" />
<result property="taxRate" column="taxRate" />
<result property="taxAmount" column="taxAmount" typeHandler="com.richwit.invoice.util.TypeHander" />
<result property="notTaxAmount" column="notTaxAmount" typeHandler="com.richwit.invoice.util.TypeHander" />
<result property="invoiceAmountSum" column="invoiceAmountSum" typeHandler="com.richwit.invoice.util.TypeHander" />
</resultMap>
<sql id="selectInvInvoiceReceipt">
select invoiceId,invoiceState,invoiceType,buyName,buyTaxNo,buyAccountBankNo,buyAddTel,
sellTaxNo,sellName,sellAccountBankNo,sellAddTel,taxRate,taxAmount,notTaxAmount,
invoiceAmountSum
from inv_invoicereceipt
</sql>
<select id="queryInvoiceReceiptList" parameterType="Map" resultMap="InvInvoiceReceiptResult">
<include refid="selectInvInvoiceReceipt"/>
<where>
<if test="startTime != null and startTime != ''">
<![CDATA[ and DATE_FORMAT(settTime, '%Y-%m-%d')>= DATE_FORMAT(#{startTime}, '%Y-%m-%d') ]]>
</if>
<if test="endTime != null and endTime != ''">
<![CDATA[ and DATE_FORMAT(settTime, '%Y-%m-%d') <= DATE_FORMAT(#{endTime}, '%Y-%m-%d') ]]>
</if>
<if test="gsbm != null and gsbm != ''"> and gsbm = #{gsbm}</if>
</where>
</select>
</mapper>
这样就已经实现了double保留2位小数的需求。