1.mybatils文件
<result column="appflymoney" property="appflymoney" jdbcType="DOUBLE" javaType="double" typeHandler="com.ydtx.comm.TwoDecimalFloatTypeHander"/>
<result column="paymeney" property="paymeney" jdbcType="DOUBLE" javaType="double" typeHandler="com.ydtx.comm.TwoDecimalFloatTypeHander"/>
2.工具类
package com.ydtx.comm;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.DoubleTypeHandler;
import org.apache.ibatis.type.FloatTypeHandler;
import org.apache.ibatis.type.JdbcType;
public class TwoDecimalFloatTypeHander extends DoubleTypeHandler{
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 NumberTool.keepTwoDecimalFloat(rs.getDouble(columnName));
}
@Override
public Double getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return NumberTool.keepTwoDecimalFloat(rs.getDouble(columnIndex));
}
@Override
public Double getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return NumberTool.keepTwoDecimalFloat(cs.getDouble(columnIndex));
}
}
3.工具类
package com.ydtx.comm;
import java.text.DecimalFormat;
public class NumberTool {
/**
* Float 保留两位小数
* @return
*/
public static Double keepTwoDecimalFloat(double d) {
DecimalFormat decimalFormat=new DecimalFormat(".00");
// System.out.println(decimalFormat.format(d));
return Double.parseDouble(decimalFormat.format(d));
}
/**
* 保留四位小数
* @param d
* @return
*/
public static Double keepFourDecimalFloat(double d) {
DecimalFormat decimalFormat=new DecimalFormat("#.####");
// System.out.println(decimalFormat.format(d));
return Double.parseDouble(decimalFormat.format(d));
}
}