MyBatis报错Type handler was null on parameter mapping for property XX

报错信息如下:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [/Users/pilaf/IdeaProject/pilaf-datax-platform/datax-dao/target/classes/mapper/UserMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [/Users/pilaf/IdeaProject/pilaf-datax-platform/datax-dao/target/classes/mapper/UserMapper.xml]'. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'roles'. It was either not specified and/or could not be found for the javaType (java.util.Set) : jdbcType (VARCHAR) combination.
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
	... 79 more
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [/Users/pilaf/IdeaProject/pilaf-datax-platform/datax-dao/target/classes/mapper/UserMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [/Users/pilaf/IdeaProject/pilaf-datax-platform/datax-dao/target/classes/mapper/UserMapper.xml]'. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'roles'. It was either not specified and/or could not be found for the javaType (java.util.Set) : jdbcType (VARCHAR) combination.
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:595)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:475)
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:615)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:180)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$78ceeca5.CGLIB$sqlSessionFactory$2(<generated>)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$78ceeca5$$FastClassBySpringCGLIB$$615744c3.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$78ceeca5.sqlSessionFactory(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 80 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [/Users/pilaf/IdeaProject/pilaf-datax-platform/datax-dao/target/classes/mapper/UserMapper.xml]'. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'roles'. It was either not specified and/or could not be found for the javaType (java.util.Set) : jdbcType (VARCHAR) combination.
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:122)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:94)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:593)
	... 93 more
Caused by: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'roles'. It was either not specified and/or could not be found for the javaType (java.util.Set) : jdbcType (VARCHAR) combination.
	at org.apache.ibatis.mapping.ParameterMapping$Builder.validate(ParameterMapping.java:119)
	at org.apache.ibatis.mapping.ParameterMapping$Builder.build(ParameterMapping.java:104)
	at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:123)
	at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:67)
	at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:77)
	at org.apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:45)
	at org.apache.ibatis.scripting.defaults.RawSqlSource.<init>(RawSqlSource.java:46)
	at org.apache.ibatis.scripting.defaults.RawSqlSource.<init>(RawSqlSource.java:40)
	at org.apache.ibatis.scripting.xmltags.XMLScriptBuilder.parseScriptNode(XMLScriptBuilder.java:72)
	at org.apache.ibatis.scripting.xmltags.XMLLanguageDriver.createSqlSource(XMLLanguageDriver.java:44)
	at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:96)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:137)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:130)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
	... 95 more

原因是我的User类定义成如下:

@Data
public class User {

    private Long id;

    private Long uid;

    private String roles;

    private boolean enable;

    private Long createUid;

    private Long updateUid;

    private Date createTime;

    private Date updateTime;

    public Set<String> getRoles(){
        if(StringUtils.isNotEmpty(roles)){
            String[] split = roles.split(",");
            return new HashSet<>(Arrays.asList(split));
        }

        return Collections.emptySet();
    }
}

MyBatis会人为我有一个Set类型的roles字段要保存到数据库里,没有找到Set类型到jdbcType=VARCHAR转换的TypeHandler。而实际上我只是想在User类里加一个工具方法,解决方法就是把这个getRoles方法去掉,相应的逻辑放在User类之外即可。

你可能感兴趣的:(mybatis)