数据源配置异常

先上异常信息
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-31 18:53:44.137 ERROR 15044 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

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).
针对如上异常原因有以下几点
  1. 数据源配置不对,需要在application.properties增加如下数据源,注意二选一

    # mysql数据库连接 spring boot 2.0(内置jdbc5驱动)
    #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #spring.datasource.url=jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8&useSSL=false
    #spring.datasource.username=root
    #spring.datasource.password=root
    # mysql数据库连接 spring boot 2.1及以上(内置jdbc8驱动)
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=root
    
  2. 在编译后的target目录中配置数据源的application.properties文件位置不对,把资源文件放到classes目录下就好啦

  3. 使用--spring.config.location指定的配置文件的位置
    ① 命令方式
    java -jar xxx.jar --spring.config.location=D:\a\b\application.properties,D:\a\b\logback-spring.xml

    ② idea开发工具参数形式
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中实现全局事务配置数据源配置可以通过整合 Spring Boot 和 Spring 的事务管理机制以及使用 Spring Boot 自带的数据库连接池实现。 以下是一个简单的示例,展示如何在 Spring Boot 中配置全局事务和数据源: 1. 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</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-actuator</artifactId> </dependency> ``` 2. 在 application.properties 文件中添加以下配置: ``` spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root # JPA 配置 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update # 全局事务配置 spring.transaction.default-timeout=60 spring.transaction.rollback-on-commit-failure=true spring.transaction.rollback-on-rollback-failure=true ``` 3. 在您的代码中添加以下注释: ``` @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) ``` 这将确保在事务方法中执行的所有操作都将在单个事务中进行,并在发生异常时回滚。 4. 在您的代码中使用 Spring Boot 自带的 JdbcTemplate 或 JPA 进行数据库操作。 ``` @Autowired private JdbcTemplate jdbcTemplate; public void insert() { jdbcTemplate.update("INSERT INTO users (name, age) VALUES (?, ?)", "Tom", 20); } ``` 或者 ``` @Autowired private UserRepository userRepository; public void insert() { User user = new User(); user.setName("Tom"); user.setAge(20); userRepository.save(user); } ``` 以上就是在 Spring Boot 中配置全局事务和数据源的示例。请注意,这只是一个简单的示例,实际实现可能会更复杂,具体实现取决于您的应用程序需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值