1. 程式人生 > >使用springboot jpa(一) 連線postgre資料庫

使用springboot jpa(一) 連線postgre資料庫

一、maven依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId
>
org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId
>
<scope>test</scope> </dependency> </dependencies>

二、配置檔案

#資料庫
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5433/spring_boot?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false  
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

三、建立實體類

@Entity
public class Arms {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer type; //種類
    private Integer quality; //品質

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getQuality() {
        return quality;
    }

    public void setQuality(Integer quality) {
        this.quality = quality;
    }
}

四、執行,資料庫中自動生成arms表
這裡寫圖片描述