1. 程式人生 > 其它 >06_資料代理

06_資料代理

整合SpringBoot-JPA

  1. 匯入依賴:

    1. spring-boot-starter-data-jpa
    2. mysql-connector-java
  2. 配置資料庫連線,jpa的表對映關係等

    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/lgf
        driver-class-name: com.mysql.jdbc.Driver
      jpa:
        database: mysql
        show-sql: true
        generate-ddl: true
        hibernate:
          ddl-auto: update
          naming:
            physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    logging:
      level:
        com.cth.dao: debug
    
  3. 實體類配置對應欄位名稱

    import javax.persistence.*;
    @Entity
    @Table()
    public class Users {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Integer id;
        private String name;
        private Integer age;
        private Long phone;
    }
    
  4. dao介面繼承 JpaRepository 介面

    public interface UserDao extends JpaRepository<Users,Integer> {}
    
  5. 正常的MVC操作流程