【问题标题】:Configuring liquibase for spring project为 spring 项目配置 liquibase
【发布时间】:2015-08-03 16:51:35
【问题描述】:

我有下一个 db.changelog-master.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
    <changeSet id="19082014-1" author="autor">
        <sql>
            CREATE TABLE testings (
            id character varying(80) NOT NULL
            )
        </sql>
    </changeSet>
</databaseChangeLog>

我的 Spring 配置文件如下所示:

@Configuration
@PropertySource("classpath:user.properties")
public class LiquibaseConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource()  {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

            dataSource.setDriverClassName(env.getProperty("spring.datasource.drivername"));
            dataSource.setUrl(env.getProperty("spring.datasource.url"));
            dataSource.setUsername(env.getProperty("spring.datasource.username"));
            dataSource.setPassword(env.getProperty("spring.datasource.password"));

        return dataSource;
    }

    @Bean
    public SpringLiquibase liquibase()  {
        SpringLiquibase liquibase = new SpringLiquibase();

        liquibase.setDataSource(dataSource());
        liquibase.setChangeLog("classpath:db.changelog-master.xml");

        return liquibase;
    }
}

所以我期待应该创建新表。但是我的数据库仍然是空的。我在日志中看不到任何错误,我做错了什么?我应该更改我的 spring 配置类吗?

【问题讨论】:

  • 从标签来看,您使用的是 Spring Boot。为什么要创建自己的 DataSource 和 Liquibase bean,而不是让 Boot 为您创建和配置它们?
  • 因为在这种情况下 Spring Boot 将使用默认的 yaml 文件格式,但我不喜欢这种格式。
  • 您可以在 Spring Boot 中使用 XML 配置 Liquibase,而无需声明自己的 bean。只需将liquibase.changeLog=classpath:db.changelog-master.xml 添加到您的application.properties

标签: java spring spring-boot liquibase


【解决方案1】:

好的,我想我解决了这个问题。每次运行脚本时,都应该更改 db.changelog-master.xml 文件中的 id 标签。

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2019-03-02
    • 2017-05-20
    • 2022-06-12
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2021-03-11
    相关资源
    最近更新 更多