import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 中间表创建策略配置
* @author qkj
*/
@ConfigurationProperties(prefix = "spring.activiti")
@Data
public class CustomTableProperties {
private boolean customTableRecreate = false;
}
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* 中间表创建策略 配置类
* @author qkj
*/
@Configuration
// 使得配置类生效
@EnableConfigurationProperties(CustomTableProperties.class)
// 配置类作用于配置文件中
@ConditionalOnProperty(name = "spring.activiti.custom-table-recreate",matchIfMissing = false)
public class CustomTableBeanConfiguration {
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Description: 自动创建中间表表配置类 (activiti内置表会自动创建 这里是创建我们的中间表)
* date: 2022/3/24 10:39
*
* @author: qkj
* @since JDK 11
*/
@Configuration
public class CustomTableConfiguration {
@Autowired
private CustomTableProperties customTableProperties;
@Value("classpath:db/create/activiti.mysql.create.history.details.sql")
private Resource createSql;
@Value("classpath:db/recreate/activiti.mysql.recreate.history.details.sql")
private Resource reCreateSql;
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) throws SQLException {
final DataSourceInitializer initializer = new DataSourceInitializer();
// 设置数据源
initializer.setDataSource(dataSource);
ResultSet tables = dataSource.getConnection().getMetaData().getTables(null, null, "act_history_details_business", null);
// 表已存在且不需要重建
if (tables.next() && !customTableProperties.isCustomTableRecreate()) {
return initializer;
}
initializer.setDatabasePopulator(databasePopulator());
return initializer;
}
private DatabasePopulator databasePopulator() {
final ResourceDatabasePopulator popular = new ResourceDatabasePopulator();
if (!customTableProperties.isCustomTableRecreate()) {
popular.addScripts(createSql);
} else {
popular.addScripts(reCreateSql);
}
return popular;
}
}
资源路径:(里面存放建表语句)
yml配置: