我在 Maven 项目中工作时遇到了类似的问题,导致同样的错误。
我的情况是我有一个带有 Spring Boot 的可执行 jar,它有一个嵌套 jar(使用常规 Spring)作为依赖项。在 IntelliJ 中运行时,由于 IntelliJ 查找其类的方式,绝对没有问题。
在尝试使用 java -jar jarname.jar 在本地启动 jar 以便我可以将其部署到远程服务器上时,MyBatis 在扫描 typeAliases 和 typeHandlers 包时遇到了问题。
由于我正在处理一个混合了在 Java 和 xml 中初始化的 Spring Beans 的遗留项目,因此我花了很多时间来确定根本原因。很多答案都说将您的 resultType 更改为完整的类路径,例如 answer。这行得通。但就我而言,这意味着对我们的 DAO 进行数百次更改。
最后,我追上了external link from titanwolf。
您需要执行以下操作:
- 将以下依赖项添加到您的 POM(包含您的 MyBatis 设置的 jar)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- 在 SqlSessionFactoryBean 上设置 VFS 属性
xml:
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="vfs" value="org.mybatis.spring.boot.autoconfigure.SpringBootVFS"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
或者如果您使用常规 Java 进行 bean 初始化:
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
return factory;
}
就是这样。在本地和远程服务器上工作。希望我为其他人省去了一些麻烦。
注意: mybatis-config.xml 包含我的配置,包括但不限于:
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.my.path.model"/> <!--wasn't being picked up before fix-->
</typeAliases>
<typeHandlers>
<package name="com.my.path.mybatis"/> <!--wasn't being picked up before fix-->
</typeHandlers>
<mappers>
<mapper resource="sql/my-dao.xml"/>
<mappers>
</configuration>
您可以通过为 MyBatis 启用日志记录来更好地跟踪您自己的错误。只需将以下行添加到您的 application.properties 文件中:
logging.level.org.mybatis=DEBUG