使用SpringBoot找不到Mapper的Bean問題及解決
SpringBoot找不到Mapper的Bean
記一次令人抓狂的錯誤解決經(jīng)歷,先說結果:
數(shù)據(jù)庫版本使用錯誤,沒有配置pom文件中的版本號,默認使用了本地驅(qū)動中的最高6版本。
更改為5版本即可發(fā)生報錯后找了很多的文檔也沒有符合我這個樣子的,于是我就把自己的經(jīng)歷寫下來,給和我一樣犯了這個錯的初學者看。
初期報錯
@Service
public class UserService {
@Autowired
private DeptMapper deptMapper;//此處報錯為找不到mapper類型的Bean
檢查啟動類中已經(jīng)正確配置路徑。
強行啟動程序后
//這句話說使用數(shù)據(jù)庫鏈接應該使用帶cj那個,說明使用的驅(qū)動版本是6,但是編寫的配置文件是5的配置。
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.
//這句說的是找不到mapper的目錄
Property 'mapperLocations' was not specified.?
如果強行調(diào)用service
就會出現(xiàn)如下錯誤:
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b3ac131]
2021-06-05 16:43:55.847 ERROR 32732 --- [p-nio-80-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
### The error may exist in top/loveeveryone/mapper/DeptMapper.java (best guess)
### The error may involve top.loveeveryone.mapper.DeptMapper.selectByPrimaryKey
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.] with root cause
//說數(shù)據(jù)庫時區(qū)有問題
綜合檢查,發(fā)現(xiàn)數(shù)據(jù)庫驅(qū)動有問題。
在pom文件中加載Mysql數(shù)據(jù)庫驅(qū)動程序時沒有添加驅(qū)動程序版本,默認使用最高版本導致驅(qū)動失敗。
將驅(qū)動修改至5版本就好了
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot集成AOP實現(xiàn)日志記錄與接口權限校驗
本文主要介紹了SpringBoot集成AOP實現(xiàn)日志記錄與接口權限校驗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2026-02-02
基于指針pointers和引用references的區(qū)別分析
本篇文章介紹了,基于指針pointers和引用references的區(qū)別分析。需要的朋友參考下2013-05-05
Maven編譯報錯:未與 -source 8 一起設置引導類路徑的完美解決方案
這篇文章主要為大家詳細介紹了Maven編譯報錯:未與 -source 8 一起設置引導類路徑的相關解決方案,文中的示例代碼講解詳細,有需要的小伙伴可以了解下2025-10-10
springboot導出excel多個sheet導出的實現(xiàn)
在Java開發(fā)過程中,合理配置pom.xml文件對項目的管理和構建至關重要,通過添加依賴管理項目所需的庫,簡化了項目構建過程,同時,掌握導出excel工具類的使用,可以有效地處理數(shù)據(jù)導出需求,提高工作效率,本文結合個人經(jīng)驗2024-10-10
java并發(fā)編程工具類JUC之LinkedBlockingQueue鏈表隊列
大家都知道LinkedBlockingQueue 隊列是BlockingQueue接口的實現(xiàn)類,所以它具有BlockingQueue接口的一切功能特點,他還提供了兩種構造函數(shù),本文中通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-06-06

