Spring-boot学习中踩过的坑
有的坑是自己手误导致,有的是因为配置与视频中不完全一致导致。总结下,希望遇到类似问题的同学能尽早脱离苦海。
Field studentMapper in com.springboot.service.StudentService required a bean of type ‘com.springboot.mapper.StudentMapper’ that could not be found.
- 根本原因是粗心导致的。
标题的意思是一个Mapper bean没有被找到! Spring-boot对Mapper类是通过自动扫描注入的。那么就应该检查启动类Application中的注解配置。检查发现是配置手误:
- 解决办法
basePackage是包路径!结果配置成了类路径。修改成com.springboot.mapper 就ok了。
Loading class `com.mysql.jdbc.Driver’. This is deprecated.
完整错误信息是:Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
- 通过网上搜索发现是自己导入的myql的依赖版本过高。视频里的老师用的是5.12。而我配置的是最新版本。
-
解决办法
①修改datasource.driver-class-name成下面这样,注意缩进。(推荐)datasource:
(2空格)driver-class-name: com.mysql.cj.jdbc.Driver②降低mysql依赖的版本。但是不要低于5.1.46
下面是包的查询路径:
https://mvnrepository.com/artifact/mysql/mysql-connector-java
Service中注入的Mapper报红色警告
虽然有红色警告存在,但是不影响服务启动和使用。
网上查资料发现。应该讲xxxMapper类中的注解从Mapper改成@Repository。红色警告就消失了。
@Autowired注解警告Field injection is not recommended
可以参考这篇博文 https://blog.csdn.net/zhangjingao/article/details/81094529