springboot configuration jpa

  Configuration

  pom-dependent

  org.springframework.boot

  spring-boot-starter-aop

  application.xml configuration

  #jpa Configuration

  spring.jpa.properties.hibernate.hbm2ddl.auto=update

  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

  spring.jpa.show-sql= true

  spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

  Note that the last line is used to define the naming policy, if not set, will use the default naming strategy, such as when construction of the table, will have a hump named converted to lowercase, and split with a line such as: the entity class name: userCommon, mapped to a table name is user-common

  Use

  Use jpa There are two ways, one is to use annotations, another is the use configuration files, these two can be achieved, but it is recommended to use notes, because more concise in this way, and is a major trend, the following describes comment The way.

  In entity classes mapped to add the following annotations

  @Entity: indicates that the class is an entity class, after adding the annotation can be scanned to jpa

  @Table: You can customize the table name

  We need to know that the above two lines of notes has been completed jpa mappings, declared after the entity class, which is the default properties of the class will be mapped to the database, but the default is often not meet our requirements, for example, named length, the primary key index are required to specify their own, so the following describes some common notes

  Extended Notes

  In the Properties Add comment

  Primary key:

  @Id: generally used to declare a primary key

  @GeneratedValue (strategy = GenerationType.IDENTITY): primary key is set to the self-energizing

  General Property: Wuxi hospital gynecological examination http://www.87554006.com/

  @column: You can customize the column names or other defined data types

  比如@column(name=“content”, columnDefinition = “longtext”)

  @Transient: no mapping database, similar to the exclusion of

  Page directly formatted value type, mainly for the date type

  @Temporal(TemporalType.DATE):如1994-05-06

  @Temporal(TemporalType.TIME):20:46:13

  @Temporal(TemporalType.TIMESTAMP):1994-05-06 20:46:13

  The only index

  @Table(name = “表名”, uniqueConstraints = {

  @UniqueConstraint (name = "Index Name", columnNames = { "Field 1", "Field 2"})

  }),

  Plus index table index

  @Table(name = “表名”,indexes = {

  @Index (name = "index name", columnList = "field name")

  })

  Database initialization

  Initialize the database often is often used, we created a database of data generally requires some initialization, configuration is as follows

  Increase application.properties profile properties

  spring.datasource.data=classpath:database/data.sql

  spring.datasource.sql-script-encoding=utf-8

  spring.datasource.initialization-mode=ALWAYS

  Creating data.sql file, it will execute the sql file when the project started, if the data item has been initialized, it may be the last line of the property instead NEVER ALWAYS


Guess you like

Origin blog.51cto.com/14503791/2437036