现象:
使用Mybatis Plus的自动填充功能时,出现以下异常
2017-09-07 16:29:27.703 [http-nio-30000-exec-1] [ERROR] c.z.test.common.ExceptionAdvice.handleException 101 -- 服务运行异常,nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='gmtModified', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #11 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='gmtModified', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #11 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) ~[mybatis-spring-1.3.1.jar:1.3.1]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.1.jar:1.3.1]
at com.sun.proxy.$Proxy13.insert(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278) ~[mybatis-spring-1.3.1.jar:1.3.1]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57) ~[mybatis-3.4.4.jar:3.4.4]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.4.jar:3.4.4]
at com.sun.proxy.$Proxy29.insert(Unknown Source) ~[na:na]
at com.baomidou.mybatisplus.service.impl.ServiceImpl.insert(ServiceImpl.java:100) ~[mybatis-plus-2.1.0.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at com.sun.proxy.$Proxy33.insert(Unknown Source) ~[na:na]
相关源码:
copy的文档中源码 稍加改动
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import java.util.Date;
public class MyMetaObjectHandler extends MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
Object fieldType = getFieldValByName("gmtCreate", metaObject);//mybatis-plus版本2.0.9+
if (fieldType == null) {
setFieldValByName("gmtCreate", new Date(System.currentTimeMillis()), metaObject);//mybatis-plus版本2.0.9+
}
}
@Override
public void updateFill(MetaObject metaObject) {
Object fieldType = getFieldValByName("gmtModified", metaObject);//mybatis-plus版本2.0.9+
if (fieldType == null) {
setFieldValByName("gmtModified", new Date(System.currentTimeMillis()), metaObject);//mybatis-plus版本2.0.9+
}
}
}
spring中Mybatis的配置文件
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:xml/mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.zx.test.entity" />
<property name="mapperLocations" value="classpath:com/zx/test/mapper/xml/*Mapper.xml" />
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor"
class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="oracle" />
</bean>
</array>
</property>
<property name="globalConfig" ref="globalConfig"></property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zx.test.mapper" />
</bean>
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--
AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
-->
<property name="idType" value="1" />
<!-- Oracle需要添加该项 -->
<property name="dbType" value="oracle" />
<!-- 全局表为下划线命名设置 true -->
<property name="dbColumnUnderline" value="true" />
<!-- 公共字段填充处理器 -->
<property name="metaObjectHandler" ref="myMetaObjectHandler" />
</bean>
<!-- 自定义处理器 -->
<bean id="myMetaObjectHandler" class="com.zx.test.common.config.MyMetaObjectHandler" />
数据库实体对象中问题字段配置
/**
* 创建时间
*/
@TableField(value = "GMT_CREATE",fill = FieldFill.INSERT)
private Date gmtCreate;
/**
* 更新时间
*/
@TableField(value = "GMT_MODIFIED",fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
目前这个问题尚未解决~,具体原因还待查。
如果有人遇到同样问题,请赐教。