启动SpringBoot报错:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

文章描述了一个SpringBoot3.0.5项目在启动时遇到的依赖问题,因版本不兼容导致UserService和UserMapper创建失败。解决方法是将mybatis-spring-boot-starter的版本从2.1.0升级到3.0.2。
摘要由CSDN通过智能技术生成

springboot 项目启动时报如下错误

在这里插入图片描述

[AbstractApplicationContext.java:592] → [Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService': Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userMapper': Error creating bean with name 'userMapper' defined in file [D:\projects\tutorails_kanomoku\practisesvr\target\classes\com\zhangziwa\practisesvr\mapper\UserMapper.class]: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required]

原因 版本不兼容

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

方案:升级 mybatis-spring-boot-starter 版本即可

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

在这里插入图片描述

参考

SpringBoot3整合MyBatis报错:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
启动Spring Boot项目报错"Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"通常是由于配置不正确或缺少必要的配置导致的。根据引用中提到的问题,可能是以下几个原因导致的: 1. 缺少MyBatis的配置:确保在Spring Boot项目的配置文件中正确配置了MyBatis相关的属性,包括数据源、Mapper扫描路径等。 2. 缺少SqlSessionFactorySqlSessionTemplate的配置:在Spring Boot项目中,需要手动配置SqlSessionFactorySqlSessionTemplate。可以通过在配置类中使用@Bean注解来创建并配置它们。 3. 配置错误:检查配置文件中的属性名是否正确拼写,并确保属性值的类型正确。 以下是一个示例配置类,演示如何正确配置SqlSessionFactorySqlSessionTemplate: ```java @Configuration @MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "sqlSessionTemplate") public class MyBatisConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); // 配置其他属性,如MapperLocations等 return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 请注意,上述示例中的`com.example.mapper`是Mapper接口所在的包路径,你需要根据自己的项目结构进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值