Mybatis查询SQL将Double保留小数点后两位

需求: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位小数的需求。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值