SpringBoot-- database management and migration (LiquiBase)

  With the development of the accumulation of time, a project will be growing, but the table structure more and more complex to manage, especially when you want to split a project into multiple A small items, the table structure demolished Branch consume a lot of energy; LiquiBase If you are using the database management, it will greatly enhance the efficiency of migration, or to just split the project as an example, if you use Liquibase, you only need to specify the module list file migration that is gone can.

  The next step is to use Springboot achieve Liquibase.

  1, import-dependent

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>

  2, configuration items

spring:
  profiles:
    active: test
  datasource:
    url: jdbc:mysql://***.76:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
    username: root
    password: ***
  liquibase:
    enabled: true
    change-log: classpath:/db/changelog/db.changelog-master.yml

  3, the definition db.changelog-master.yml

databaseChangeLog:
  # 支持 yaml 格式的 SQL 语法
  - changeSet:
      id: 1
      author: Levin
      changes:
        - createTable:
            tableName: person
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: first_name
                  type: varchar(255)
                  constraints:
                    nullable: false
              - column:
                  name: last_name
                  type: varchar(255)
                  constraints:
                    nullable: false

  - changeSet:
      id: 2
      author: Levin
      changes:
        - insert:
            tableName: person
            columns:
              - column:
                  name: first_name
                  value: Marcel
              - column:
                  name: last_name
                  value: Overdijk
  # Also supports dependent on external SQL files (TODO personally like this)
   - changeSet: 
      the above mentioned id: 3 
      author: Levin 
      Changes:
         - SQLFile: 
            encoding: utf8 
            path: the CLASSPATH: db /changelog/sqlfile/test1.sql

  4, due to the dependence on external files are also supported, once a new sql file

INSERT INTO `person` (`id`, `first_name`, `last_name`) VALUES ('3', 'test', 'test2');

  5, run the project

  It can be seen at the start of the project, liquibase built two tables DATABASECHANGELOG, DATABASECHANGELOGLOCK, used to store the log table structure changes, creating a person table based on the profile and stored data.

 

Guess you like

Origin www.cnblogs.com/liconglong/p/11728767.html