Spring JDBC Null指针只是解决不了

问题描述

首先,我已经搜索了我连续3个小时能找到的每篇文章和每个问题。我只是想不通如何使用Spring运行JDBC连接。

我正在使用Gradle,所以事不宜迟,这里是我的build.gradle

plugins {
  id 'org.springframework.boot' version '2.3.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  id 'java'
}

group = 'my.self'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'

    runtimeOnly 'MysqL:mysql-connector-java'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage',module: 'junit-vintage-engine'
    }
}

test {
  useJUnitPlatform()
}

包含我的主要方法的类:

package my.self.SpringDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class,args);
        Tester a = new Tester();
    }
}

我的测试器类是我自动连接的JdbcTemplate类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class Tester {

    @Autowired
    JdbcTemplate jdbc;

    public Tester(){
        int result = jdbc.queryForObject("SELECT COUNT(*) FROM Customer",Integer.class);
        System.out.println(result);
    }
}

application.config文件

spring.datasource.url=jdbc:MysqL://localhost:3306/sks?serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver

但是我总是在这一行中得到一个NullPointerException:

int result = jdbc.queryForObject("SELECT COUNT(*) FROM Customer",Integer.class);

我无法弄清楚,到目前为止,没有任何文章等对我有帮助。除了我已经做过的事情外,他们什么都没有解释。自动装配JdbcTemplate等...

该语句也是有效的,因为当我在MysqL db上手动执行此查询时,得到的结果为1。 (仅1个条目)

在所有这些小时之后,这几乎是一个很大的问号。

我迫切需要帮助...考虑^^

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)