自己编写一个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注解加了没,排查数据源配置配好了没
查错两法宝 耐心和有道 (指拿着有道词典细品报错信息)