SpringBoot数据库连接失败解决方案


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    3.0.2


    com.mysql
    mysql-connector-j
    runtime


     org.springframework.boot
     spring-boot-starter-web

上面的是SpringBoot的Pop.xml数据库框架代码

SpringBoot是会自动读取配的,就不需要像之前一样配置Mybatis文件了,但是会出现一些奇怪的问题

例如这些

2023-11-09T20:42:47.570+08:00  WARN 23284 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.test]' package. Please check your configuration.

第一种是因为你没有@Mapper这个注释

SpringBoot数据库连接失败解决方案_第1张图片

错误案例如上图,添加@Mapper注释即可

No database selected  第二种

 [] threw exception [Request processing failed:

第三种org.springframework.jdbc.UncategorizedSQLException: 
### Error querying database.  Cause: java.sql.SQLException: No database selected

这两种是数据库选择问题

SpringBoot数据库连接失败解决方案_第2张图片

错误案例如上,因为没有添加对应的数据库而是直接调用属性

第三种:Access denied for user '严欣铷'@'localhost' (using password: YES)

第四种2023-11-09T20:50:20.501+08:00 ERROR 14124 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.mybatis.spring.MyBatisSystemException] with root cause
这个的核心是因为你的配置文件是name而不是USername

错误案例如下

SpringBoot数据库连接失败解决方案_第3张图片

改成username就完事了

完整代码及数据库如下

SpringBoot数据库连接失败解决方案_第4张图片

注意了yml文件和那个properties文件除了样式没有区别,用法是一样的

特别注意YML文件  名字 = 这一定要有一个空格 属性

SpringBoot数据库连接失败解决方案_第5张图片

SpringBoot数据库连接失败解决方案_第6张图片

SpringBoot数据库连接失败解决方案_第7张图片

SpringBoot数据库连接失败解决方案_第8张图片

运行结果如下

SpringBoot数据库连接失败解决方案_第9张图片

JSON文件的格式

你可能感兴趣的:(spring,boot,数据库,后端)