mybatis异常invalid comparison: java.util.Date and java.lang.String

本文解决MyBatis3.3.0版本中,使用时间类型参数与空字符串对比导致的异常问题,通过调整代码逻辑避免错误,确保数据库更新操作正常执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原因是mybatis 3.3.0中对于时间参数进行比较时的一个bug. 如果拿传入的时间类型参数与空字符串''进行对比判断则会引发异常. 所以在上面的代码中去该该判断, 只保留非空判断就正常了

更改前:

<insert id="modify" parameterType="com.ssm.mybatiseg.pojo.Provider">
        update smbms_provider
            <trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
                <if test="proCode!=null and proCode!=''">proCode=#{proCode},</if>
                <if test="proName!=null and proName!=''">proName=#{proName},</if>
                <if test="proDesc!=null and proDesc!=''">proDesc=#{proDesc},</if>
                <if test="proContact !=null and proContact!=''">proCode=#{proCode},</if>
                <if test="proCode!=null and proCode!=''">proContact=#{proContact},</if>
                <if test="proPhone!=null and proPhone!=''">proPhone=#{proPhone},</if>
                <if test="proAddress!=null and proAddress!=''">proAddress=#{proAddress},</if>
                <if test="proFax!=null and proFax!=''">proFax=#{proFax},</if>
                <if test="createdBy!=null and createdBy!=''">createdBy=#{createdBy},</if>
                <if test="creationDate!=null and creationDate!=''">creationDate=#{creationDate},</if>
                <if test="modifyBy!=null and modifyBy!=''">modifyBy=#{modifyBy},</if>
                <if test="modifyDate!=null and modifyDate!=''">modifyDate=#{modifyDate},</if>
            </trim>
    </insert>

运行结果:
org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
    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:58)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy12.modify(Unknown Source)
    at com.ssm.mybatiseg.dao.ProviderMapperTest.ProviderMapperTest.modify(ProviderMapperTest.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:39)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Iterator.forEachRemaining(Unknown Source)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.forEach(Unknown Source)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:79)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:70)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
    at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:93)
    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:470)
    at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:434)
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)
    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.TrimSqlNode.apply(TrimSqlNode.java:55)
    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:292)
    at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:40)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46)
    at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:558)
    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)
    ... 46 more

更改后:

<insert id="modify" parameterType="com.ssm.mybatiseg.pojo.Provider">
        update smbms_provider
            <trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
                <if test="proCode!=null and proCode!=''">proCode=#{proCode},</if>
                <if test="proName!=null and proName!=''">proName=#{proName},</if>
                <if test="proDesc!=null and proDesc!=''">proDesc=#{proDesc},</if>
                <if test="proContact !=null and proContact!=''">proCode=#{proCode},</if>
                <if test="proCode!=null and proCode!=''">proContact=#{proContact},</if>
                <if test="proPhone!=null and proPhone!=''">proPhone=#{proPhone},</if>
                <if test="proAddress!=null and proAddress!=''">proAddress=#{proAddress},</if>
                <if test="proFax!=null and proFax!=''">proFax=#{proFax},</if>
                <if test="createdBy!=null and createdBy!=''">createdBy=#{createdBy},</if>
                <if test="creationDate!=null ">creationDate=#{creationDate},</if>
                <if test="modifyBy!=null and modifyBy!=''">modifyBy=#{modifyBy},</if>
                <if test="modifyDate!=null ">modifyDate=#{modifyDate},</if>
            </trim>
    </insert>

运行结果:

[DEBUG] 2018-10-26 13:05:55,351 com.ssm.mybatiseg.dao.ProviderMapperTest.ProviderMapperTest - testModify=================
static factory=======================
[DEBUG] 2018-10-26 13:05:55,420 org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[DEBUG] 2018-10-26 13:05:55,537 org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[DEBUG] 2018-10-26 13:05:55,566 org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] 2018-10-26 13:05:55,566 org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] 2018-10-26 13:05:55,566 org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] 2018-10-26 13:05:55,567 org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[DEBUG] 2018-10-26 13:05:55,826 org.apache.ibatis.io.VFS - Class not found: org.jboss.vfs.VFS
[DEBUG] 2018-10-26 13:05:55,826 org.apache.ibatis.io.JBoss6VFS - JBoss 6 VFS API is not available in this environment.
[DEBUG] 2018-10-26 13:05:55,827 org.apache.ibatis.io.VFS - Class not found: org.jboss.vfs.VirtualFile
[DEBUG] 2018-10-26 13:05:55,828 org.apache.ibatis.io.VFS - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
[DEBUG] 2018-10-26 13:05:55,829 org.apache.ibatis.io.VFS - Using VFS adapter org.apache.ibatis.io.DefaultVFS
[DEBUG] 2018-10-26 13:05:55,942 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
[DEBUG] 2018-10-26 13:05:56,205 org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 2856254.
[DEBUG] 2018-10-26 13:05:56,206 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2b953e]
[DEBUG] 2018-10-26 13:05:56,209 com.ssm.mybatiseg.dao.provider.ProviderMapper.modify - ==>  Preparing: update smbms_provider set proCode=?, proName=?, proDesc=?, proCode=?, proContact=?, proPhone=?, createdBy=?, creationDate=? where id = ? 
[DEBUG] 2018-10-26 13:05:56,279 com.ssm.mybatiseg.dao.provider.ProviderMapper.modify - ==> Parameters: CS_test666(String), 测试公司名6666(String), 测试公司描述666(String), CS_test666(String), 测试联系人666(String), 1000000000(String), 1(Integer), 1999-01-01 00:00:00.0(Timestamp), 17(Integer)
[DEBUG] 2018-10-26 13:05:56,463 com.ssm.mybatiseg.dao.provider.ProviderMapper.modify - <==    Updates: 1
[DEBUG] 2018-10-26 13:05:56,463 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2b953e]
[DEBUG] 2018-10-26 13:05:56,520 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2b953e]
[DEBUG] 2018-10-26 13:05:56,521 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2b953e]
[DEBUG] 2018-10-26 13:05:56,521 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 2856254 to pool.
更新条数1
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值