@EqualsAndHashCode(callSuper = true)
@ConfigurationProperties(prefix = "datasource.tidb")
@Component
@Data
@Slf4j
public class TidbSource extends JDBCCacheSource {
private static DataSource dataSource = null;
private String database;
private String driverClassName = "com.mysql.jdbc.Driver";
private String getJdbcUrl() {
return String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true&autoReconnect=true&failOverReadonly=false",
this.host, this.port, this.database);
}
@PostConstruct
public void init() {
Properties properties = new Properties();
properties.setProperty("url", this.getJdbcUrl());
properties.setProperty("username", this.username);
properties.setProperty("password", this.password);
properties.setProperty("driverClassName", this.driverClassName);
try {
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
throw new SqlExecuteException("tidb获得jdbc连接失败");
}
}
@Override
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new SqlExecuteException("tidb获得jdbc连接失败");
}
}
@Override
public void createTable(String tableName, List columns, String comment) {
//获得创表语句
String createTableSql = buildCreateTableSql(tableName, columns, comment);
Connection connection = this.getConnection();
Statement statement = null;
//执行SQL
try {
statement = connection.createStatement();
int count = statement.executeUpdate(createTableSql);
log.info(String.format("tidb在%s创建表%s", DateTime.now().toDateStr(), tableName));
statement.close();
} catch (SQLException e) {
e.printStackTrace();
throw new SqlExecuteException(String.format("tidb创建表%s失败", tableName));
} finally {
closeStatement(statement);
}
}
@Override
public void insertTable(String tableName, List columns, List