Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot integrates Liquibase

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to integrate SpringBoot Liquibase, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

There are two cases of integration.

Automatically execute the script when you start the project. If you add a new Liquibase script, you need to restart the project to execute the script.

You can also make it execute the script manually through plug-ins or instructions when the project is not started.

Integration either integrates only 1, or integrates 1 and 2 together.

If only integrate 2 and not integrate 1, liquibase-related bean Times errors will be generated when the project is started.

Integration 1

Introduce Maven dependency

Here you import the package for Liquibase and the package for connecting to the MySQL database

Org.liquibase liquibase-core 3.6.2 mysql mysql-connector-java runtime

Configure in the SpringBoot configuration file

For liquibase-related configuration, you only need to configure the location of the root changelog. Database-related configuration liquibase will get the following datasource by itself. Note that url needs to be added with time zone serverTimezone.

Spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai liquibase: change-log: classpath:/db/liquibaseChangeLogFile.xml

Configure liquibaseChangeLogFile.xml

Also need to pay attention to the path of the xml file, according to the above configuration, the file in src/main/resources/db/liquibaseChangeLogFile.xml

Configure changeLog.xml

According to the above configuration, the changeLog file should be placed in src/main/resources/db/changelog/

Let's simply write a changelog to test

Consolidation:

After configuring according to the above process, when starting the project, it will execute the unexecuted Lisquibase script ~

Integration 2

Sometimes if you want to execute the Liquibase script without starting or restarting the project, you should integrate 2.

Introduction of Maven plug-in

The version of the plug-in introduced is the same as the version number of the dependent package above, but I have not studied what will happen to the inconsistency. Interested partners can try it, but is it not uncomfortable to see the inconsistency? in the configuration tag here, configure the configuration file of the liquibase-related information to be read, and pay attention to the path.

Org.liquibase liquibase-maven-plugin 3.6.2 src/main/resources/liquibase.properties false

Configure Liquibase.properties

Note that the path of the configuration file should be the same as that declared when configuring the Maven plug-in, that is, src/main/resources/liquibase.properties is configured with the following five attributes. Note that url needs to add the time zone serverTimezone. Note that the path of the changeLogFile entry is a relative path, and the configuration will be shown below.

# the configuration is integrated in the yml configuration file. If you want to execute the liquibase script when you don't start, you can't save the configuration! Driver-class-name=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaiusername=rootpassword=rootchangeLogFile=db/liquibaseChangeLogFile.xml

Configure liquibaseChangeLogFile.xml

It was already configured when integrating 1.

Configure changeLog.xml

According to the above configuration, the changeLog file should be placed under src/main/resources/db/changelog/ to write another liquibase script

Test integration

Mvn clean and install first! Otherwise, we can't find the xml file we wrote.

Find liquibase:update in the maven plug-in and double-click ~

Add: let's share a sample code to explain how to integrate liquibase with spring boot.

1. Add dependency

Org.liquibase liquibase-core 3.8.3 org.liquibase liquibase-maven-plugin 3.8.3 org.liquibase liquibase-maven-plugin 3.8.3 src/main/resources/db/liquibase.properties

two。 Create liquibase.properties content as follows

Url=jdbc:postgresql://127.0.0.1:5432/ems_factorymodeldbusername=namepassword=passworddriver=org.postgresql.DriveroutputChangeLogFile=src/main/resources/liquibase-outputChangeLog.xml

3. Create db.changelog-master.xml

4. Increase configuration

# liquibasespring.liquibase.enabled=truespring.liquibase.change-log=classpath:db/db.changelog-master.xml Thank you for reading this article carefully. I hope the article "how to integrate SpringBoot into Liquibase" shared by the editor will be helpful to you. At the same time, I hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report