mirror of
https://github.com/kerwincui/FastBee.git
synced 2025-10-15 04:30:46 +08:00
2022年5月24日14:34:18 增加初始化时创建数据库功能
This commit is contained in:
@@ -36,6 +36,8 @@ public class TDengineConfig {
|
|||||||
|
|
||||||
@Value("${spring.datasource.druid.tdengine-server.dbName}")
|
@Value("${spring.datasource.druid.tdengine-server.dbName}")
|
||||||
private String dbName;
|
private String dbName;
|
||||||
|
@Value("${spring.datasource.druid.tdengine-server.url}")
|
||||||
|
private String jdbc;
|
||||||
|
|
||||||
|
|
||||||
@Bean(name = "tDengineDataSource")
|
@Bean(name = "tDengineDataSource")
|
||||||
|
@@ -1,25 +0,0 @@
|
|||||||
package com.ruoyi.iot.tdengine.dao;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @package com.ruoyi.mysql.mysql.tdengine
|
|
||||||
* 类名: DatabaseMapper
|
|
||||||
* 描述: TODO
|
|
||||||
* 时间: 2022/5/16,0016 1:27
|
|
||||||
* 开发人: wxy
|
|
||||||
*/
|
|
||||||
@Repository
|
|
||||||
public interface DatabaseDAO {
|
|
||||||
|
|
||||||
int createDB();
|
|
||||||
|
|
||||||
int dropDatabase();
|
|
||||||
|
|
||||||
int useDatabase();
|
|
||||||
|
|
||||||
int createTable();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@@ -1,8 +1,11 @@
|
|||||||
package com.ruoyi.iot.tdengine.init;
|
package com.ruoyi.iot.tdengine.init;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.ruoyi.common.annotation.DataSource;
|
||||||
import com.ruoyi.iot.tdengine.config.TDengineConfig;
|
import com.ruoyi.iot.tdengine.config.TDengineConfig;
|
||||||
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
|
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
|
||||||
|
import com.taosdata.jdbc.TSDBDriver;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -12,6 +15,13 @@ import org.springframework.context.ApplicationContext;
|
|||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.io.*;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 类名: ApplicationStarted
|
* 类名: ApplicationStarted
|
||||||
* 描述: TODO
|
* 描述: TODO
|
||||||
@@ -26,15 +36,24 @@ public class ApplicationStarted implements ApplicationRunner {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private DruidDataSource dataSource;
|
||||||
|
|
||||||
|
private TDengineConfig dengineConfig;
|
||||||
|
|
||||||
|
private TDDeviceLogDAO deviceLogMapper;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) {
|
public void run(ApplicationArguments args) {
|
||||||
|
|
||||||
//先获取TDengine的配置,检测TDengine是否已经配置
|
//先获取TDengine的配置,检测TDengine是否已经配置
|
||||||
if (containBean(TDengineConfig.class)) {
|
if (containBean(TDengineConfig.class)) {
|
||||||
TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class);
|
this.dengineConfig = applicationContext.getBean(TDengineConfig.class);
|
||||||
TDDeviceLogDAO tDDeviceLogDAO = applicationContext.getBean(TDDeviceLogDAO.class);
|
this.dataSource = applicationContext.getBean("tDengineDataSource", DruidDataSource.class);
|
||||||
initTDengine(tDengineConfig, tDDeviceLogDAO);
|
this.deviceLogMapper= applicationContext.getBean(TDDeviceLogDAO.class);
|
||||||
|
initTDengine(this.dengineConfig.getDbName());
|
||||||
System.out.println("初始化TDengine成功");
|
System.out.println("初始化TDengine成功");
|
||||||
}else{
|
} else {
|
||||||
System.out.println("MySQL初始化成功");
|
System.out.println("MySQL初始化成功");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,12 +66,12 @@ public class ApplicationStarted implements ApplicationRunner {
|
|||||||
* @date 2022/5/22,0022 14:27
|
* @date 2022/5/22,0022 14:27
|
||||||
* @author wxy
|
* @author wxy
|
||||||
*/
|
*/
|
||||||
public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogDAO deviceLogMapper) {
|
public void initTDengine(String dbName) {
|
||||||
try {
|
try {
|
||||||
String dbName = dengineConfig.getDbName();
|
createDatabase();
|
||||||
int db = deviceLogMapper.createDB(dbName);
|
//创建数据库表
|
||||||
deviceLogMapper.createSTable(dbName);
|
deviceLogMapper.createSTable(dbName);
|
||||||
System.out.println(db);
|
System.out.println("完成超级表的创建");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println("ERROR");
|
System.out.println("ERROR");
|
||||||
@@ -60,6 +79,43 @@ public class ApplicationStarted implements ApplicationRunner {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Method
|
||||||
|
* @Description 根据数据库连接自动创建数据库
|
||||||
|
* @Param null
|
||||||
|
* @return
|
||||||
|
* @date 2022/5/24,0024 14:32
|
||||||
|
* @author wxy
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void createDatabase(){
|
||||||
|
try {
|
||||||
|
String dbName = dengineConfig.getDbName();
|
||||||
|
String jdbcUrl = dataSource.getRawJdbcUrl();
|
||||||
|
String username = dataSource.getUsername();
|
||||||
|
String password = dataSource.getPassword();
|
||||||
|
jdbcUrl += ("&user=" + username);
|
||||||
|
jdbcUrl += ("&password=" + password);
|
||||||
|
int startIndex = jdbcUrl.indexOf('/',12);
|
||||||
|
int endIndex = jdbcUrl.indexOf('?');
|
||||||
|
String newJdbcUrl = jdbcUrl.substring(0,startIndex);
|
||||||
|
newJdbcUrl= newJdbcUrl+jdbcUrl.substring(endIndex);
|
||||||
|
System.out.println(newJdbcUrl);
|
||||||
|
|
||||||
|
Properties connProps = new Properties();
|
||||||
|
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||||
|
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||||
|
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||||
|
Connection conn = DriverManager.getConnection(newJdbcUrl, connProps);
|
||||||
|
conn.createStatement().execute(String.format("create database if not exists %s;",dbName));
|
||||||
|
conn.close();
|
||||||
|
System.out.println("完成数据库创建");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("ERROR");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return
|
* @return
|
||||||
* @Method containBean
|
* @Method containBean
|
||||||
@@ -76,4 +132,6 @@ public class ApplicationStarted implements ApplicationRunner {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,40 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
|
||||||
<!DOCTYPE mapper
|
|
||||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|
||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
||||||
<mapper namespace="com.ruoyi.iot.tdengine.dao.DatabaseDAO">
|
|
||||||
|
|
||||||
<!-- 创建数据库-->
|
|
||||||
<update id="createDB" parameterType="java.lang.String">
|
|
||||||
create database if not exists ${dbName};
|
|
||||||
</update>
|
|
||||||
|
|
||||||
<!-- 删除数据库-->
|
|
||||||
<update id="dropDatabase" parameterType="java.lang.String">
|
|
||||||
DROP database if exists ${dbName};
|
|
||||||
</update>
|
|
||||||
|
|
||||||
<!-- 使用数据库-->
|
|
||||||
<update id="useDatabase" parameterType="java.lang.String">
|
|
||||||
use ${dbName};
|
|
||||||
</update>
|
|
||||||
|
|
||||||
<update id="createTable">
|
|
||||||
create stable if not exists ${tableName}
|
|
||||||
(ts timestamp,
|
|
||||||
log_id BIGINT,
|
|
||||||
identity NCHAR(100),
|
|
||||||
log_type NCHAR(20),
|
|
||||||
log_value NCHAR(100),
|
|
||||||
device_id BIGINT,
|
|
||||||
device_name NCHAR(100),
|
|
||||||
serial_number NCHAR(100),
|
|
||||||
is_monitor int,
|
|
||||||
create_by NCHAR(100),
|
|
||||||
create_time timestamp,
|
|
||||||
remark NCHAR(1000),
|
|
||||||
);
|
|
||||||
</update>
|
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
|
@@ -42,7 +42,6 @@
|
|||||||
</update>
|
</update>
|
||||||
|
|
||||||
<update id="createSTable">
|
<update id="createSTable">
|
||||||
|
|
||||||
create STABLE if not exists ${database}.device_log
|
create STABLE if not exists ${database}.device_log
|
||||||
(
|
(
|
||||||
ts timestamp,
|
ts timestamp,
|
||||||
|
Reference in New Issue
Block a user