我正在尝试使用 SpringBoot 和 Postgres 数据库开发 Web 应用程序。但是,在连接到应用程序时,我收到错误“无法确定合适的驱动程序类” 根据旧帖子中的建议,我尝试使用不同版本的 jdbc 驱动程序,并尝试手动为 NamedParameterJdbcTemplate 创建 bean。我还验证了库是否存在并且可以从 Java 代码访问,并且这些库是否存在于类路径中。但它仍然给出同样的问题。 我正在使用 gradle 将所有 jar 导入构建路径。
这里是代码的 git 存储库: https://github.com/ashubisht/sample-sbs.git
Gradle依赖代码:
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-websocket")
compile("org.springframework.boot:spring-boot-starter-jdbc")
//compile("org.postgresql:postgresql")
compile("org.postgresql:postgresql:9.4-1206-jdbc42")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile group: 'junit', name: 'junit', version: '4.12'
}
构建Bean的代码
@Configuration
@PropertySource("classpath:application.properties")
public class Datasource {
@Value("${db.driverClassName}")
private String driverClass;
@Value("${db.url}")
private String url;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception{
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(source);
return namedParameterJdbcTemplate;
}
}
这是application.properties
server.port=8086
#spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
#spring.datasource.username=postgres
#spring.datasource.password=password
#spring.datasource.platform=postgresql
#spring.jpa.hibernate.ddl-auto=create-drop
db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/testdb
db.username=postgres
db.password=password
对我来说,问题在于 postgresSql 的拼写错误
只有一个,
更换
与
spring.datasource.url=jdbc:postgresql://localhost:5432/databaseName
还要检查休眠方言上的相同内容,
更换
PostgresSQLDialect
与 PostgreSQLDialect
通过创建两个 bean 解决了该问题。为 DataSource 和 NamedParameterJdbcTemplate 创建单独的 bean。
@Bean
public DataSource dataSource(){
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
return source;
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource());
return namedParameterJdbcTemplate;
}
请尝试一下
spring.r2dbc.url=r2dbc:postgresql://ip:port/datafeed?currentSchema=user_management
spring.r2dbc.username=username
spring.r2dbc.password=12345
spring.r2dbc.driver=postgresql
希望能帮到你!
有同样的问题。 我的解决方案是将
application.properties
文件扩展名更改为 application.yml
我遇到了同样的错误。当您安装 sts 版本 3 时会发生这种情况。 我通过尝试和错误方法找到了解决这个问题的方法。 发生此错误的原因是应用程序属性与服务器之间的连接不可用。我通过将应用程序属性中的端口号更改为 9090 来了解这一点,后来在运行应用程序时,控制台显示默认端口号 8080。 因此,您应该 Maven clean 并 Maven 构建您的 Spring Boot 应用程序。 完成上述步骤后,您的应用程序将作为 Spring Boot 应用程序正常运行,数据库将连接并且应用程序将启动。
对我来说,错误是
配置数据源失败:未指定“url”属性,无法配置嵌入数据源。 原因:未能确定合适的驱动类
行动: 考虑以下: 如果您想要嵌入式数据库(H2、HSQL 或 Derby),请输入 它在类路径上。 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有活动配置文件)。
问题是缺少个人资料 所以我在类路径中添加了以下内容并且它起作用了
spring.profiles.active=dev
此外,问题可能是因为您的项目中缺乏
jdbc
依赖性。如果您使用 maven
添加到 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
就我而言,我只需在 pom.xml 中添加依赖项,我以为我已经添加了,但事实并非如此。 将其添加到 pom.xml 的依赖项部分
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>