【问题标题】:How can I use liquibase in Spring Project如何在 Spring Project 中使用 liquibase
【发布时间】:2015-09-25 09:24:39
【问题描述】:
我正在尝试学习 liquibase。但我无法在示例中应用步骤。您能逐步告诉我如何将表或列添加到我的数据库并查看更改吗?
我的项目是一个 Spring MVC 项目,我使用 Maven、Hibernate、PostgreSQL 并以编程方式更改数据库。
【问题讨论】:
标签:
spring
postgresql
maven
model-view-controller
liquibase
【解决方案1】:
您将需要 liquibase 和 hibernate jar。假设您有一个 pojo 类 person 具有 Id,Name,Gender 属性。创建这些属性的 getter 和 setter。
你需要创建liquibase文件(db-changelog.xml)
例如:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
<changeSet author="rover" id="123456789-1">
<createTable tableName="PERSON">
<column autoIncrement="true" name="PERSON_ID" type="BIGINT">
<constraints nullable="false" primaryKey="true" />
</column>
<column name="NAME" type="VARCHAR(255)" />
<column name="GENDER" type="VARCHAR(2)" />
</createTable>
</changeSet>
</databaseChangeLog>
别忘了在你的 bean 中添加 liquibase bean
<bean id="LiquibaseUpdater" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
</bean>
您还需要添加 spring/hibernate bean。
【解决方案2】:
在pom.xml中添加如下maven依赖,
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>2.0.3</version>
</dependency>
将以下 liquibase bean 添加到 applicationcontext.xml
<bean id="LiquibaseUpdater" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
</bean>
在类路径中添加 db-changelog.xml,
例如:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
<changeSet author="John" id="add-column" onValidationFail="MARK_RAN" failOnError="false">
<preConditions onFail="MARK_RAN">
<or>
<not>
<columnExists tableName="TABLENAME" columnName="NEWCOLUMN"/>
</not>
</or>
</preConditions>
<addColumn tableName="TABLENAME">
<column name="NEWCOLUMN" type="VARCHAR(50)"/>
</addColumn>
</changeSet>
【解决方案3】:
在 Spring Framework 应用程序上设置 Liquibase。
- 添加依赖项
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.6.2</version>
</dependency>
- 添加豆
@Bean
public SpringLiquibase springLiquibase(DataSource dataSource) {
SpringLiquibase springLiquibase = new SpringLiquibase();
springLiquibase.setDataSource(dataSource);
springLiquibase.setChangeLog("db/changelog/db.changelog-master.xml");
return springLiquibase;
}
其中“db/changelog/”是更改日志文件的默认和推荐位置。
- 将“db.changelog-master.xml”文件添加到资源下的“db/changelog/”文件夹中。至少添加以下内容:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<includeAll path="db/migrations"/>
</databaseChangeLog>
- 将一个变更日志文件添加到“db/migrations”文件夹中,例如changelog_base.sql。它的内容可以是简单的 SQL 语句,也可以只是 SQL 注释。
当构建系统默认不包含所有资源文件时,您可能需要对其进行配置。
并且应用程序还需要通过 Spring 正确配置和设置 DB 连接。
现在运行应用程序并查找输出日志。如果一切正常,那么 liquibase 应该会有一些输出,例如:
25-Jan-2022 14:36:53.088 INFO [localhost-startStop-12] liquibase.database.null Set default schema name to public
25-Jan-2022 14:36:53.217 INFO [localhost-startStop-12] liquibase.lockservice.null Successfully acquired change log lock
25-Jan-2022 14:36:53.332 INFO [localhost-startStop-12] liquibase.changelog.null Reading resource: db/migrations/changelog_base.sql
25-Jan-2022 14:36:53.453 INFO [localhost-startStop-12] liquibase.changelog.null Reading from public.databasechangelog
25-Jan-2022 14:36:53.506 INFO [localhost-startStop-12] liquibase.lockservice.null Successfully released change log lock
检查数据库 - 它应该有新表“databasechangelog”和“databasechangeloglock”。以及databasechangelog表中的一些内容。