Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nnjcy-data-model
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ljb
nnjcy-data-model
Commits
ec165856
Commit
ec165856
authored
Oct 25, 2023
by
ljb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解决数据库连接无响应问题
parent
0e9d716d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
209 additions
and
157 deletions
+209
-157
data-collect-server/src/main/java/com/zq/datacollect/holder/DatabaseHolder.java
+2
-1
data-collect-server/src/main/java/com/zq/datacollect/runner/DatabaseRunner.java
+2
-1
dataoperation-server/pom.xml
+2
-7
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
+5
-6
dataoperation-server/src/main/java/com/zq/dataoperation/dao/DataCollectTaskLogMapper.java
+8
-0
dataoperation-server/src/main/java/com/zq/dataoperation/entity/DataCollectTaskLog.java
+69
-0
dataoperation-server/src/main/java/com/zq/dataoperation/holder/DatabaseHolder.java
+5
-11
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
+13
-9
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
+32
-73
dataoperation-server/src/main/java/com/zq/dataoperation/service/DataCleanRecordService.java
+2
-2
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
+0
-19
dataoperation-server/src/main/java/com/zq/dataoperation/utils/SqlUtils.java
+29
-1
dataoperation-server/src/main/java/com/zq/dataoperation/utils/TableUtil.java
+40
-27
No files found.
data-collect-server/src/main/java/com/zq/datacollect/holder/DatabaseHolder.java
View file @
ec165856
...
@@ -29,4 +29,4 @@ public class DatabaseHolder {
...
@@ -29,4 +29,4 @@ public class DatabaseHolder {
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
}
}
}
}
\ No newline at end of file
data-collect-server/src/main/java/com/zq/datacollect/runner/DatabaseRunner.java
View file @
ec165856
...
@@ -61,4 +61,4 @@ public class DatabaseRunner implements ApplicationRunner {
...
@@ -61,4 +61,4 @@ public class DatabaseRunner implements ApplicationRunner {
DatabaseHolder
.
set
(
jdbcTemplateMap
);
DatabaseHolder
.
set
(
jdbcTemplateMap
);
log
.
info
(
"成功加载数据库:{}个"
,
jdbcTemplateMap
.
size
());
log
.
info
(
"成功加载数据库:{}个"
,
jdbcTemplateMap
.
size
());
}
}
}
}
\ No newline at end of file
dataoperation-server/pom.xml
View file @
ec165856
...
@@ -111,20 +111,15 @@
...
@@ -111,20 +111,15 @@
<groupId>
org.springframework
</groupId>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
<artifactId>
spring-test
</artifactId>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
com.zq
</groupId>
<groupId>
com.zq
</groupId>
<artifactId>
sys-server
</artifactId>
<artifactId>
sys-server
</artifactId>
<version>
1.0.0
</version>
<version>
1.0.0
</version>
<scope>
compile
</scope>
<scope>
compile
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
com.zq
</groupId>
<artifactId>
data-collect-server
</artifactId>
<version>
1.0.0
</version>
<scope>
compile
</scope>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
<plugins>
<plugins>
<plugin>
<plugin>
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/controller/CommonQueryController.java
View file @
ec165856
...
@@ -93,12 +93,11 @@ public class CommonQueryController {
...
@@ -93,12 +93,11 @@ public class CommonQueryController {
return
ResultVo
.
success
();
return
ResultVo
.
success
();
}
}
// @ApiOperation("运行查询")
@ApiOperation
(
"运行查询"
)
// @PostMapping("/run")
@PostMapping
(
"/run"
)
// public ResultVo run(@RequestBody Map<String, Object> body) throws Exception {
public
ResultVo
run
(
@RequestBody
Map
<
String
,
Object
>
body
)
{
// System.out.println(body);
return
ResultVo
.
success
(
commonQueryService
.
run
(
body
));
// return ResultVo.success(commonQueryService.run(body));
}
// }
@ApiOperation
(
"自定义sql查询"
)
@ApiOperation
(
"自定义sql查询"
)
@PostMapping
(
"/runSelect"
)
@PostMapping
(
"/runSelect"
)
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/dao/DataCollectTaskLogMapper.java
0 → 100644
View file @
ec165856
package
com
.
zq
.
dataoperation
.
dao
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.zq.dataoperation.entity.DataCollectTaskLog
;
public
interface
DataCollectTaskLogMapper
extends
BaseMapper
<
DataCollectTaskLog
>
{
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/entity/DataCollectTaskLog.java
0 → 100644
View file @
ec165856
package
com
.
zq
.
dataoperation
.
entity
;
import
com.baomidou.mybatisplus.annotation.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
java.util.Date
;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName
(
value
=
"data_collect_task_log"
)
public
class
DataCollectTaskLog
{
/**
* 数据汇聚表id
*/
private
Integer
dataCollectSettingId
;
/**
* 开始时间
*/
private
Date
startTime
;
/**
* 结束时间
*/
private
Date
endTime
;
/**
* 花费多少秒
*/
private
Integer
duration
;
/**
* 状态
* 1运行中 2正常结束 3异常终止
*/
private
Integer
status
;
/**
* 错误信息
* 异常终止时记录异常信息
*/
private
String
error
;
/**
* 使用序列
* id
*/
@TableId
(
type
=
IdType
.
AUTO
)
private
Integer
id
;
/**
* createTime
*/
@TableField
(
fill
=
FieldFill
.
INSERT
)
private
Date
createTime
;
/**
* updateTime
*/
@TableField
(
fill
=
FieldFill
.
INSERT_UPDATE
)
private
Date
updateTime
;
}
dataoperation-server/src/main/java/com/zq/dataoperation/holder/DatabaseHolder.java
View file @
ec165856
...
@@ -11,30 +11,23 @@ public class DatabaseHolder {
...
@@ -11,30 +11,23 @@ public class DatabaseHolder {
private
static
Map
<
Integer
,
JdbcTemplate
>
jdbcTemplateMap
;
private
static
Map
<
Integer
,
JdbcTemplate
>
jdbcTemplateMap
;
public
static
Map
<
Integer
,
JdbcTemplate
>
getJdbcTemplateMap
()
{
return
jdbcTemplateMap
;
}
public
static
void
set
(
Map
<
Integer
,
JdbcTemplate
>
map
)
{
public
static
void
set
(
Map
<
Integer
,
JdbcTemplate
>
map
)
{
jdbcTemplateMap
=
map
;
jdbcTemplateMap
=
map
;
}
}
public
static
void
add
(
Integer
id
,
JdbcTemplate
jdbcTemplate
)
{
public
static
void
add
(
Integer
id
,
JdbcTemplate
jdbcTemplate
){
if
(
jdbcTemplateMap
==
null
)
{
jdbcTemplateMap
=
new
HashMap
<>();
}
jdbcTemplateMap
.
put
(
id
,
jdbcTemplate
);
jdbcTemplateMap
.
put
(
id
,
jdbcTemplate
);
}
}
public
static
JdbcTemplate
getJdbcTemplate
(
Integer
id
)
{
public
static
JdbcTemplate
getJdbcTemplate
(
Integer
id
){
return
jdbcTemplateMap
.
get
(
id
);
return
jdbcTemplateMap
.
get
(
id
);
}
}
public
static
Connection
getConnection
(
Integer
id
)
{
public
static
Connection
getConnection
(
Integer
id
){
try
{
try
{
return
jdbcTemplateMap
.
get
(
id
).
getDataSource
().
getConnection
();
return
jdbcTemplateMap
.
get
(
id
).
getDataSource
().
getConnection
();
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
}
}
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/runner/DatabaseRunner.java
View file @
ec165856
...
@@ -2,32 +2,34 @@ package com.zq.dataoperation.runner;
...
@@ -2,32 +2,34 @@ package com.zq.dataoperation.runner;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder
;
import
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.zq.common.exception.BusinessException
;
import
com.zq.common.exception.BusinessException
;
import
com.zq.dataoperation.dao.QueryDbDao
;
import
com.zq.dataoperation.entity.QueryDb
;
import
com.zq.dataoperation.entity.QueryDb
;
import
com.zq.dataoperation.holder.DatabaseHolder
;
import
com.zq.dataoperation.holder.DatabaseHolder
;
import
com.zq.dataoperation.service.CommonQueryService
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
@Slf4j
@Slf4j
@Component
@Component
public
class
DatabaseRunner
implements
ApplicationRunner
{
public
class
DatabaseRunner
implements
ApplicationRunner
{
@Resource
@Resource
private
QueryDbDao
queryDbDao
;
private
CommonQueryService
service
;
private
Map
<
Integer
,
JdbcTemplate
>
jdbcTemplateMap
=
new
HashMap
<>();
@Override
@Override
public
void
run
(
ApplicationArguments
args
)
throws
SQLException
{
public
void
run
(
ApplicationArguments
args
)
throws
SQLException
{
List
<
QueryDb
>
datasource
=
queryDbDao
.
selectList
(
new
QueryWrapper
<>()
);
List
<
QueryDb
>
datasource
=
service
.
getDatasource
(
);
for
(
QueryDb
ds
:
datasource
)
{
for
(
QueryDb
ds
:
datasource
)
{
DruidDataSource
druidDataSource
=
DruidDataSourceBuilder
.
create
().
build
();
DruidDataSource
druidDataSource
=
DruidDataSourceBuilder
.
create
().
build
();
String
url
=
""
;
String
url
=
""
;
...
@@ -53,8 +55,9 @@ public class DatabaseRunner implements ApplicationRunner {
...
@@ -53,8 +55,9 @@ public class DatabaseRunner implements ApplicationRunner {
druidDataSource
.
setUsername
(
ds
.
getUsername
());
druidDataSource
.
setUsername
(
ds
.
getUsername
());
druidDataSource
.
setPassword
(
ds
.
getPassword
());
druidDataSource
.
setPassword
(
ds
.
getPassword
());
druidDataSource
.
init
();
druidDataSource
.
init
();
DatabaseHolder
.
add
(
ds
.
getId
(),
new
JdbcTemplate
(
druidDataSource
));
jdbcTemplateMap
.
put
(
ds
.
getId
(),
new
JdbcTemplate
(
druidDataSource
));
}
}
log
.
info
(
"成功加载数据库:{}个"
,
DatabaseHolder
.
getJdbcTemplateMap
().
size
());
DatabaseHolder
.
set
(
jdbcTemplateMap
);
log
.
info
(
"成功加载数据库:{}个"
,
jdbcTemplateMap
.
size
());
}
}
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/service/CommonQueryService.java
View file @
ec165856
This diff is collapsed.
Click to expand it.
dataoperation-server/src/main/java/com/zq/dataoperation/service/DataCleanRecordService.java
View file @
ec165856
...
@@ -4,11 +4,11 @@ import cn.hutool.core.date.DateUtil;
...
@@ -4,11 +4,11 @@ import cn.hutool.core.date.DateUtil;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.zq.common.utils.AssertUtils
;
import
com.zq.common.utils.AssertUtils
;
import
com.zq.common.vo.ResultVo
;
import
com.zq.common.vo.ResultVo
;
import
com.zq.datacollect.entity.DataCollectTaskLog
;
import
com.zq.datacollect.mapper.DataCollectTaskLogMapper
;
import
com.zq.dataoperation.dao.DataCleanRecordDao
;
import
com.zq.dataoperation.dao.DataCleanRecordDao
;
import
com.zq.dataoperation.dao.DataCollectTaskLogMapper
;
import
com.zq.dataoperation.dao.QueryDbDao
;
import
com.zq.dataoperation.dao.QueryDbDao
;
import
com.zq.dataoperation.entity.DataCleanRecord
;
import
com.zq.dataoperation.entity.DataCleanRecord
;
import
com.zq.dataoperation.entity.DataCollectTaskLog
;
import
com.zq.dataoperation.entity.QueryDb
;
import
com.zq.dataoperation.entity.QueryDb
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
...
dataoperation-server/src/main/java/com/zq/dataoperation/utils/ConnectionUtil.java
deleted
100644 → 0
View file @
0e9d716d
package
com
.
zq
.
dataoperation
.
utils
;
import
com.alibaba.druid.pool.DruidPooledConnection
;
import
java.sql.Connection
;
import
java.util.Map
;
public
class
ConnectionUtil
{
private
static
Map
<
Integer
,
DruidPooledConnection
>
connectionMap
;
public
static
void
set
(
Map
<
Integer
,
DruidPooledConnection
>
map
)
{
connectionMap
=
map
;
}
public
static
Connection
get
(
Integer
id
){
return
connectionMap
.
get
(
id
).
getConnection
();
}
}
dataoperation-server/src/main/java/com/zq/dataoperation/utils/SqlUtils.java
View file @
ec165856
...
@@ -31,6 +31,7 @@ import java.sql.*;
...
@@ -31,6 +31,7 @@ import java.sql.*;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
/**
/**
* @author /
* @author /
...
@@ -234,4 +235,30 @@ public class SqlUtils {
...
@@ -234,4 +235,30 @@ public class SqlUtils {
return
sqlList
;
return
sqlList
;
}
}
}
public
static
String
jointInsertSql
(
String
tableName
,
Set
<
String
>
cloums
){
StringBuilder
fileds
=
new
StringBuilder
();
for
(
String
cloum
:
cloums
)
{
fileds
.
append
(
cloum
.
replaceAll
(
"\\p{Punct}"
,
""
)).
append
(
","
);
}
fileds
.
deleteCharAt
(
fileds
.
length
()-
1
);
StringBuilder
sql
=
new
StringBuilder
(
"INSERT INTO "
).
append
(
tableName
).
append
(
"("
).
append
(
fileds
).
append
(
") VALUES ("
);
for
(
int
i
=
0
;
i
<
cloums
.
size
();
i
++)
{
if
(
i
==
0
){
sql
.
append
(
"?"
);
}
else
{
sql
.
append
(
",?"
);
}
}
sql
.
append
(
")"
);
return
sql
.
toString
();
}
public
static
String
joinCreateTableSql
(
String
tableName
,
Set
<
String
>
cloums
){
StringBuilder
sql
=
new
StringBuilder
(
"CREATE TABLE "
).
append
(
tableName
).
append
(
"("
);
for
(
String
cloum
:
cloums
)
{
sql
.
append
(
cloum
.
replaceAll
(
"\\p{Punct}"
,
""
)).
append
(
" VARCHAR(255),"
);
}
sql
.
deleteCharAt
(
sql
.
length
()-
1
).
append
(
")"
);
return
sql
.
toString
();
}
}
\ No newline at end of file
dataoperation-server/src/main/java/com/zq/dataoperation/utils/TableUtil.java
View file @
ec165856
...
@@ -3,7 +3,6 @@ package com.zq.dataoperation.utils;
...
@@ -3,7 +3,6 @@ package com.zq.dataoperation.utils;
import
com.zq.dataoperation.holder.DatabaseHolder
;
import
com.zq.dataoperation.holder.DatabaseHolder
;
import
com.zq.dataoperation.vo.Column
;
import
com.zq.dataoperation.vo.Column
;
import
lombok.SneakyThrows
;
import
lombok.SneakyThrows
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.DatabaseMetaData
;
...
@@ -18,35 +17,49 @@ public class TableUtil {
...
@@ -18,35 +17,49 @@ public class TableUtil {
@SneakyThrows
@SneakyThrows
public
static
Object
getColumns
(
Integer
queryId
,
String
tableName
)
{
public
static
Object
getColumns
(
Integer
queryId
,
String
tableName
)
{
String
driver
=
"com.mysql.cj.jdbc.Driver"
;
List
<
Column
>
columns
=
new
ArrayList
<>();
Class
.
forName
(
driver
);
Connection
connection
=
null
;
Connection
connection
=
DatabaseHolder
.
getConnection
(
queryId
);
ResultSet
idxRs
=
null
;
DatabaseMetaData
metaData
=
connection
.
getMetaData
();
ResultSet
columnRs
=
null
;
String
primaryColumn
=
""
;
try
{
ResultSet
idxRs
=
metaData
.
getIndexInfo
(
connection
.
getCatalog
(),
connection
.
getCatalog
(),
tableName
,
false
,
false
);
String
driver
=
"com.mysql.cj.jdbc.Driver"
;
while
(
idxRs
.
next
())
{
Class
.
forName
(
driver
);
String
indexName
=
idxRs
.
getString
(
"INDEX_NAME"
);
connection
=
DatabaseHolder
.
getConnection
(
queryId
);
if
(
"PRIMARY"
.
equals
(
indexName
))
{
DatabaseMetaData
metaData
=
connection
.
getMetaData
();
primaryColumn
=
idxRs
.
getString
(
"COLUMN_NAME"
);
String
primaryColumn
=
""
;
break
;
idxRs
=
metaData
.
getIndexInfo
(
connection
.
getCatalog
(),
connection
.
getCatalog
(),
tableName
,
false
,
false
);
while
(
idxRs
.
next
())
{
String
indexName
=
idxRs
.
getString
(
"INDEX_NAME"
);
if
(
"PRIMARY"
.
equals
(
indexName
))
{
primaryColumn
=
idxRs
.
getString
(
"COLUMN_NAME"
);
break
;
}
}
}
}
List
<
Column
>
columns
=
new
ArrayList
<>();
columnRs
=
metaData
.
getColumns
(
connection
.
getCatalog
(),
connection
.
getCatalog
(),
tableName
,
"%"
);
ResultSet
columnRs
=
metaData
.
getColumns
(
connection
.
getCatalog
(),
connection
.
getCatalog
(),
tableName
,
"%"
);
while
(
columnRs
.
next
())
{
while
(
columnRs
.
next
())
{
Column
column
=
new
Column
();
Column
column
=
new
Column
();
column
.
setName
(
columnRs
.
getString
(
"COLUMN_NAME"
));
column
.
setName
(
columnRs
.
getString
(
"COLUMN_NAME"
));
column
.
setType
(
columnRs
.
getString
(
"TYPE_NAME"
));
column
.
setType
(
columnRs
.
getString
(
"TYPE_NAME"
));
column
.
setLength
(
columnRs
.
getString
(
"COLUMN_SIZE"
));
column
.
setLength
(
columnRs
.
getString
(
"COLUMN_SIZE"
));
column
.
setDecimalDigits
(
columnRs
.
getString
(
"DECIMAL_DIGITS"
));
column
.
setDecimalDigits
(
columnRs
.
getString
(
"DECIMAL_DIGITS"
));
column
.
setNotNull
(!
columnRs
.
getBoolean
(
"NULLABLE"
));
column
.
setNotNull
(!
columnRs
.
getBoolean
(
"NULLABLE"
));
column
.
setPrimary
(
column
.
getName
().
equals
(
primaryColumn
));
column
.
setPrimary
(
column
.
getName
().
equals
(
primaryColumn
));
column
.
setComment
(
columnRs
.
getString
(
"REMARKS"
));
column
.
setComment
(
columnRs
.
getString
(
"REMARKS"
));
column
.
setQueryId
(
queryId
.
toString
());
column
.
setQueryId
(
queryId
.
toString
());
columns
.
add
(
column
);
columns
.
add
(
column
);
}
}
finally
{
if
(
columnRs
!=
null
){
columnRs
.
close
();
}
if
(
idxRs
!=
null
){
idxRs
.
close
();
}
if
(
connection
!=
null
){
connection
.
close
();
}
}
}
return
columns
;
return
columns
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment