【发布时间】: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