1. 程式人生 > >三、SpringBoot JPA 配置Hibernate

三、SpringBoot JPA 配置Hibernate

文章目錄


#步驟
##1.maven新增支援
       <!-- Spring Boot JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>

##2.編寫application.properties

spring.datasource.url: jdbc:mysql://localhost/beauty
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

##3.寫entity

@Entity
@Table(name = "user")
public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id = 0L;
    private String name;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

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

##4.寫JPARepository

public interface UserRepository extends JpaRepository<UserEntity,Integer>{
}
  • **注意事項:**測試之前一定要先確認配置檔案的中資料庫檔案是存在的.user和password是正確的

博主開發的第三方CSDN客戶端.體驗很棒哦,快來體驗下載吧
在這裡插入圖片描述