3

IDEA SpringBoot SQL连接常见五大异常处理

 1 year ago
source link: https://blog.51cto.com/yeatsliao/5885214
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

项目:Springboot+mybatis+MySQL

一、no data sources are configured to run this sql and provide advanced code assistance警告处理

1.异常错误

  • SpringBoot项目中.xml或者.sql文件调用时报错
No data sources are configured to run this SQL and provide advanced code assistance.
IDEA SpringBoot SQL连接常见五大异常处理_sql
  • 没有配置任何数据源来运行此sql并提供高级代码帮助
  • 意思就是你的项目并未连接到数据库,我们需要做的是连接SQL
  • 需要手动配置数据库客户端工具来连接SQL,并且执行命令创建数据表结构

3.解决方法

1.连接Database

在IDEA左下角的小框中,选择Database

IDEA SpringBoot SQL连接常见五大异常处理_mysql_02

在右上角弹出的菜单中,点击+号,添加Data Source中的MySQL

IDEA SpringBoot SQL连接常见五大异常处理_mysql_03

2.配置Mysql

填写你需要连接数据库的相关信息,Host本机就为localhost Port默认为3306,确定URL中的库连接

点击Test Connection,显示如下信息,则表示数据库链接成功

IDEA SpringBoot SQL连接常见五大异常处理_插入图片_05

二、Loading class `com.mysql.jdbc.Driver’. This is deprecated警告处理

1.异常错误

  • 数据源配置的驱动出错,启动后如果出现以下错误
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.

IDEA SpringBoot SQL连接常见五大异常处理_sql_06

  • MySQL5用的驱动url是com.mysql.jdbc.Driver
  • MySQL6以后用的是com.mysql.cj.jdbc.Driver。
  • 版本不匹配便会报驱动类已过时的错误。

3.解决方法

当配置好数据库后,还需要在src下找到application.properties的数据库配置

  • 本机MySQL版本5.7,driver-Class-name选择com.mysql.jdbc.Driver
  • MySQL时8.0以上,选择com.mysql.cj.jdbc.Driver
IDEA SpringBoot SQL连接常见五大异常处理_mysql_07

三、Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually警告处理

1.异常错误

  • 时区设置错误
Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually
  • 时区不一致,MySQL驱动jar中的默认时区是UTC,代表的是全球标准时间
  • 我们使用的时间北京时区是东八区,领先UTC八个小时
  • com.mysql.cj.jdbc.Driver是mysql-connector-java 6中的特性
  • 相比mysql-connector-java 5 多了一个时区serverTimezone

3.解决方法

IDEA SpringBoot SQL连接常见五大异常处理_sql_08
Advanced中将serverTimezone设置成Hongkong或者UTC,然后点击 APPLY

IDEA SpringBoot SQL连接常见五大异常处理_sql_09

再次尝试 Test Connection 成功连接

IDEA SpringBoot SQL连接常见五大异常处理_mysql_10

四、Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded警告处理

1.异常错误

  • url配置数据源出错,未能确定合适的驱动程序类
Failed to configure a DataSource: 'url' attribute is not specified and no embedded
  • pom.xml文件中添加了mybatis依赖
  • application.properties中没有配置连接数据库的url、用户名user 、和密码 password
  • pom.xml文件中添加了有关数据库的依赖时,需要在属性文件中配置连接该库的路径,用户名和密码

3.解决方法

  • application.properties填写相关配置信息,注意url连接数据库路径 url=jdbc:mysql://localhost:3306/test 这里的test指直接连接的数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=xxxxxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=8004
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

mybatis.mapper-locations=classpath:mapper/*.xml
IDEA SpringBoot SQL连接常见五大异常处理_sql_11

五、Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist警告处理

1.异常错误

  • 未指定对应的数据库
Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist
  • 出现此问题原因跟四一样,url配置不正确,导致找不到数据库中相关表
  • 或者是mapper.xml文件中查询条件出错导致
    IDEA SpringBoot SQL连接常见五大异常处理_插入图片_12
IDEA SpringBoot SQL连接常见五大异常处理_mysql_13

3.解决方法

  • 如果是url配置不正确,则参考四的解决方法
  • 如果是查询条件出错,查看语法错误并修改

no data sources are configured to run this sql and provide advanced code assistance警告处理
Loading class `com.mysql.jdbc.Driver’. This is deprecated警告处理
Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually警告处理
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded警告处理
Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist警告处理


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK