MyBatis 配置文件空值设置

1、如果数据库字段允许为null空值,但Mybatis配置文件没有为该字段设置默认值时,如果外界传入的是空值时将会发生如下错误:

DEBUG 11-22 12:25:08 ooo Connection Opened  (JakartaCommonsLoggingImpl.java:27)
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database.  Cause: org.apache.ibatis.type.TypeException: Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for allnullable parameters. Cause: java.sql.SQLException: 无效的列类型
### The error may involve com.cdsf.messageplus.warehouse.message.dao.MessageDAO.insertMessage-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.type.TypeException: Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: 无效的列类型
 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
 at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:100)
 at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:87)
 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54)
 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
 at $Proxy1.insertMessage(Unknown Source)
 at com.cdsf.messageplus.warehouse.message.serviceimpl.MessageServiceImpl.insertMessage(MessageServiceImpl.java:65)
 at com.cdsf.messageplus.warehouse.message.action.MessageAction.saveDraft(MessageAction.java:284)
 at com.cdsf.messageplus.warehouse.message.action.MessageAction.add(MessageAction.java:110)
 at com.cdsf.messageplus.warehouse.message.action.TestSaveMsg.main(TestSaveMsg.java:73)
Caused by: org.apache.ibatis.type.TypeException: Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: 无效的列类型
 at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:17)
 at org.apache.ibatis.executor.parameter.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:73)
 at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:61)
 at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:43)
 at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:56)
 at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:28)
 at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:72)
 at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:43)
 at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:98)
 ... 8 more
Caused by: java.sql.SQLException: 无效的列类型
 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
 at oracle.jdbc.driver.OracleStatement.getInternalType(OracleStatement.java:3433)
 at oracle.jdbc.driver.OraclePreparedStatement.setNullCritical(OraclePreparedStatement.java:4261)
 at oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:4250)
 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.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100)
 at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57)
 at oracle.jdbc.internal.OraclePreparedStatement$$EnhancerByProxool$$11fe31fb.setNull(<generated>)
 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.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:53)
 at $Proxy2.setNull(Unknown Source)
 at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:15)
 ... 16 more
DEBUG 11-22 12:25:08 xxx Connection Closed  (JakartaCommonsLoggingImpl.java:27)

2、解决办法,需要为空值字段设置默认值如下:

                #{isLss,jdbcType=DECIMAL},
             
                #{isRead,jdbcType=DECIMAL},
             
               #{desc1,jdbcType=VARCHAR},
             
               #{desc2,jdbcType=VARCHAR}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值