springboot整合MyBatis启动报错 required a bean of type / error creating bean with name ‘sqlSessionFactory‘

自己编写一个springboot项目,新建时按照如何创建一个SpringBoot项目?(详细的图文教程)_一颗卷心菜丶的博客-CSDN博客_springboot项目创建

然后pom中引入MyBatis,编写好dao(随意的)

import com.weibo.entity.Article;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public interface ArticleDao {

    void insertArticle(long userId, Article article);

    void selectAllArticle();

    List<Article> selectHot20Article();

    List<Article> selectArticleWithKey();

}

启动

2022-09-15 18:06:24.721  WARN 9652 --- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'articleAction': Unsatisfied dependency expressed through field 'articleDbService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleDbServiceImpl': Unsatisfied dependency expressed through field 'articleDao'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.weibo.dao.ArticleDao' available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-09-15 18:06:24.729  INFO 9652 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-09-15 18:06:24.753  INFO 9652 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-09-15 18:06:24.801 ERROR 9652 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field articleDao in com.weibo.service.impl.ArticleDbServiceImpl required a bean of type 'com.weibo.dao.ArticleDao' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

歌词大意:名叫articleDbServiceImpl的类里面用Autowired自动注入了名叫articleDao的属性,这个articleDao的类型是ArticleDao,但压根没有类型为ArticleDao的Bean 划重点:required=true,也就是一定要但没找到

解决思路1:把required=true改成required=false 我不要不就不报错了 (划掉 ,还用不用辣?)

原因:同时满足 (1)ArticleDao类没加@Mapper (2)启动类(就那个@SpringBootApplication的)没有@MapperScan(basePackages = {"对应的dao层类目录"})

任意完成上述一项任务,MyBatis的JDK就能自动把dao层类ArticleDao变为可用的

@Mapper
@Repository
public interface ArticleDao { 
略
@MapperScan(basePackages = {"com.my.dao"})//你的dao层类目录
@SpringBootApplication
public class LzjSpringBootApplication {
还是略

作为一个小心严谨的人 我把两项任务都完成了  心满意足的启动

2022-09-15 18:35:37.329  WARN 22980 --- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'articleAction': Unsatisfied dependency expressed through field 'articleDbService';
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleDbServiceImpl': Unsatisfied dependency expressed through field 'articleDao'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleDao' defined in file [C:\Users\46092\IdeaProjects\lzj-SpringBoot\target\classes\com\weibo\dao\ArticleDao.class]: 
Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; 
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2022-09-15 18:35:37.337  INFO 22980 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-09-15 18:35:37.369  INFO 22980 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-09-15 18:35:37.401 ERROR 22980 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

细品,发现报错看似又在说articleDao,实际想说dataSource,ZWZYBZJ了属于是

得出结论:看报错 要看栈的最底部

解决思路很简单:springboot的数据库配置配的不对

解决(根据实际情况细节不同):application.properties配就完了

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306

spring.datasource.username=username
spring.datasource.password=password

spring.datasource.driver-class-name一看就知道是选数据库驱动的类名 那肯定这个类你得有

所以如果报红就查一下引了没(总不能自己手写吧):pom文件 如果没有就加上

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

总结:排查MyBatis注解加了没,排查数据源配置配好了没

                查错两法宝 耐心和有道 (指拿着有道词典细品报错信息)

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值