第一次用springboot做项目,这个错误看这很简单却让我找了两个多小时才解决。!!
1.照惯例写了一个测试是否连接数据库成功的测试类
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
@SpringBootTest
class SpringbootThymeleaf001ApplicationTests {
//测试数据库连接
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws Exception{
System.out.println("获取的数据库连接为:"+dataSource.getConnection());
}
}
一开始就测试连接了,连接成功。然后启动项目的时候出现了错误我才转回去又测试了一下连接,竟然报错了!!中途什么都没有更改!
2.解决方法
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
上面依赖换为下列依赖,成功解决!
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>