问题描述
在eclipse开发环境下,使用mybaitis 创建一个自定义的 BaseTypeHandler,用于解析 String 数组。开发环境中运行正常。当上传到测试环境后,运行报 ested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalStateException: No typehandler found for property *** 异常。
产生原因
StringArrayTypeHandler 定义在另外一个模块,并打包为jar包依赖。
查询资料 在GitHub 官网 有相同issues ---> https://github.com/mybatis/mybatis-3/issues/325
通过debug 发现 eclipse 在加载class 使用的 是 ResolverUtil类 具体加载方法为
public ResolverUtil<T> find(Test test, String packageName) {
String path = getPackagePath(packageName);
try {
List<String> children = VFS.getInstance().list(path);
for (String child : children) {
if (child.endsWith(".class")) {
addIfMatching(test, child);
}
}
} catch (IOException ioe) {
log.error("Could not read package: " + packageName, ioe);
}
return this;
}
解决方案
再xml 中添加
<settings>
<setting name="vfsImpl" value="com.mydomain.vfs.SpringBootExecutableJarVFS" />
</settings>
如果使用Springboot 可以在 创建 SqlSessionFactory 之前中添加
VFS.addImplClass(SpringBootVFS.class);
这样使用jar包启动 不会再报错。
本文介绍了一个使用MyBatis处理String数组时遇到的问题及解决方案。问题出现在将自定义BaseTypeHandler部署到测试环境时,由于依赖的jar包导致无法识别类型处理器。文章详细解释了产生该问题的原因,并提供了两种解决方法:一是修改MyBatis配置文件中的vfsImpl设置;二是通过编程方式在创建SqlSessionFactory前指定VFS实现。
372






