Skip to content

Latest commit

 

History

History
49 lines (44 loc) · 1.79 KB

File metadata and controls

49 lines (44 loc) · 1.79 KB

JPA

Spring JPA

  • JPA的完整配置示例:

    spring:
      jpa:
        show-sql: true
        database: mysql
        hibernate:
          ddl-auto: create
          naming:
            physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        properties:
          hibernate:
            dialect: org.hibernate.dialect.MySQL5Dialect
      datasource:
        url: jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8
        username: admin
        password: ******
        driver-class-name: com.mysql.jdbc.Driver
        testWhileIdle: true
        validationQuery: SELECT 1
    
  • JPA配置自动创建表时,按指定的表名及列名创建(指定大小写),需要用如下策略:spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

  • spring.jpa.hibernate.ddl-auto: none, vlidate, update, create, create-drop

    • validate: validate the schema, makes no changes to the database.
    • update: update the schema.
    • create: creates the schema, destroying previous data.
    • create-drop: drop the schema at the end of the session
    • none: 生产环境应该用none
    • 对于hsqldb, h2, derby默认值为create-drop, 其他数据库的默认值为none
  • 对于CreateDate或UpdateDate这样的字段,如果想直接使用数据库的时间可以用如下方法:

        @CreationTimestamp
        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="CREATE_DATE", insertable = false, updatable = false)
        private Date createDate;
    
        @UpdateTimestamp
        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="UPDATE_DATE", insertable = false, updatable = false)
        private Date updateDate;
    

    Date需要使用java.util.Date