mybatis报错:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.lang.NumberFormatException: For input string: "{4=null}"
### The error may exist in com/mapper/NewsMapper.xml
### The error may involve com.mapper.NewsMapper.insertTypeWithNewsIdAndTypeId
### The error occurred while executing an update
### Cause: java.lang.NumberFormatException: For input string: "{4=null}"
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:59)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
at com.sun.proxy.$Proxy0.insertTypeWithNewsIdAndTypeId(Unknown Source)
at com.ui.Test.main(Test.java:148)
Caused by: java.lang.NumberFormatException: For input string: "{4=null}"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at org.apache.ibatis.ognl.OgnlOps.doubleValue(OgnlOps.java:243)
at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:100)
at org.apache.ibatis.ognl.OgnlOps.isEqual(OgnlOps.java:143)
at org.apache.ibatis.ognl.OgnlOps.equal(OgnlOps.java:802)
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:53)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:493)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:457)
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:293)
at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:563)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
... 5 more
主要原因是代码中参数是int类型,mybatis框架中#号为了防止sql注入,对所有的数据都加上了双引号,所以在int类型是 直接不用写#{}
错误实例:
<insert id="insertTypeWithNewsIdAndTypeId">
insert into news_type(news_id <if test="#{arg1}!=null and #{arg1}!=0">,type_id</if>)
values(#{arg0} <if test="#{arg1}!=null and #{arg1}!=0">,#{arg1} </if>)
</insert>
正确改法:
<insert id="insertTypeWithNewsIdAndTypeId">
insert into news_type(news_id <if test="#{arg1}!=null and arg1!=0">,type_id</if>)
values(#{arg0} <if test="#{arg1}!=null and arg1!=0">,#{arg1} </if>)
</insert>