Spring Data Jpa sporing Boot postgresql not creating配置文件

bkhjykvo  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(77)

所以我有一个spring Boot 应用程序,并配置了postgresql,如下所示

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql:dburl
spring.datasource.username=admin
spring.datasource.password=XXX

字符串
但我在应用程序启动时遇到失败,因为没有创建一个临时文件

No qualifying bean of type 'javax.sql.DataSource' available


我决定创建一个虚拟bean:

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {
        // Create and configure the DataSource here
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.hibernate.dialect.PostgreSQLDialect");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/your-database");
        dataSource.setUsername("your-username");
        dataSource.setPassword("your-password");

        return dataSource;
    }
}


这似乎建立了一种联系。
但是我不明白为什么我不能从属性中创建一个数组。

cig3rfwq

cig3rfwq1#

在给定的示例中,您缺少Postgres的端口号。
使用spring Boot 连接到Postgres非常简单。请尝试以下操作-
pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>

字符串
application.properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/my-db
spring.datasource.username=postgres
spring.datasource.password=root

weylhg0b

weylhg0b2#

你能给我们展示一下你的依赖吗?如果使用Maven作为你的构建工具,请检查你是否有这个(如果没有,请添加,然后重试):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

字符串

相关问题