iteye_19889 2014-07-18 10:12
浏览 3460
已采纳

使用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>
  • 写回答

3条回答 默认 最新

  • iteye_11009 2014-07-18 12:29
    关注

    自定义一个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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?
  • ¥15 关于CPLEX的问题,请专家解答