Spring Boot 连接数据库问题
由IDEA直接快速搭建的Spring Boot项目结构
启动类SbtestApplication.java
package com.example.sbtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SbtestApplication {
public static void main(String[] args) {
SpringApplication.run(SbtestApplication.class, args);
}
}
测试类SbtestApplicationTests.java
package com.example.sbtest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@SpringBootTest
class SbtestApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
}
@Test
public void test(){
jdbcTemplate.
List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from testtable");
System.out.println("query result is " + result.size() );
System.out.println("success");
}
}
application.properties文件
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring //数据库地址
spring.datasource.username=root //username
spring.datasource.password=root //password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver //Mysql驱动
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sbtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sbtest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
**因为是第一次建SpringBoot项目,创建的时候多点了很多没有用的依赖,其实下面的就够用了
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sbtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sbtest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
报错无法注入
总体来说其实问题不少
首先来解决第一个问题 - 无法生成对应的类,也就是说无法注入JdbcTemplate
JdbcTemplate是Spring框架自带的对JDBC操作的封装,及操作数据源,而启动类中设置了注解@SpringBootApplication(exclude = DataSourceAutoConfiguration.class),exclude = DataSourceAutoConfiguration.class使得禁止springboot自动注入数据源配置,因此无法自动注入,将括号中删去就可以解决加载问题
至此已经成功加载了
但是目前仅仅只是注入了JdbcTemplate,调试查看可以看到其实并没有连接成功
报错显示数据库连接异常,必须要客户端插件验证,这个其实是MySQL版本造成的,我查到本机的MySQL版本是5.0.15-nt,而SpringBoot默认的MySQL版本过高。在mysql5中,jdbc连接mysql5是com.mysql.jdbc.Driver,而mysql6以及以上是com.mysql.cj.jdbc.Driver,并且在url之后要指定时区。
我pom.xml中的MySQL依赖没有设置版本号,因此是Spring默认的高版本,而application.properties中使用的驱动是mysql5的低版本,所以会确实必要的字段。
在pom.xml中设置mysql的驱动版本号,更新后等下载完毕
总结:
1、需要读取数据源的时候不能在启动类中设置@SpringBootApplication(exclude = DataSourceAutoConfiguration.class),将会导致无法注入
2、MySQL低版本和高版本的驱动不同,并且url写法也不同,mysql6以上需要在url中指定时区,如spring.datasource.url=jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
外传:
@SpringBoot注释可以指定启动类,有时测试时会有些方法没有运行,但是直接启动时却可以运行,这就是没有设置启动类,需要进行如下设置,设置指定的启动类,并希望将测试在Spring环境下进行
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {启动类.class})
测试类 {
}
想使用RunWith时发现无法导入,得知是pom.xml的问题;
maven管理依赖中会进行依赖传递,而pom.xml设置中允许使用exclusions将某些不需要同时下载的jar依赖排除
我刚刚的pom中使用exclusions将junit排除了出去,因此不能导入org.junit包,无法使用RunWith,修改如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
至此已经可以使用@RunWith和@SpringBootTest设置指定的启动类了,需要将原来的org.junit.jupiter.api.Test包删除,导包
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;