注册 登录
编程论坛 JAVA论坛

springboot的demo,测试的时候报datasource的url错误,求帮忙!!

一神经彬 发布于 2020-02-24 10:58, 2665 次点击
新建的springboot项目,测试的时候始终报datasource的url不正确,
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


找过好多方法都不行,实在搞不明白哪里有问题,求各位帮忙看看!

这是pom.xml
程序代码:
<properties>
        <java.version>1.8</java.version>
        <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
   

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <!-- 测试工具启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

这是applicationContext.properties
程序代码:
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/jpa_study?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql =true

mysql-connector-java-8.0.18.jar

这是respository
public interface UsersRepository extends JpaRepository<Users, Integer> {

}

这是JUnit测试
程序代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class UsersRepositoryTest {
   

    @Autowired
    private UsersRepository usersRepository;
   

    @Test
    public void testSave(){
        Users users = new Users("张三", 22, "北京武汉");
        this.usersRepository.save(users);
    }
}

然后启动测试,始终报这个错
程序代码:
Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

2020-02-24 09:21:42.371 ERROR 8952 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@1ab3a8c8] to prepare test instance [com.fbb.test.UsersRepositoryTest@749f539e]

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class






[此贴子已经被作者于2020-2-24 11:05编辑过]

5 回复
#2
一神经彬2020-02-25 09:19
有人吗?求助
#3
hhwz2020-02-25 10:42
给你的
程序代码:

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
</dependency>

添加版本号 要8.X以上
#4
一神经彬2020-02-26 19:22
回复 3楼 hhwz
自动下载的依赖就是8.0.18的,我加了version还是报那样的错
#5
hhwz2020-02-27 10:48
就配置文件来说没有什么问题。
那问题有可能是 没有加载到配置文件
看看文件是否放在配置文件区域

HikariDataSource?驱动不对?
#6
一神经彬2020-02-28 10:41
回复 5楼 hhwz
是对的,我把代码换到IDEA就能运行了,我透,搞不懂
1