场景
MyBatis映射文件,参数是一个对象,这个对象的某个属性是个一维数组,如何取出这个数组的两个值,进行between and比较:
java
// UserQuery 类中的字段定义
public class UserQuery {
private LocalDate[] birth;
// getter and setter
}
xml
<if test="birth != null">
and birth between #{birth[0],jdbcType=DATE} and #{birth[1],jdbcType=DATE}
</if>
错误
java
org.mybatis.spring.MyBatisSystemException:
### Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birth[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.time.LocalDate;) : jdbcType (DATE) combination.
### Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birth[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.time.LocalDate;) : jdbcType (DATE) combination.
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:99)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:347)
at jdk.proxy2/jdk.proxy2.$Proxy23.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:194)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:141)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
at jdk.proxy2/jdk.proxy2.$Proxy24.selectWithCondition(Unknown Source)
at wego.mapper.UserMapperTest.selectWithCondition(UserMapperTest.java:138)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birth[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.time.LocalDate;) : jdbcType (DATE) combination.
### Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birth[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.time.LocalDate;) : jdbcType (DATE) combination.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:156)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:142)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:333)
... 11 more
Caused by: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birth[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.time.LocalDate;) : jdbcType (DATE) combination.
at org.apache.ibatis.mapping.ParameterMapping$Builder.validate(ParameterMapping.java:117)
at org.apache.ibatis.mapping.ParameterMapping$Builder.build(ParameterMapping.java:104)
at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:147)
at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:90)
at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:76)
at org.apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:52)
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:42)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:320)
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:128)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:59)
at jdk.proxy2/jdk.proxy2.$Proxy29.query(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:154)
... 18 more
解决方案
修改mybatis映射文件中:
xml
<if test="birth != null">
and birth between #{birth[0],jdbcType=DATE,typeHandler=org.apache.ibatis.type.LocalDateTypeHandler} and #{birth[1],jdbcType=DATE,typeHandler=org.apache.ibatis.type.LocalDateTypeHandler}
</if>