| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2778 人关注过本帖
标题:springboot的demo,测试的时候报datasource的url错误,求帮忙!!
只看楼主 加入收藏
一神经彬
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
springboot的demo,测试的时候报datasource的url错误,求帮忙!!
新建的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编辑过]

搜索更多相关主题的帖子: Failed class exception version boot 
2020-02-24 10:58
一神经彬
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-1
收藏
得分:0 
有人吗?求助
2020-02-25 09:19
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:20 
给你的
程序代码:
<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
</dependency>

添加版本号 要8.X以上
2020-02-25 10:42
一神经彬
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-1
收藏
得分:0 
回复 3楼 hhwz
自动下载的依赖就是8.0.18的,我加了version还是报那样的错
2020-02-26 19:22
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:0 
就配置文件来说没有什么问题。
那问题有可能是 没有加载到配置文件
看看文件是否放在配置文件区域

HikariDataSource?驱动不对?
2020-02-27 10:48
一神经彬
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-11-1
收藏
得分:0 
回复 5楼 hhwz
是对的,我把代码换到IDEA就能运行了,我透,搞不懂
2020-02-28 10:41
快速回复:springboot的demo,测试的时候报datasource的url错误,求帮忙!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026987 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved