1. 程式人生 > 程式設計 >idea中springboot專案連線資料庫報錯的原因解析

idea中springboot專案連線資料庫報錯的原因解析

一.我在運用mybatis下報錯,反覆檢查無邏輯問題,下圖為專案結構截圖

idea中springboot專案連線資料庫報錯的原因解析

二.最後總結問題如下:

1)報錯程式碼:

Caused by: java.lang.RuntimeException: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:486)

原因:pom.xml檔案中mysql依賴版本低(5.1.6),但並沒有在properties或者yml檔案中手動新增驅動如下(沒有添加註釋的driver-class-name)

spring:
 datasource:
  url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
  username: root
  password: root
  #driver-class-name: com.mysql.cj.jdbc.Driver
  #driver-class-name: com.mysql.jdbc.Driver

ps:下面兩個配置都可以讓低版本mysql依賴下的專案執行,但第二個會報錯如圖

driver-class-name: com.mysql.cj.jdbc.Driver

driver-class-name: com.mysql.jdbc.Driver

idea中springboot專案連線資料庫報錯的原因解析

2)

報錯程式碼:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.mapper.UserMapper.findAll

原因分析:pom.xml中,依賴mysql為高版本(8.0.22)此時任然報錯檢查properties/yml配置檔案中的如下程式碼

#載入Mapper對映檔案
mapper-locations: classpath:/mybatis/mappers/*.xml

到此這篇關於idea中springboot專案連線資料庫報錯的原因解析的文章就介紹到這了,更多相關idea中springboot連線資料庫報錯內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!