Intellij idea:Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Initialization 
of bean failed; nested exception is 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/hello/mapping/*.xml]: class path resource [com/hello/mapping/] cannot be resolved to URL because it does not exist
···
···
···
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/hello/mapping/*.xml]: 
class path resource [com/hello/mapping/] cannot be resolved to URL because it does not exist

我用的是Intellij idea开发Spring+maven+mybatis,根据上面报错,是因为找不到xml文件,但是我的xml文件和路径都是对的,而且存在,为什么找不到呢?
后来发现,编译后的文件夹target中的class里,并没有mapping文件夹,更没有xml。那么问题就很清楚了,是Intellij idea导致不把xml编译进去,之前使用MyEclipse并没遇到这个问题。
解决办法是:须在pom.xml文件中添加如下配置(来源:http://blog.csdn.net/u012599988/article/details/44041205)

<build>
<finalName>API</finalName>
<resources>
······
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
······
</resources>
</build>
### Spring Boot中关于UserControlller、UserService和UserMapper的依赖注入问题分析 在Spring Boot项目中,`UnsatisfiedDependencyException`通常表示某些Bean未正确配置或缺失。对于使用MyBatis时出现的`sqlSessionFactory`或`sqlSessionTemplate`未配置导致的错误,这通常是由于MyBatis相关配置不完整或映射器(Mapper)未被正确注册到Spring容器中引起的[^1]。 以下是解决此问题的详细方法: #### 1. 确保引入了正确的依赖 首先需要确认项目的`pom.xml`文件中是否包含了MyBatis-Spring-Boot-Starter的相关依赖。如果没有,请添加以下依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version> </dependency> ``` #### 2. 配置数据源 确保`application.properties`或`application.yml`文件中已正确配置数据源信息。例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database spring.datasource.username=root spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` #### 3. 自动扫描Mapper接口 为了使Spring能够自动扫描并注册Mapper接口为Bean,需要在主类或配置类上添加`@MapperScan`注解。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的Mapper包路径 public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` #### 4. 配置SqlSessionFactory或SqlSessionTemplate 如果仍然遇到`sqlSessionFactory`或`sqlSessionTemplate`未配置的问题,可以手动定义这些Bean。例如,在一个配置类中添加以下代码: ```java import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class MyBatisConfig { @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` #### 5. Mapper接口示例 确保Mapper接口正确编写,并且与数据库表结构一致。例如: ```java import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserMapper { @Select("SELECT * FROM users WHERE id = #{id}") User findById(int id); } ``` #### 6. Service层调用Mapper 在Service层中通过构造函数或`@Autowired`注解注入Mapper。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private final UserMapper userMapper; @Autowired public UserService(UserMapper userMapper) { this.userMapper = userMapper; } public User getUserById(int id) { return userMapper.findById(id); } } ``` #### 7. Controller层调用Service 最后,在Controller层中注入Service并实现业务逻辑。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("/user/{id}") public User getUserById(@PathVariable int id) { return userService.getUserById(id); } } ``` 通过以上步骤,可以有效解决`UnsatisfiedDependencyException`问题,并确保`sqlSessionFactory`或`sqlSessionTemplate`正确配置。 ### 注意事项 - 如果使用的是Spring Boot 2.x版本及以上,推荐直接使用`mybatis-spring-boot-starter`,它会自动配置`SqlSessionFactory`和`SqlSessionTemplate`。 - 确保Mapper接口所在的包路径与`@MapperScan`注解中的路径一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值