20191128 Spring Boot official document learning (9.10)

9.10. Database initialization

You can use different ways to initialize SQL database, depending on what the stack yes. Of course, if the database is a separate process, you can also perform a manual. We recommend the use of a single mechanism for pattern generation.

9.10.1. Initialize the database using JPA

JPA has a function for generating a DDL, which can be set to run against the database at startup. This is controlled by two external properties:

  • spring.jpa.generate-ddl (Boolean value) to open and close the function, and independent suppliers.
  • spring.jpa.hibernate.ddl-auto(Enumeration) is a function Hibernate can be more finely controlled behavior.

9.10.2. Initialize the database using Hibernate

You can set up spring.jpa.hibernate.ddl-autoas a standard of clear and Hibernate property nonevalues: validate, update, , create, and create-drop. Spring Boot your database is embedded to select the default value for you. If the manager does not detect any pattern or by default in all other cases none create-drop. By looking at Connectioncan detect embedded database type. hsqldb, h2 and embedded derby, while others are not. When the in-memory database is converted to "real" database from, please do not make assumptions about the existence of the new platform and data tables. You must explicitly set ddl-autoor use one of the other mechanisms to initialize the database.

You can enable org.hibernate.SQLto create recorder output mode. If debug mode is enabled, this will be done for you automatically.

Further, if Hibernate mode from scratch (i.e., if the ddl-autoproperty is set to createor create-drop), at the time of startup will be performed in the root directory named classpath import.sqlfiles. If you are careful enough, which is useful for demonstration and testing, but may not want to appear in the class path production environment. This is a Hibernate function (nothing to do with Spring).

9.10.3. Initialize the database

Spring Boot can automatically create your DataSource model (DDL scripts) and initialize (DML script). Respectively, from which the standard root class path locations schema.sqland data.sqlloading SQL. Further, Spring Boot processing schema-${platform}.sqland data-${platform}.sqlfiles (if present), wherein platformthe value spring.datasource.platform. This allows you to switch to a database-specific script when necessary. For example, you can choose to set it to the database vendor name (hsqldb, h2, oracle, mysql , postgresql , etc.).

Spring Boot automatically creates an embedded mode DataSource. You can use spring.datasource.initialization-modethe properties to customize this behavior. For example, if you want to always initialized, regardless of the type of DataSource how:

spring.datasource.initialization-mode =always

By default, Spring Boot Enable rapid-fail function Spring JDBC initialization procedure. This means that if the script causes an exception, then the application will not start. You can set spring.datasource.continue-on-errorto adjust the behavior.

In JPA-based application, you can choose to use Hibernate mode or create schema.sql, but you can not do both. If you are using schema.sql, be sure to disable spring.jpa.hibernate.ddl-auto.

9.10.4. Spring Batch initialize a database

If you are using Spring Batch, then it is the most popular database platforms with pre-packaged with the SQL initialization script. Spring Boot can detect your database type and execute scripts at startup. If you use an embedded database, this situation will occur by default. You can also think that it, as the following example to enable any database type as shown:

spring.batch.initialize-schema =always

You can also set up spring.batch.initialize-schema=neverclear close initialized.

9.10.5. Use advanced database migration tool

Spring Boot supports two higher levels of migration tools: Flywayand Liquibase.

Flyway perform database migration at startup

To automatically run at startup Flyway database migration, please org.flywaydb:flyway-coreadd it to your classpath.

Typically, migration is of the following form script V<VERSION>__<NAME>.sql(with <VERSION>underlined separate versions, for example, "1" or "2_1"). By default, they are located is named classpath:db/migrationfiles in the folder, but you can set by spring.flyway.locationsmodifying the position. It is one or more classpath:or a filesystem:comma-separated list position. For example, the following configuration script search path location and a default category / opt / migration directory:

spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration

You can also add a special {vendor}placeholder to use vendor-specific scripts. Assume the following:

spring.flyway.locations=classpath:db/migration/{vendor}

The foregoing configuration is not using db / migration, but according to the type of database (e.g., db/migration/mysqlfor MySQL) to be used to set the file folder. In the list of supported databases DatabaseDriverto provide in.

Migration can be written in Java. Flyway will use any implementation JavaMigrationbean automatic configuration.

FlywayPropertiesMost settings provide Flyway and small amounts of other properties that can be used to disable the migration or closed position check. If you need more control over the configuration, consider registering FlywayConfigurationCustomizerBean.

Spring Boot call Flyway.migrate()to perform database migration. If you want more control, provide implementations for FlywayMigrationStrategy@Bean of.

Flyway support SQL and Java callback. To put SQL callback, set a callback-based scripts classpath:db/migrationfolder. To use the Java-based callback, create one or more implementations Callbackof the bean. Any such bean will automatically register with the Flyway. It can be used @Orderor implemented Orderedto adjust the order. Also it can detect realized deprecated FlywayCallbackBean interface, but can not be used with Callback Bean.

By default, Flyway automatic automatic connection in your context ( @Primary) and the DataSource for migration. If you want to use other DataSource, you can create one and mark it as @FlywayDataSourcethe @Bean. If this does want two data sources, remember to create another data source and mark it as @Primary. In addition, you can set up spring.flyway.[url,user,password]to use external Flyway native DataSource property. Set spring.flyway.urlor spring.flyway.userenough to make Flyway use your own DataSource. If any of these three attributes are not set, then the use of the equivalent property spring.datasourcevalue.

You can also use Flyway provide data for a particular situation. For example, you can src/test/resourcesput the migration-specific tests, and only started to run when test them in your application. In addition, you can use a specific profile to customize the configuration spring.flyway.locationsto run only when active in certain specific migration profiles. For example, in application-dev.properties, you can specify the following settings:

spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration

With this setting, dev/db/migrationrun the migration if only in dev profile is active.

Liquibase perform database migration at startup

To automatically run Liquibase database migration, set when you start org.liquibase:liquibase-coreto add to your classpath.

By default, from db/changelog/db.changelog-master.yamlreading the main change log, but you can set spring.liquibase.change-logto change its position. In addition to YAML, Liquibase also supports JSON, XML and SQL change log format.

By default, Liquibase automatic assembly (@Primary) DataSource your and context for the migration. If you need to use other DataSource, you can create one and mark it as @LiquibaseDataSourcethe @Bean. If you do, and you want two data sources, remember to create another data source and mark it as @Primary. In addition, you can set up spring.liquibase.[url,user,password]to use external Liquibase native DataSource property. Set spring.liquibase.urlor spring.liquibase.userenough to make Liquibase use its own DataSource. If any of these three attributes are not set, then the use of the equivalent property spring.datasourcevalue.

See LiquibasePropertiesfor more information about the available settings, such as context, default and other architecture.

Guess you like

Origin www.cnblogs.com/huangwenjie/p/11947463.html