提交 da3be51d authored 作者: 黄承天's avatar 黄承天

[数据源模块]初始提交

上级 d0a91b50
......@@ -32,7 +32,7 @@
<scope>test</scope>
</dependency>
<!-- 下面是我引入的东西 zsp-->
<!-- 下面是我引入的东西 zsp-->
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
......@@ -100,7 +100,7 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- caffeine缓存 -->
<!-- caffeine缓存 -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
......@@ -110,30 +110,36 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--动态数据源组件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<resources>
<!-- <resource>-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*</include>-->
<!-- </includes>-->
<!-- <excludes>-->
<!-- <exclude>**/.svn/*</exclude>-->
<!-- </excludes>-->
<!-- <filtering>false</filtering>-->
<!-- </resource>-->
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>META-INF/resources/</targetPath>
</resource>
<!-- <resource>-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*</include>-->
<!-- </includes>-->
<!-- <excludes>-->
<!-- <exclude>**/.svn/*</exclude>-->
<!-- </excludes>-->
<!-- <filtering>false</filtering>-->
<!-- </resource>-->
<!-- <resource>-->
<!-- <directory>${project.basedir}/src/main/resources</directory>-->
<!-- <targetPath>META-INF/resources/</targetPath>-->
<!-- </resource>-->
</resources>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
......
package com.tykj.workflowcore.ds.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataSourceInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String url;
private String username;
private String password;
private String driverClassName;
}
package com.tykj.workflowcore.ds.repository;
import com.tykj.workflowcore.ds.entity.DataSourceInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DataSourceInfoRepository extends JpaRepository<DataSourceInfo,Integer> {
}
package com.tykj.workflowcore.ds.service;
import com.tykj.workflowcore.model_layer.entity.ColumnInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.vo.ColumnVO;
import com.tykj.workflowcore.model_layer.entity.vo.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static java.util.Objects.nonNull;
/**
*
*/
@Service
public class DataOperation {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSourceManager dataSourceManager;
@Autowired
private ModelService modelService;
public void copyTableFromDataSource(String tableName, String dataSourceName) {
dataSourceManager.switchToDataSource(dataSourceName);
String sql = String.valueOf(jdbcTemplate.queryForMap(format("show create table %s", tableName)).get("sql"));
dataSourceManager.clear();
jdbcTemplate.execute(sql);
}
public void syncAllTablesFromDataSource(String dataSourceName) {
dataSourceManager.switchToDataSource(dataSourceName);
List<String> tableNames = getAllTableNames();
for (String tableName : tableNames) {
TableVO tableVO = new TableVO();
tableVO.setModelName(tableName);
tableVO.setModelType(3);
tableVO.setDataList(getColumns(tableName));
modelService.newTable(tableVO);
}
}
private List<String> getAllTableNames() {
return jdbcTemplate.queryForList("show tables").stream()
.map(resultMap -> String.valueOf(resultMap.get("Tables_in_center")))
.collect(Collectors.toList());
}
private List<ColumnVO> getColumns(String tableName) {
return jdbcTemplate.queryForList(format("desc %s", tableName)).stream()
.map(this::columnVO)
.collect(Collectors.toList());
}
private ColumnVO columnVO(Map<String, Object> columnInfoMap) {
String columnName = String.valueOf(columnInfoMap.get("Field"));
String typeValue = String.valueOf(columnInfoMap.get("Type"));
//
boolean hasLength = typeValue.contains("(") && typeValue.contains(")");
String columnType;
Integer columnLength;
if (hasLength) {
//
columnType = typeValue.substring(0, typeValue.indexOf("("));
columnLength = Integer.valueOf(typeValue.substring(typeValue.indexOf("(") + 1, typeValue.indexOf(")")));
} else {
columnType = typeValue;
columnLength = 0;
}
String keyValue = String.valueOf(columnInfoMap.get("Key"));
Integer isPrimary = Objects.equals(keyValue, "PRI") ? 0 : 1;
return new ColumnVO(
null,
isPrimary,
columnType,
columnName,
Strings.EMPTY,
columnLength,
Strings.EMPTY
);
}
public void syncData(String tableName) {
List<Map<String, Object>> dataList = jdbcTemplate.queryForList(format("select * from %s"), tableName);
for (Map<String, Object> data : dataList) {
String sql = insertSql(tableName, data);
}
}
private String insertSql(String tableName, Map<String, Object> data) {
StringBuilder result = new StringBuilder();
result.append("insert info ").append(tableName).append(" ");
StringBuilder names = new StringBuilder();
names.append("(");
StringBuilder values = new StringBuilder();
values.append("(");
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (nonNull(entry.getValue())){
names.append(entry.getKey()).append(",");
values.append(entry.getValue()).append(",");
}
}
names.delete(names.indexOf(","), names.indexOf(",") + 1);
values.delete(values.indexOf(","), values.indexOf(",") + 1);
names.append(")");
values.append(")");
result.append(names);
result.append(" values ");
result.append(values);
return result.toString();
}
}
package com.tykj.workflowcore.ds.service;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.tykj.workflowcore.ds.entity.DataSourceInfo;
import com.tykj.workflowcore.ds.repository.DataSourceInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.util.List;
@Service
public class DataSourceManager {
@Autowired
private DataSourceInfoRepository dataSourceInfoRepository;
@Autowired
private DataSource dataSource;
private BasicDataSourceCreator dataSourceCreator = new BasicDataSourceCreator();
private void save(DataSourceInfo dataSourceInfo) {
dataSourceInfoRepository.save(dataSourceInfo);
}
private void deleteById(Integer id) {
}
private List<DataSourceInfo> findAll() {
return dataSourceInfoRepository.findAll();
}
private DataSourceInfo findById(Integer id) {
return dataSourceInfoRepository.findById(id).orElseThrow(() -> new RuntimeException("未找到该id的数据"));
}
public void init() {
List<DataSourceInfo> dataSourceInfos = dataSourceInfoRepository.findAll();
dataSourceInfos.forEach(this::initDataSource);
}
public void initDataSource(DataSourceInfo dataSourceInfo) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setUrl(dataSourceInfo.getUrl());
dataSourceProperty.setUsername(dataSourceInfo.getUsername());
dataSourceProperty.setPassword(dataSourceInfo.getPassword());
dataSourceProperty.setDriverClassName(dataSourceInfo.getDriverClassName());
DataSource basicDataSource = dataSourceCreator.createDataSource(dataSourceProperty);
((DynamicRoutingDataSource) dataSource).addDataSource(dataSourceInfo.getName(), basicDataSource);
}
public void switchToDataSource(String dataSourceName) {
DynamicDataSourceContextHolder.push(dataSourceName);
}
public void clear() {
DynamicDataSourceContextHolder.clear();
}
}
......@@ -4,9 +4,21 @@ spring:
password: Huang123+
url: jdbc:mysql://47.106.142.73:3306/www_hxh?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf-8&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
#使用动态数据源组件后所必须添加的配置 若没有则会在初始化数据源时应无法找到primary数据源而报错
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
lazy: false #默认立即初始化数据源,true则支持在需要建立连接时再初始化数据源
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://47.106.142.73:3306/www3?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf-8&nullCatalogMeansCurrent=true
username: root
password: Huang123+
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: update
server:
port: 8800
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论