解决 springboot3 集成 mybatis-plus 报 Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required异常

5 篇文章 0 订阅

解决 springboot3 集成 mybatis-plus 报 sqlSession 异常

如下是官网给出的信息,跟着做就行,其实只用在pom.xml中加一段就行了

CSDN一堆解决方案没一个能打的,浪费我一早上时间,以下就是正解啦。帮忙点个赞,让大家都能看见,给在垃圾堆里面翻一早上的人更快看见。
在这里插入图片描述
在这里插入图片描述

<repositories>
<!-- 下载mybatis-plus SNAPSHOT 版本所需仓库 -->
	<repository>
		<id>ossrh</id>
		<name>OSS Snapshot repository</name>
		<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
		<releases>
			<enabled>false</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
</repositories>
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
当在Spring Boot 3中尝试整合MyBatis-Plus时,遇到`Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required`这个错误,通常是由于你没有正确配置SpringMyBatis-Plus之间的依赖关系。这是必要的,因为MyBatis-Plus依赖于MyBatis的`SqlSessionFactory`或`SqlSessionTemplate`来执行SQL操作。 以下是解决这个问题的步骤: 1. **配置MyBatis**: 在Spring Boot应用的配置文件(application.yml 或 application.properties)中添加MyBatisMyBatis-Plus相关的配置。例如: ```yaml mybatis: type-aliases-package: com.example.demo.entity # 如果你的实体类包名在此 mapper-locations: classpath:mapper/*.xml # XML映射文件的位置 config-location: classpath:mybatis-config.xml # 配置文件位置(可选) mybatis-plus: global-config: # 全局配置类 db-config: # 数据库配置 id-type: ID_WORKER # 自增主键策略 table-prefix: "tb_" # 表前缀 ``` 2. **添加依赖**: 确保在你的`pom.xml`或`build.gradle`文件中包含了Spring BootSpring Data JPA(如果使用)、MyBatis以及MyBatis-Plus的依赖。例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> ``` 3. **注入SqlSessionFactorySqlSessionTemplate**: 在你的Repository或Service类上,需要通过`@Autowired`注解来注入`SqlSessionFactory`或`SqlSessionTemplate`。例如: ```java @Autowired private SqlSessionFactory sqlSessionFactory; ``` 4. **检查是否扫描Mapper接口**: 如果你使用的是Java配置(即不使用XML),记得在`@Configuration`类中启用Mapper扫描: ```java @MapperScan("com.example.demo.mapper") // 替换为你的Mapper接口所在的包名 ``` 按照以上步骤检查并配置,应该能解决`Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required`的错误。如果你在配置过程中还是遇到问题,可以提供具体的错误堆栈以便更精确地诊断。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李南想做条咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值