注释配置的mybatis DAO,在Date参数中为null

时间:2017-08-30 21:28:07

标签: java sql mybatis

UPDATE:问题是if @ test中的语法错误,其中引用不应该包含在#{}中;以下作品:<if test='dateFrom != null'>

我有一个DAO对象,其中选择了一个java.util.Date参数:

@Results({...})
@Select("<script>SELECT ... FROM ... WHERE ... <if test='#{dateFrom} !=
 null'>AND date &gt; #{dateFrom}</if></script>")
List<Person> getPersons(@Param("dateFrom") java.util.Date date, ...);

当通过此参数传递空值时,mybatis会抱怨:

org.apache.ibatis.type.TypeException: 
Could not set parameters for mapping: ParameterMapping{property='dateFrom', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 
Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #2 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. 
Cause: java.sql.SQLException: Invalid column type: 1111

我已经阅读了几个建议,其中一些建议将类型显式设置为jdbcType = TIMESTAMP,但是考虑到我的DAO是通过注释配置的......有没有我可以做到的地方?

我尝试将引用从#{dateFrom}更改为#{dateFrom, jdbcType=TIMESTAMP},但mybatis不喜欢这样:

org.apache.ibatis.binding.BindingException: Parameter 'TIMESTAMP' not found. Available parameters are [param7, param8, param5, param6, offset, param9, contextId, canSignOnly, param3, dateFrom, param4, param1, param2, originatedOnly, limit, dateTo, itemTypeMask, param10, statusIds, signedOnly]

注释@Param似乎没有提供定义jdbcType的方法。

0 个答案:

没有答案