我们的项目中,有关于金额的计算,所以,一般在java环境中我们使用bigdecimal来做运算和存储金额信息。数据库sqlServer2008用的float类型

问题是,当我将金额赋值成0时,很意外的发现数据库存储的是null. 我的持久层框架用的mybatis。

在查阅了一翻资料后发现,原来是我在判断金额类型时,一个不规范的错误导致的,直接上代码。

--有问题代码 PS我的maypper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.travesky.bluesky.dao.tkt.OrdTktDataDao">
<!-- 对于sqlserver数据库浮点型数据如果传入null则会抛出字符串转数值异常,下面通过判断来解决 -->
<!-- <if test="comm ==null or comm==''"> -->
<!-- null, -->
<!-- </if> -->
<!-- <if test="comm !=null and comm!=''"> -->
<!-- #{comm}, -->
<!-- </if> -->
<!-- 插入数据 -->
<insert >
#{comm},
</if>
<!-- comm end -->

)
</insert>
</mapper>

-----------就是因为我做了一个空串的判断,导致mybatis会按照字符串来解析属性。这就尴尬了。首先既然是封装数据类型,不可能有空串的可能性,所以这里应该只判断是够为null就可以了。

----正确写法

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.travesky.bluesky.dao.tkt.OrdTktDataDao"> 

<!-- 插入数据 --> 
<insert >
#{comm},
</if>
<!-- comm end -->

)
</insert>
</mapper>

 

具体的详细问题分析,大家可以参考http://blog.csdn.net/qing_gee/article/details/50518795

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-05-23
  • 2021-07-06
  • 2021-11-15
  • 2022-12-23
猜你喜欢
  • 2021-06-05
  • 2021-06-29
  • 2022-12-23
  • 2021-08-30
  • 2022-01-23
  • 2021-04-24
相关资源
相似解决方案