需求描述 mybatis 中 使用 LocalDate 等时间类型无法被正确的映射,出现以下异常:
1 Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: 无效的列类型
解决方案 添加类型处理器依赖即可(3.4 +):
1 2 3 4 5 <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-typehandlers-jsr310</artifactId > <version > ${lastVersion}</version > </dependency >
老版本需要手动配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <typeHandlers > <typeHandler handler ="org.apache.ibatis.type.InstantTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.LocalDateTimeTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.LocalDateTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.LocalTimeTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.OffsetDateTimeTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.OffsetTimeTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.ZonedDateTimeTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.YearTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.MonthTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.YearMonthTypeHandler" /> <typeHandler handler ="org.apache.ibatis.type.JapaneseDateTypeHandler" /> </typeHandlers >
官网说明