mybatis中SqlSessionFactoryBean路径配置错误

本文解决了一个关于MyBatis配置中mapperLocations属性设置错误的问题,详细介绍了如何正确配置以加载XML映射文件。

mybatis配置文件

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:/com/**/wap/dao/impl/*.xml]: class path resource [com/***/wap/dao/impl/] cannot be resolved to URL because it does not exist

Caused by: java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:/com/***/wap/dao/impl/*.xml]: class path resource [com/***/wap/dao/impl/] cannot be resolved to URL because it does not exist



路径报错:修改为

<bean id="msqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="mapperLocations" value="classpath*:/com/**/wap/dao/impl/*.xml" />
</bean>

http://ojdbc.com/springmvcmybatis-exception-resolution/

在使用 MyBatis 时遇到 `Builder` 出现空指针异常(`NullPointerException`)的情况,通常与配置文件、资源加载或映射器的初始化问题有关。以下是可能导致此错误的常见原因以及相应的解决方案: ### 1. **MyBatis 配置文件路径错误** 如果 `mybatis-config.xml` 文件路径配置不正确,或者文件本身存在格式错误,会导致 `SqlSessionFactoryBuilder` 在构建 `SqlSessionFactory` 时无法正确加载配置,从而引发空指针异常。 **解决方案:** - 确保 `mybatis-config.xml` 文件位于类路径下(如 `resources` 文件夹),并且配置文件的路径在代码中正确引用。 - 检查配置文件中是否存在语法错误,例如标签未正确闭合或属性拼写错误。 示例代码: ```java String resource = "mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); ``` 确保 `mybatis/mybatis-config.xml` 文件实际存在于类路径下,并且可以被正确加载[^2]。 --- ### 2. **Mapper XML 文件未正确加载** 如果 `Mapper` 文件路径未正确配置,或者没有在 `mybatis-config.xml` 中注册,也会导致 `Builder` 在解析 `Mapper` 时出现空指针异常。 **解决方案:** - 确保 `Mapper` 文件位于正确的路径下,并且在 `mybatis-config.xml` 中通过 `<mappers>` 标签注册。 - 如果使用 Spring 整合 MyBatis,确保 `mapperLocations` 属性正确指向 `Mapper` 文件的位置。 示例配置: ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> ``` 确保 `mapperLocations` 指向的路径包含所有需要的 `Mapper` 文件,并且文件名匹配模式正确[^2]。 --- ### 3. **Spring 整合 MyBatis配置问题** 在 SpringMyBatis 整合时,如果 `SqlSessionFactory` 或 `MapperScannerConfigurer` 配置不正确,也可能导致 `Builder` 在初始化过程中出现空指针异常。 **解决方案:** - 确保 `SqlSessionFactory` 正确配置,并且能够访问到数据库连接池。 - 检查 `MapperScannerConfigurer` 是否正确配置了 `basePackage` 属性,以扫描到所有的 `Mapper` 接口。 示例配置: ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 确保 `dataSource` 已正确配置,并且能够正常连接到数据库[^1]。 --- ### 4. **多环境配置问题** 如果项目中使用了多环境配置(如开发环境、测试环境、生产环境),并且配置文件中缺少对应的环境配置,可能会导致 `Builder` 在查找环境配置时出现空指针异常。 **解决方案:** - 确保在 `mybatis-config.xml` 中配置了正确的环境,并且在启动时指定了默认环境。 - 检查是否在 `applicationContext.xml` 或其他 Spring 配置文件中正确引用了数据库配置。 示例配置: ```xml <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> ``` 确保 `default` 属性指向的环境在配置文件中存在,并且配置正确[^3]。 --- ### 5. **依赖版本不兼容** 不同版本的 MyBatisSpring 之间可能存在兼容性问题,尤其是在使用较旧的 Spring 版本时,可能会导致 `Builder` 在初始化过程中出现空指针异常。 **解决方案:** - 确保使用的 MyBatisSpring 版本兼容。 - 检查 Maven 或 Gradle 依赖管理文件中的版本号,确保没有冲突。 示例 Maven 依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis.spring</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> ``` 确保依赖版本之间没有冲突,并且与 Spring Boot 版本兼容[^4]。 --- ### 6. **资源未正确释放** 在某些情况下,如果资源未正确释放,例如 `InputStream` 未关闭,可能会导致后续的 `Builder` 操作失败。 **解决方案:** - 确保在使用完 `InputStream` 后正确关闭资源。 - 使用 try-with-resources 语句确保资源自动关闭。 示例代码: ```java try (InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml")) { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } ``` 确保资源在使用后正确关闭,避免资源泄漏[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值