记录一次springboot+mybatisplus项目@Autowired依赖注入失败的问题

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxMapper' defined in file Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

 

遇到@Autowired依赖注入失败的问题,百度也找不到答案,基本上说是因为IDEA的严格导致显示报错的,但实际上我是真的报错了。

后来阅读https://stackoverflow.com/questions/35610147/spring-boot-mybatis-mapperscan-and-sqlsessionfactory发现原来是我少了mybatis-plus-boot-starter的依赖,加入依赖问题解决

转载于:https://www.cnblogs.com/EchoXian/p/9603776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Spring Boot 和 MyBatis-Plus 进行项目重构,可以遵循以下步骤来完成: 1. 添加依赖:在项目的 `pom.xml` 文件中,添加 Spring Boot 和 MyBatis-Plus 的相关依赖。例如: ```xml <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 请确保将 MyBatis-Plus 的最新版本号替换到 `<version>` 标签中。 2. 配置数据库连接:在 `application.properties` 或 `application.yml` 文件中配置数据库连接信息,包括数据库 URL、用户名和密码等。 3. 创建实体类:根据项目需求,在 Java 包中创建实体类,用于映射数据库表结构。 4. 创建 Mapper 接口:创建一个继承自 MyBatis-Plus 的 `BaseMapper` 接口的 Mapper 接口,用于定义数据库操作方法。 ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface YourEntityMapper extends BaseMapper<YourEntity> { // 在此定义自定义的数据库操作方法 } ``` 5. 编写 Mapper XML 文件:在 resources 目录下创建与 Mapper 接口对应的 XML 文件,编写 SQL 语句以实现数据库操作。 6. 创建 Service 层:创建一个 Service 层来处理业务逻辑,并注入 Mapper 接口的实例。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class YourEntityService { private final YourEntityMapper yourEntityMapper; @Autowired public YourEntityService(YourEntityMapper yourEntityMapper) { this.yourEntityMapper = yourEntityMapper; } // 在此编写业务逻辑方法 } ``` 7. 创建 Controller 层:创建一个 Controller 层来处理 HTTP 请求,并注入 Service 层的实例。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/your-entity") public class YourEntityController { private final YourEntityService yourEntityService; @Autowired public YourEntityController(YourEntityService yourEntityService) { this.yourEntityService = yourEntityService; } // 在此定义接口方法,处理请求 } ``` 8. 运行项目:启动 Spring Boot 应用程序,可以使用 `@SpringBootApplication` 注解标记启动类,并在其中添加 `main` 方法。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 以上是使用 Spring Boot 和 MyBatis-Plus 进行项目重构的基本步骤。你可以根据实际需求进行更多的配置和开发工作,如添加其他依赖、配置缓存、编写业务逻辑等。 希望以上内容对你有所帮助!如果有任何疑问,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值