据说我们可以使用以下spring bean运行多个数据源
public enum OperatorType {
IS_ONE_OF("IS_ONE_OF", 1,
Collections.singletonList(DataType.ALL)),
IS_NOT_ONE_OF("IS_NOT_ONE_OF", 2,
Collections.singletonList(DataType.ALL)),
ENDS_WITH("ENDS_WITH", 3,
Collections.singletonList(DataType.STRING)),
DOES_NOT_ENDS_WITH("DOES_NOT_ENDS_WITH", 4,
Collections.singletonList(DataType.STRING)),
STARTS_WITH("STARTS_WITH", 5,
Collections.singletonList(DataType.STRING)),
DOES_NOT_START_WITH("DOES_NOT_START_WITH", 6,
Collections.singletonList(DataType.STRING)),
MATCHES("MATCHES", 7,
Collections.singletonList(DataType.STRING)),
DOES_NOT_MATCH("DOES_NOT_MATCH", 8,
Collections.singletonList(DataType.STRING)),
CONTAINS("CONTAINS", 9,
Collections.singletonList(DataType.STRING)),
DOES_NOT_CONTAIN("DOES_NOT_CONTAIN", 10,
Collections.singletonList(DataType.STRING)),
GREATER_THAN("GREATER_THAN", 11, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
GREATER_THAN_OR_EQUAL_TO("GREATER_THAN_OR_EQUAL_TO", 12, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
LESS_THAN("LESS_THAN", 13, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
LESS_THAN_OR_EQUAL_TO("LESS_THAN_OR_EQUAL_TO", 15, Arrays.asList(DataType.INTEGER,DataType.DOUBLE)),
AFTER("AFTER", 15,
Collections.singletonList(DataType.DATE)),
BEFORE("BEFORE", 16,
Collections.singletonList(DataType.DATE));
private final int value;
private final String key;
private final List<DataType> supportedtypes;
OperatorType(String key, int value, List<DataType> supportedtypes) {
this.value = value;
this.key = key;
this.supportedtypes = supportedtypes;
}
public int getValue() {
return this.value;
}
public String getKey() {
return this.key;
}
public List<DataType> getSupportedtypes() {
return this.supportedtypes;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
@JsonCreator
public static OperatorType create(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
for (OperatorType v : values()) {
if (v.getKey().equalsIgnoreCase(key)) {
return v;
}
}
throw new IllegalArgumentException();
}
public static OperatorType fromValue(Integer value) {
for (OperatorType type : OperatorType.values()) {
if (value == type.getValue()) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
public static OperatorType fromValue(String key) {
for (OperatorType type : OperatorType.values()) {
if (type.getKey().equalsIgnoreCase(key)) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum type supplied");
}
或使用以下pom.xml配置文件
public static Map<DataType, List<OperatorType>> buildmap() {
Map<OperatorType, List<DataType>> map = new HashMap<>();
Arrays.stream(OperatorType.values()).collect(groupingBy(i -> OperatorType.fromValue(i.getKey()),
mapping(i -> i.getSupportedtypes(), Collectors.toList()))).entrySet().forEach(entry ->
{
map.put(entry.getKey(),
entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList()));
});
return map.entrySet().stream().flatMap(e -> e.getValue().stream().map(v -> new SimpleEntry<>(v, e.getKey())))
.collect(
Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
}
如果我们需要以混合顺序运行脚本怎么办?在我的情况下,我需要从<bean id="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource1" />
<property name="changeLog" value="classpath:db1-changelog.xml" />
</bean>
<bean id="liquibase2" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource2" />
<property name="changeLog" value="classpath:db2-changelog.xml" />
</bean>
运行一个脚本,然后再运行 <profiles>
<profile>
<id>db1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
<profile>
<id>db2</id>
<properties>
<liquibase.url>jdbc:h2:target/db2/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
</profiles>
,然后再运行dataSource1
,然后再运行dataSource2
答案 0 :(得分:0)
我们可以通过使用相同的数据源多次使用不同的bean注入来完成此操作
<bean id="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource1" />
<property name="changeLog" value="classpath:db1-changelog1.xml" />
</bean>
<bean id="liquibase2" depends-on="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource2" />
<property name="changeLog" value="classpath:db2-changelog1.xml" />
</bean>
<bean id="liquibase3" depends-on="liquibase2" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource1" />
<property name="changeLog" value="classpath:db1-changelog2.xml" />
</bean>
<bean id="liquibase4" depends-on="liquibase3" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource2" />
<property name="changeLog" value="classpath:db2-changelog2.xml" />
</bean>
将这些bean定义放在单独的xml配置文件中并将其导入
是个好主意