springboot连接MySQL

有没有人知道为啥springboot在测试文件中运行的时候一直连接不上MySQL啊,数据库用的phpstudy的MySQL
application.properties文件

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/store
spring.datasource.username=root
spring.datasource.password=123456

测试文件

package com.cy.store;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.SQLException;

@SpringBootTest
public class StoreApplicationTests
{
    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads()
    {
        System.out.println("hello world");
    }

    @Test
    public void getConnection() throws SQLException
    {
        System.out.println(dataSource.getConnection());
    }
}

报错

java.lang.NullPointerException
    at com.cy.store.StoreApplicationTests.getConnection(StoreApplicationTests.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

你这里用的是junit4,他结合SpringbootTest的时候还需要加一个注解。@RunWith(SpringRunner.class)

首先通过MySQL工具连接数据库试试,看数据库是否运行正常

启动类呢??

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7497189
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:springboot连接mysql问题解决
  • 除此之外, 这篇博客: SpringBoot 2.7教程:SpringBoot 整合 Mysql 项目应用-2022年最新图文版本中的 7.4 配置mysql连接信息,application.properties 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • # 数据库驱动:
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    # 数据源名称
    spring.datasource.name=defaultDataSource
    # 数据库连接地址
    spring.datasource.url=jdbc:mysql://服务器地址:3306/db_name?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
    # 数据库用户名&密码:
    spring.datasource.username=***
    spring.datasource.password=***