使用Mybatis框架,如何使查询结果float数据保留两位小数

    <id column="id" property="id" />
    <result column="sku_code" property="skuCode" />
    <result column="sku_price" property="skuPrice" />
    <result column="ori_price" property="oriPrice" />
    <result column="sku_select_value" property="skuSelectValue" />
    <result column="quantity" property="quantity" />
    <result column="buy_limit" property="buyLimit" />
    <result column="img" property="img" />
    <result column="product_id" property="productId" />
     <result column="modifyTime" property="modify_time" />
</resultMap>


<sql id="ProductSkuInfo">
    SELECT 
        product_id,
        id,
        sku_code,
        sku_price,
        ori_price,
        sku_select_value,
        quantity,
        img,
        buy_limit,
        modify_time
    from t_product_sku

</sql>

自定义一个TypeHander继承FloatTypeHandler,例子
public class TwoDecimalFloatTypeHander extends FloatTypeHandler{

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, Float parameter, JdbcType jdbcType)
      throws SQLException {
    ps.setFloat(i, parameter);
  }

  @Override
  public Float getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
    return NumberTool.keepTwoDecimalFloat(rs.getFloat(columnName));
  }

  @Override
  public Float getNullableResult(ResultSet rs, int columnIndex)
      throws SQLException {
    return NumberTool.keepTwoDecimalFloat(rs.getFloat(columnIndex));
  }

  @Override
  public Float getNullableResult(CallableStatement cs, int columnIndex)
      throws SQLException {
    return NumberTool.keepTwoDecimalFloat(cs.getFloat(columnIndex));
  }

}
/**
* Float 保留两位小数
* @return
*/
public static Float keepTwoDecimalFloat(Float f) {
DecimalFormat decimalFormat=new DecimalFormat(".00");
return Float.parseFloat(decimalFormat.format(f));
}

然后在mybatis配置中引入自定义的handler处理器,例:












    <collection property="orderItems" ofType="com.ldlife.accountserver.user.domain.OrderItem" column="orderId">  
        <id column="itemid" property="itemid"/>  
        <result column="order_id" property="orderId"/>
        <result column="seller_id" property="sellerId"/>  
        <result column="productSku_id" property="productSkuId"/>  
        <result column="sku_code" property="skuCode"/>  
        <result column="amount" property="amount"/> 
        <result column="create_date" property="createDate"/>
        <result column="ori_price" property="oriPrice" typeHandler="com.ldlife.accountserver.base.mybatis.typehander.TwoDecimalFloatTypeHander"/>
        <result column="price" property="price" typeHandler="com.ldlife.accountserver.base.mybatis.typehander.TwoDecimalFloatTypeHander"/> 
    </collection> 
</resultMap>
<sql id="orderInfo">
    SELECT
        o.id,
        o.deliver_fee,
        o.note,
        o.payment_way,
        o.product_total_price as productTotal_price,
        o.order_state,
        o.total_price,
        o.buyer_id,
        o.deliver_id,
        o.receipt_date,
        o.receipt_time,
        o.create_date,
        oi.itemid,
        oi.order_id,
        oi.seller_id,
        oi.product_sku_id as productSku_id,
        oi.sku_code,
        oi.amount,
        oi.ori_price,
        oi.price
    FROM
        t_order o 
        left join t_order_item oi
        on o.id = oi.order_id
        where 1=1
<l>

根据以往的分析,此处可以做如下分析:
1、在查询的时候,将该字段修改成to_char(a,'fm9999999999999.99'),最简单的一种
2、也可以在ibatis查询返回组装结果的时候对返回的对象的set方法进行处理
3、如果觉得2可能破坏了set的封装性,可以在对该值进行处理的时候进行转换

可以的 要定义typehandler






[code="java"]
public class OracleFloatTypeHandler implements TypeHandler {

@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
log.debug("setParameter called"); <================ BREAKPOINT HERE
}

@Override
public String getResult(ResultSet rs, String columnName)
throws SQLException {
log.debug("getResult 2 called"); <================ BREAKPOINT HERE
return "";
}

@Override
public String getResult(ResultSet rs, int columnIndex)
throws SQLException {
log.debug("getResult 2 called"); <================ BREAKPOINT HERE
return "";
}

@Override
public String getResult(CallableStatement cs, int columnIndex)
throws SQLException {
log.debug("getResult 3 called"); <================ BREAKPOINT HERE
return "";
}
}
[/code]