com.taotao.boot.idgenerator.uid.config.UidGenConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-idgenerator Show documentation
Show all versions of taotao-boot-starter-idgenerator Show documentation
taotao-boot-starter-idgenerator
The newest version!
package com.taotao.boot.idgenerator.uid.config;
import com.taotao.boot.common.utils.log.LogUtils;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* uidgen配置类
*/
@ConditionalOnClass({MapperScan.class, SqlSession.class})
@ComponentScan(basePackages = {"com.taotao.boot.idgenerator.uid"})
//@Configuration(proxyBeanMethods = false)
//// 当存在某个类时,此自动配置类才会生效
//@ConditionalOnClass(value = {HelloService.class})
// 导入我们自定义的配置类,供当前类使用
@EnableConfigurationProperties(value = {UidGenProperties.class})
//@EnableConfigurationProperties(value = )
//// 只有非web应用程序时此自动配置类才会生效
//@ConditionalOnWebApplication
//判断atta.config.flag的值是否为“true”, matchIfMissing = true:没有该配置属性时也会正常加载
@MapperScan(value = {"com.taotao.boot.idgenerator.uid.worker.dao"})
//@ConditionalOnProperty(prefix = "atta.config", name = "flag", havingValue = "true", matchIfMissing = true)
public class UidGenConfig {
@Bean
@ConditionalOnBean(DataSource.class)
public DetectWorkNodeTable detectTable(DataSource dataSource) {
return new DetectWorkNodeTable(dataSource);
}
public static class DetectWorkNodeTable implements ApplicationRunner {
public static final String undoLogSql =
"""
CREATE TABLE IF NOT EXISTS WORKER_NODE (
ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',
HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',
PORT VARCHAR(64) NOT NULL COMMENT 'port',
TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',
LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',
MODIFIED TIMESTAMP NOT NULL COMMENT 'modified time',
CREATED TIMESTAMP NOT NULL COMMENT 'created time',
PRIMARY KEY(ID)
)
COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB;
""";
private final DataSource dataSource;
public DetectWorkNodeTable(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void run(ApplicationArguments args) throws Exception {
try {
dataSource.getConnection().prepareStatement(undoLogSql).execute();
} catch (SQLException e) {
LogUtils.error("创建[uid] WORKER_NODE表错误。", e);
}
}
}
}