Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
b5d4791b
Commit
b5d4791b
authored
May 28, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:获取数据库数据
parent
e635c8d0
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
401 additions
and
108 deletions
+401
-108
pom.xml
pom.xml
+0
-5
CellData.java
...main/java/cn/com/poc/common/domain/database/CellData.java
+27
-0
TableDataResponse.java
.../cn/com/poc/common/domain/database/TableDataResponse.java
+39
-0
TableInfo.java
...ain/java/cn/com/poc/common/domain/database/TableInfo.java
+54
-0
DatabaseUtil.java
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
+153
-52
ContentReportRestImpl.java
...va/cn/com/poc/expose/rest/impl/ContentReportRestImpl.java
+0
-26
DatabaseTableInfoCache.java
...va/cn/com/poc/knowledge/cache/DatabaseTableInfoCache.java
+4
-3
BizKnowledgeDatabaseDto.java
...ava/cn/com/poc/knowledge/dto/BizKnowledgeDatabaseDto.java
+4
-3
DatabaseTableDataDto.java
...n/java/cn/com/poc/knowledge/dto/DatabaseTableDataDto.java
+41
-0
DatabaseTableDataResponseDto.java
...n/com/poc/knowledge/dto/DatabaseTableDataResponseDto.java
+28
-0
DatabaseRest.java
src/main/java/cn/com/poc/knowledge/rest/DatabaseRest.java
+9
-4
DatabaseRestImpl.java
...java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
+29
-10
DatabaseUtilTest.java
src/test/java/cn/com/poc/utils/DatabaseUtilTest.java
+13
-5
No files found.
pom.xml
View file @
b5d4791b
...
@@ -427,10 +427,5 @@
...
@@ -427,10 +427,5 @@
<name>
Nexus aliyun
</name>
<name>
Nexus aliyun
</name>
<url>
http://maven.aliyun.com/nexus/content/groups/public
</url>
<url>
http://maven.aliyun.com/nexus/content/groups/public
</url>
</repository>
</repository>
<repository>
<id>
com.e-iceblue
</id>
<name>
e-iceblue
</name>
<url>
https://repo.e-iceblue.cn/repository/maven-public/
</url>
</repository>
</repositories>
</repositories>
</project>
</project>
src/main/java/cn/com/poc/common/domain/database/CellData.java
0 → 100644
View file @
b5d4791b
package
cn
.
com
.
poc
.
common
.
domain
.
database
;
/**
* @author alex.yao
* @date 2025/5/28
*/
public
class
CellData
{
private
String
type
;
private
Object
value
;
public
String
getType
()
{
return
type
;
}
public
void
setType
(
String
type
)
{
this
.
type
=
type
;
}
public
Object
getValue
()
{
return
value
;
}
public
void
setValue
(
Object
value
)
{
this
.
value
=
value
;
}
}
src/main/java/cn/com/poc/common/domain/database/TableDataResponse.java
0 → 100644
View file @
b5d4791b
package
cn
.
com
.
poc
.
common
.
domain
.
database
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
java.util.List
;
/**
* @author alex.yao
* @date 2025/5/28
*/
public
class
TableDataResponse
{
private
List
<
String
>
column
;
private
List
<
List
<
CellData
>>
tableDatas
;
private
PagingInfo
pageInfo
;
public
List
<
String
>
getColumn
()
{
return
column
;
}
public
void
setColumn
(
List
<
String
>
column
)
{
this
.
column
=
column
;
}
public
List
<
List
<
CellData
>>
getTableDatas
()
{
return
tableDatas
;
}
public
void
setTableDatas
(
List
<
List
<
CellData
>>
tableDatas
)
{
this
.
tableDatas
=
tableDatas
;
}
public
PagingInfo
getPageInfo
()
{
return
pageInfo
;
}
public
void
setPageInfo
(
PagingInfo
pageInfo
)
{
this
.
pageInfo
=
pageInfo
;
}
}
src/main/java/cn/com/poc/common/domain/database/TableInfo.java
0 → 100644
View file @
b5d4791b
package
cn
.
com
.
poc
.
common
.
domain
.
database
;
/**
* @author alex.yao
* @date 2025/5/28
*/
public
class
TableInfo
{
private
String
TABLE_NAME
;
private
String
TABLE_COMMENT
;
private
int
TABLE_ROWS
;
private
int
TABLE_COLUMN
;
public
String
getTABLE_NAME
()
{
return
TABLE_NAME
;
}
public
void
setTABLE_NAME
(
String
TABLE_NAME
)
{
this
.
TABLE_NAME
=
TABLE_NAME
;
}
public
String
getTABLE_COMMENT
()
{
return
TABLE_COMMENT
;
}
public
void
setTABLE_COMMENT
(
String
TABLE_COMMENT
)
{
this
.
TABLE_COMMENT
=
TABLE_COMMENT
;
}
public
int
getTABLE_ROWS
()
{
return
TABLE_ROWS
;
}
public
void
setTABLE_ROWS
(
int
TABLE_ROWS
)
{
this
.
TABLE_ROWS
=
TABLE_ROWS
;
}
public
int
getTABLE_COLUMN
()
{
return
TABLE_COLUMN
;
}
public
void
setTABLE_COLUMN
(
int
TABLE_COLUMN
)
{
this
.
TABLE_COLUMN
=
TABLE_COLUMN
;
}
@Override
public
String
toString
()
{
return
"TableInfo{"
+
"TABLE_NAME='"
+
TABLE_NAME
+
'\''
+
", TABLE_COMMENT='"
+
TABLE_COMMENT
+
'\''
+
", TABLE_ROWS="
+
TABLE_ROWS
+
", TABLE_COLUMN="
+
TABLE_COLUMN
+
'}'
;
}
}
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
View file @
b5d4791b
package
cn
.
com
.
poc
.
common
.
utils
;
package
cn
.
com
.
poc
.
common
.
utils
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
cn.com.poc.common.domain.database.*
;
import
java.sql.Connection
;
import
java.sql.*
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -36,6 +35,78 @@ public class DatabaseUtil {
...
@@ -36,6 +35,78 @@ public class DatabaseUtil {
"FROM INFORMATION_SCHEMA.TABLES t "
+
"FROM INFORMATION_SCHEMA.TABLES t "
+
"WHERE TABLE_SCHEMA = ?"
;
"WHERE TABLE_SCHEMA = ?"
;
/**
* 获取表结构
*/
public
static
ResultSet
getTableStructure
(
String
host
,
int
port
,
String
username
,
String
password
,
String
database
,
String
tableName
)
{
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
// 连接超时(毫秒)
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
// socket超时(毫秒)
String
sql
=
"SELECT t.table_name,"
+
" c.column_name,"
+
" c.data_type, "
+
" c.numeric_precision, "
+
" ISNULL(CHARACTER_MAXIMUM_LENGTH,0) AS SIZE "
+
"FROM INFORMATION_SCHEMA.TABLES AS t "
+
"INNER JOIN INFORMATION_SCHEMA.COLUMNS c ON"
+
" (t.table_name = c.table_name) z"
+
"WHERE t.table_name = ? "
+
"ORDER BY t.table_name"
;
try
{
Class
.
forName
(
DRIVER_NAME
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
sql
);
preparedStatement
.
setString
(
1
,
tableName
);
// preparedStatement.setString(2, tableName);
ResultSet
resultSet
=
preparedStatement
.
executeQuery
();
// List<TableInfo> tableInfoList = new ArrayList<>();
// while (resultSet.next()) {
// TableInfo tableInfo = new TableInfo();
// tableInfo.setTABLE_NAME(resultSet.getString("TABLE_NAME"));
// tableInfo.setTABLE_COMMENT(resultSet.getString("TABLE_COMMENT"));
// tableInfo.setTABLE_ROWS(resultSet.getInt("TABLE_ROWS"));
// tableInfo.setTABLE_COLUMN(resultSet.getInt("TABLE_COLUMN"));
// tableInfoList.add(tableInfo);
// }
connection
.
close
();
return
resultSet
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"连接数据库失败"
,
e
);
return
null
;
}
}
/**
* 获取表数据
*/
// public static List<List<String>> getTableData(String host, int port, String username, String password, String database, String tableName, int pageNum, int pageSize) {
// String url = "jdbc:mysql://" + host + ":" + port + "/" + database
// + "?connectTimeout=" + CONNECT_TIMEOUT // 连接超时(毫秒)
// + "&socketTimeout=" + SOCKET_TIMEOUT; // socket超时(毫秒)
// try {
// Class.forName(DRIVER_NAME);
// Connection connection = DriverManager.getConnection(url, username, password);
// PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " + "?" + " LIMIT " + (pageNum - 1) * pageSize + "," + pageSize);
// preparedStatement.setString(1, tableName);
// ResultSet resultSet = preparedStatement.executeQuery();
// List<List<String>> dataList = new ArrayList<>();
// while (resultSet.next()) {
// int columnCount = resultSet.getMetaData().getColumnCount();
// List<String> rowData = new ArrayList<>();
// for (int i = 1; i <= columnCount; i++) {
// rowData.add(resultSet.getString(i));
// }
// dataList.add(rowData);
// }
// connection.close();
// return dataList;
// } catch (Exception e) {
// logger.error("连接数据库失败", e);
// throw new BusinessException(e);
// }
// }
/**
/**
* 测试数据库连接
* 测试数据库连接
...
@@ -62,6 +133,84 @@ public class DatabaseUtil {
...
@@ -62,6 +133,84 @@ public class DatabaseUtil {
}
}
}
}
/**
* 获取表数据方法
*/
public
static
TableDataResponse
getTableData
(
String
host
,
int
port
,
String
username
,
String
password
,
String
database
,
String
tableName
,
int
pageNum
,
int
pageSize
)
{
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
TableDataResponse
response
=
new
TableDataResponse
();
Connection
connection
=
null
;
try
{
Class
.
forName
(
DRIVER_NAME
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
// 获取总行数
PreparedStatement
countStmt
=
connection
.
prepareStatement
(
"SELECT COUNT(*) FROM "
+
tableName
);
ResultSet
countRs
=
countStmt
.
executeQuery
();
countRs
.
next
();
int
totalRows
=
countRs
.
getInt
(
1
);
// 构建分页信息
PagingInfo
pageInfo
=
new
PagingInfo
();
pageInfo
.
setPageNo
(
pageNum
);
pageInfo
.
setPageSize
(
pageSize
);
pageInfo
.
setTotalRows
(
totalRows
);
response
.
setPageInfo
(
pageInfo
);
// 获取数据
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
"SELECT * FROM "
+
tableName
+
" LIMIT "
+
(
pageNum
-
1
)
*
pageSize
+
","
+
pageSize
);
ResultSet
resultSet
=
preparedStatement
.
executeQuery
();
ResultSetMetaData
metaData
=
resultSet
.
getMetaData
();
// 构建列信息
List
<
String
>
columns
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<=
metaData
.
getColumnCount
();
i
++)
{
columns
.
add
(
metaData
.
getColumnName
(
i
));
}
response
.
setColumn
(
columns
);
// 构建表格数据
List
<
List
<
CellData
>>
tableDatas
=
new
ArrayList
<>();
while
(
resultSet
.
next
())
{
List
<
CellData
>
row
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<=
metaData
.
getColumnCount
();
i
++)
{
CellData
cell
=
new
CellData
();
int
columnType
=
metaData
.
getColumnType
(
i
);
// 根据列类型设置数据类型
if
(
columnType
==
Types
.
INTEGER
||
columnType
==
Types
.
BIGINT
||
columnType
==
Types
.
DECIMAL
||
columnType
==
Types
.
DOUBLE
)
{
cell
.
setType
(
"number"
);
cell
.
setValue
(
resultSet
.
getObject
(
i
));
}
else
{
cell
.
setType
(
"string"
);
cell
.
setValue
(
resultSet
.
getString
(
i
));
}
row
.
add
(
cell
);
}
tableDatas
.
add
(
row
);
}
response
.
setTableDatas
(
tableDatas
);
return
response
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"连接数据库失败"
,
e
);
throw
new
BusinessException
(
e
);
}
finally
{
try
{
if
(
connection
!=
null
)
connection
.
close
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"关闭连接失败"
,
e
);
}
}
}
/**
/**
* 获取数据库表列表
* 获取数据库表列表
*/
*/
...
@@ -92,53 +241,5 @@ public class DatabaseUtil {
...
@@ -92,53 +241,5 @@ public class DatabaseUtil {
}
}
}
}
public
static
class
TableInfo
{
private
String
TABLE_NAME
;
private
String
TABLE_COMMENT
;
private
int
TABLE_ROWS
;
private
int
TABLE_COLUMN
;
public
String
getTABLE_NAME
()
{
return
TABLE_NAME
;
}
public
void
setTABLE_NAME
(
String
TABLE_NAME
)
{
this
.
TABLE_NAME
=
TABLE_NAME
;
}
public
String
getTABLE_COMMENT
()
{
return
TABLE_COMMENT
;
}
public
void
setTABLE_COMMENT
(
String
TABLE_COMMENT
)
{
this
.
TABLE_COMMENT
=
TABLE_COMMENT
;
}
public
int
getTABLE_ROWS
()
{
return
TABLE_ROWS
;
}
public
void
setTABLE_ROWS
(
int
TABLE_ROWS
)
{
this
.
TABLE_ROWS
=
TABLE_ROWS
;
}
public
int
getTABLE_COLUMN
()
{
return
TABLE_COLUMN
;
}
public
void
setTABLE_COLUMN
(
int
TABLE_COLUMN
)
{
this
.
TABLE_COLUMN
=
TABLE_COLUMN
;
}
@Override
public
String
toString
()
{
return
"TableInfo{"
+
"TABLE_NAME='"
+
TABLE_NAME
+
'\''
+
", TABLE_COMMENT='"
+
TABLE_COMMENT
+
'\''
+
", TABLE_ROWS="
+
TABLE_ROWS
+
", TABLE_COLUMN="
+
TABLE_COLUMN
+
'}'
;
}
}
}
}
src/main/java/cn/com/poc/expose/rest/impl/ContentReportRestImpl.java
View file @
b5d4791b
...
@@ -6,30 +6,16 @@ import cn.com.poc.common.utils.UUIDTool;
...
@@ -6,30 +6,16 @@ import cn.com.poc.common.utils.UUIDTool;
import
cn.com.poc.expose.dto.ContentReportDto
;
import
cn.com.poc.expose.dto.ContentReportDto
;
import
cn.com.poc.expose.rest.ContentReportRest
;
import
cn.com.poc.expose.rest.ContentReportRest
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.hutool.Hutool
;
import
cn.hutool.poi.word.Word07Writer
;
import
com.itextpdf.text.pdf.PdfWriter
;
import
com.vladsch.flexmark.html.HtmlRenderer
;
import
com.vladsch.flexmark.html.HtmlRenderer
;
import
com.vladsch.flexmark.parser.Parser
;
import
com.vladsch.flexmark.parser.Parser
;
import
com.vladsch.flexmark.util.ast.Document
;
import
com.vladsch.flexmark.util.ast.Document
;
import
com.vladsch.flexmark.util.ast.Node
;
import
org.apache.pdfbox.io.RandomAccessBuffer
;
import
org.apache.pdfbox.io.RandomAccessBufferedFileInputStream
;
import
org.apache.pdfbox.pdfparser.PDFParser
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.poi.openxml4j.exceptions.InvalidFormatException
;
import
org.apache.poi.poifs.filesystem.DirectoryEntry
;
import
org.apache.poi.poifs.filesystem.DirectoryEntry
;
import
org.apache.poi.poifs.filesystem.DocumentEntry
;
import
org.apache.poi.poifs.filesystem.DocumentEntry
;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem
;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem
;
import
org.apache.poi.util.Units
;
import
org.apache.poi.xwpf.usermodel.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.awt.*
;
import
java.io.*
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Base64
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
...
@@ -80,18 +66,6 @@ public class ContentReportRestImpl implements ContentReportRest {
...
@@ -80,18 +66,6 @@ public class ContentReportRestImpl implements ContentReportRest {
}
}
// private String convertHtmlToWord(String html) throws IOException {
// File file = File.createTempFile(UUIDTool.getUUID(), ".docx");
// Word07Writer writer = new Word07Writer();
// writer.addText(new Font("宋体", Font.PLAIN, 10), html);
// writer.flush(file);
// FileInputStream fileInputStream = new FileInputStream(file);
// String upload = bosConfigService.upload(fileInputStream, "docx", "application/msword");
// file.deleteOnExit();
// fileInputStream.close();
// return upload;
// }
/**
/**
* @param content 富文本内容转word
* @param content 富文本内容转word
* @throws Exception
* @throws Exception
...
...
src/main/java/cn/com/poc/knowledge/cache/DatabaseTableInfoCache.java
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
cache
;
package
cn
.
com
.
poc
.
knowledge
.
cache
;
import
cn.com.poc.common.domain.database.TableInfo
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.service.RedisService
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.SpringUtils
;
import
cn.com.poc.common.utils.SpringUtils
;
...
@@ -20,16 +21,16 @@ public class DatabaseTableInfoCache {
...
@@ -20,16 +21,16 @@ public class DatabaseTableInfoCache {
private
static
Long
expireTime
=
5
*
60L
;
private
static
Long
expireTime
=
5
*
60L
;
public
static
boolean
updateOrSaveCache
(
int
id
,
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
)
{
public
static
boolean
updateOrSaveCache
(
int
id
,
List
<
TableInfo
>
tableInfos
)
{
logger
.
info
(
"updateOrSaveCache id:{},tableInfos:{}"
,
id
,
tableInfos
);
logger
.
info
(
"updateOrSaveCache id:{},tableInfos:{}"
,
id
,
tableInfos
);
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
return
redisService
.
set
(
prefix
+
id
,
tableInfos
,
expireTime
);
return
redisService
.
set
(
prefix
+
id
,
tableInfos
,
expireTime
);
}
}
public
static
List
<
DatabaseUtil
.
TableInfo
>
getCache
(
int
id
)
{
public
static
List
<
TableInfo
>
getCache
(
int
id
)
{
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
RedisService
redisService
=
SpringUtils
.
getBean
(
RedisService
.
class
);
if
(
redisService
.
hasKey
(
prefix
+
id
))
{
if
(
redisService
.
hasKey
(
prefix
+
id
))
{
return
(
List
<
DatabaseUtil
.
TableInfo
>)
redisService
.
get
(
prefix
+
id
);
return
(
List
<
TableInfo
>)
redisService
.
get
(
prefix
+
id
);
}
}
return
null
;
return
null
;
}
}
...
...
src/main/java/cn/com/poc/knowledge/dto/BizKnowledgeDatabaseDto.java
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
dto
;
package
cn
.
com
.
poc
.
knowledge
.
dto
;
import
cn.com.poc.common.domain.database.TableInfo
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
java.util.List
;
import
java.util.List
;
...
@@ -167,13 +168,13 @@ public class BizKnowledgeDatabaseDto {
...
@@ -167,13 +168,13 @@ public class BizKnowledgeDatabaseDto {
* tableInfos
* tableInfos
* 数据库表信息
* 数据库表信息
*/
*/
private
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
;
private
List
<
TableInfo
>
tableInfos
;
public
List
<
DatabaseUtil
.
TableInfo
>
getTableInfos
()
{
public
List
<
TableInfo
>
getTableInfos
()
{
return
tableInfos
;
return
tableInfos
;
}
}
public
void
setTableInfos
(
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
)
{
public
void
setTableInfos
(
List
<
TableInfo
>
tableInfos
)
{
this
.
tableInfos
=
tableInfos
;
this
.
tableInfos
=
tableInfos
;
}
}
}
}
\ No newline at end of file
src/main/java/cn/com/poc/knowledge/dto/DatabaseTableDataDto.java
0 → 100644
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
dto
;
import
cn.com.poc.common.domain.database.CellData
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
java.util.List
;
/**
* @author alex.yao
* @date 2025/5/28
*/
public
class
DatabaseTableDataDto
{
private
List
<
String
>
column
;
private
List
<
List
<
CellData
>>
tableDatas
;
private
PagingInfo
pageInfo
;
public
List
<
String
>
getColumn
()
{
return
column
;
}
public
void
setColumn
(
List
<
String
>
column
)
{
this
.
column
=
column
;
}
public
List
<
List
<
CellData
>>
getTableDatas
()
{
return
tableDatas
;
}
public
void
setTableDatas
(
List
<
List
<
CellData
>>
tableDatas
)
{
this
.
tableDatas
=
tableDatas
;
}
public
PagingInfo
getPageInfo
()
{
return
pageInfo
;
}
public
void
setPageInfo
(
PagingInfo
pageInfo
)
{
this
.
pageInfo
=
pageInfo
;
}
}
src/main/java/cn/com/poc/knowledge/dto/DatabaseTableDataResponseDto.java
0 → 100644
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
dto
;
import
org.springframework.web.bind.annotation.RequestParam
;
/**
* @author alex.yao
* @date 2025/5/28
*/
public
class
DatabaseTableDataResponseDto
{
private
Integer
id
;
private
String
table
;
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
String
getTable
()
{
return
table
;
}
public
void
setTable
(
String
table
)
{
this
.
table
=
table
;
}
}
src/main/java/cn/com/poc/knowledge/rest/DatabaseRest.java
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
rest
;
package
cn
.
com
.
poc
.
knowledge
.
rest
;
import
cn.com.poc.knowledge.dto.BizDatabaseTableInfoDto
;
import
cn.com.poc.knowledge.dto.*
;
import
cn.com.poc.knowledge.dto.BizKnowledgeDatabaseDto
;
import
cn.com.poc.knowledge.dto.DatabaseTestContentDto
;
import
cn.com.poc.knowledge.dto.KnowledgeDatabaseSearchDto
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDatabaseEntity
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDatabaseEntity
;
import
cn.com.yict.framemax.core.rest.BaseRest
;
import
cn.com.yict.framemax.core.rest.BaseRest
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
...
@@ -83,5 +80,13 @@ public interface DatabaseRest extends BaseRest {
...
@@ -83,5 +80,13 @@ public interface DatabaseRest extends BaseRest {
*/
*/
List
<
BizDatabaseTableInfoDto
>
getTableInfo
(
@RequestParam
Integer
id
);
List
<
BizDatabaseTableInfoDto
>
getTableInfo
(
@RequestParam
Integer
id
);
/**
* 获取数据库表数据
*
* @param dto 数据库表数据查询条件
* @return 数据库表数据
*/
DatabaseTableDataDto
getTableData
(
@RequestBody
DatabaseTableDataResponseDto
dto
,
PagingInfo
pagingInfo
);
}
}
\ No newline at end of file
src/main/java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
View file @
b5d4791b
package
cn
.
com
.
poc
.
knowledge
.
rest
.
impl
;
package
cn
.
com
.
poc
.
knowledge
.
rest
.
impl
;
import
cn.com.poc.common.domain.database.TableDataResponse
;
import
cn.com.poc.common.domain.database.TableInfo
;
import
cn.com.poc.common.utils.Assert
;
import
cn.com.poc.common.utils.Assert
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.BlContext
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.StringUtils
;
import
cn.com.poc.common.utils.StringUtils
;
import
cn.com.poc.knowledge.cache.DatabaseTableInfoCache
;
import
cn.com.poc.knowledge.cache.DatabaseTableInfoCache
;
import
cn.com.poc.knowledge.convert.BizKnowledgeDatabaseConvert
;
import
cn.com.poc.knowledge.convert.BizKnowledgeDatabaseConvert
;
import
cn.com.poc.knowledge.dto.BizDatabaseTableInfoDto
;
import
cn.com.poc.knowledge.dto.*
;
import
cn.com.poc.knowledge.dto.BizKnowledgeDatabaseDto
;
import
cn.com.poc.knowledge.dto.DatabaseTestContentDto
;
import
cn.com.poc.knowledge.dto.KnowledgeDatabaseSearchDto
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDatabaseEntity
;
import
cn.com.poc.knowledge.entity.BizKnowledgeDatabaseEntity
;
import
cn.com.poc.knowledge.query.KnowledgeDatabaseQueryInfoQueryCondition
;
import
cn.com.poc.knowledge.query.KnowledgeDatabaseQueryInfoQueryCondition
;
import
cn.com.poc.knowledge.query.KnowledgeDatabaseQueryInfoQueryItem
;
import
cn.com.poc.knowledge.query.KnowledgeDatabaseQueryInfoQueryItem
;
...
@@ -19,6 +18,7 @@ import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
...
@@ -19,6 +18,7 @@ import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
cn.com.yict.framemax.data.model.PagingInfo
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
...
@@ -44,7 +44,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -44,7 +44,7 @@ public class DatabaseRestImpl implements DatabaseRest {
Assert
.
notBlank
(
dto
.
getDbUsername
(),
"Database Username must not be blank"
);
Assert
.
notBlank
(
dto
.
getDbUsername
(),
"Database Username must not be blank"
);
Assert
.
notBlank
(
dto
.
getDbPassword
(),
"Database Password must not be blank"
);
Assert
.
notBlank
(
dto
.
getDbPassword
(),
"Database Password must not be blank"
);
boolean
connect
=
DatabaseUtil
.
testConnect
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
boolean
connect
=
DatabaseUtil
.
testConnect
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
List
<
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
if
(!
connect
||
tableInfos
==
null
)
{
if
(!
connect
||
tableInfos
==
null
)
{
throw
new
BusinessException
(
"Database connection failed"
);
throw
new
BusinessException
(
"Database connection failed"
);
}
}
...
@@ -85,7 +85,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -85,7 +85,7 @@ public class DatabaseRestImpl implements DatabaseRest {
}
}
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
);
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
);
//获取表信息
//获取表信息
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
id
);
List
<
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
id
);
if
(
tableInfos
==
null
)
{
if
(
tableInfos
==
null
)
{
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
...
@@ -113,7 +113,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -113,7 +113,7 @@ public class DatabaseRestImpl implements DatabaseRest {
}
}
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
);
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
);
//获取表信息
//获取表信息
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
id
);
List
<
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
id
);
if
(
tableInfos
==
null
)
{
if
(
tableInfos
==
null
)
{
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
...
@@ -144,7 +144,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -144,7 +144,7 @@ public class DatabaseRestImpl implements DatabaseRest {
if
(
CollectionUtils
.
isNotEmpty
(
items
))
{
if
(
CollectionUtils
.
isNotEmpty
(
items
))
{
for
(
KnowledgeDatabaseQueryInfoQueryItem
item
:
items
)
{
for
(
KnowledgeDatabaseQueryInfoQueryItem
item
:
items
)
{
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
itemToDto
(
item
);
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
itemToDto
(
item
);
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
bizKnowledgeDatabaseDto
.
getId
().
intValue
());
List
<
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
bizKnowledgeDatabaseDto
.
getId
().
intValue
());
if
(
tableInfos
==
null
)
{
if
(
tableInfos
==
null
)
{
tableInfos
=
DatabaseUtil
.
getTableInfo
(
item
.
getDbHost
(),
tableInfos
=
DatabaseUtil
.
getTableInfo
(
item
.
getDbHost
(),
item
.
getDbPort
(),
item
.
getDbPort
(),
...
@@ -185,7 +185,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -185,7 +185,7 @@ public class DatabaseRestImpl implements DatabaseRest {
Assert
.
notNull
(
dto
.
getId
(),
"id can not be null"
);
Assert
.
notNull
(
dto
.
getId
(),
"id can not be null"
);
boolean
connect
=
DatabaseUtil
.
testConnect
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
boolean
connect
=
DatabaseUtil
.
testConnect
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbName
());
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
dto
.
getDbHost
(),
List
<
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
dto
.
getDbHost
(),
dto
.
getDbPort
(),
dto
.
getDbPort
(),
dto
.
getDbUsername
(),
dto
.
getDbUsername
(),
dto
.
getDbPassword
(),
dto
.
getDbPassword
(),
...
@@ -214,7 +214,7 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -214,7 +214,7 @@ public class DatabaseRestImpl implements DatabaseRest {
if
(
bizKnowledgeDatabaseEntity
==
null
||
!
bizKnowledgeDatabaseEntity
.
getMemberId
().
equals
(
userId
.
intValue
()))
{
if
(
bizKnowledgeDatabaseEntity
==
null
||
!
bizKnowledgeDatabaseEntity
.
getMemberId
().
equals
(
userId
.
intValue
()))
{
throw
new
BusinessException
(
"no database found"
);
throw
new
BusinessException
(
"no database found"
);
}
}
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
List
<
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbUsername
(),
bizKnowledgeDatabaseEntity
.
getDbUsername
(),
bizKnowledgeDatabaseEntity
.
getDbPassword
(),
bizKnowledgeDatabaseEntity
.
getDbPassword
(),
...
@@ -233,4 +233,23 @@ public class DatabaseRestImpl implements DatabaseRest {
...
@@ -233,4 +233,23 @@ public class DatabaseRestImpl implements DatabaseRest {
return
dto
;
return
dto
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
}
}
@Override
public
DatabaseTableDataDto
getTableData
(
DatabaseTableDataResponseDto
dto
,
PagingInfo
pagingInfo
)
{
Assert
.
notNull
(
dto
.
getId
(),
"id can not be null"
);
Assert
.
notNull
(
dto
.
getTable
(),
"table can not be null"
);
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
Long
userId
=
userBaseEntity
.
getUserId
();
BizKnowledgeDatabaseEntity
bizKnowledgeDatabaseEntity
=
bizKnowledgeDatabaseService
.
get
(
dto
.
getId
().
longValue
());
if
(
bizKnowledgeDatabaseEntity
==
null
||
!
bizKnowledgeDatabaseEntity
.
getMemberId
().
equals
(
userId
.
intValue
()))
{
throw
new
BusinessException
(
"no database found"
);
}
TableDataResponse
tableDataResponse
=
DatabaseUtil
.
getTableData
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbUsername
(),
bizKnowledgeDatabaseEntity
.
getDbPassword
(),
bizKnowledgeDatabaseEntity
.
getDbName
(),
dto
.
getTable
(),
pagingInfo
.
getPageNo
(),
pagingInfo
.
getPageSize
());
DatabaseTableDataDto
result
=
new
DatabaseTableDataDto
();
BeanUtils
.
copyProperties
(
tableDataResponse
,
result
);
return
result
;
}
}
}
\ No newline at end of file
src/test/java/cn/com/poc/utils/DatabaseUtilTest.java
View file @
b5d4791b
...
@@ -10,8 +10,6 @@ import org.springframework.test.context.ContextConfiguration;
...
@@ -10,8 +10,6 @@ import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
java.sql.SQLException
;
/**
/**
* @author alex.yao
* @author alex.yao
* @date 2025/4/27
* @date 2025/4/27
...
@@ -33,17 +31,27 @@ public class DatabaseUtilTest {
...
@@ -33,17 +31,27 @@ public class DatabaseUtilTest {
}
}
@Test
@Test
public
void
test_getTableInfo
()
throws
SQLException
,
ClassNotFoundException
{
public
void
test_getTableData
()
{
String
host
=
"192.168.21.31"
;
int
port
=
3306
;
String
username
=
"poc_root"
;
String
password
=
"8db58a2836dc"
;
String
database
=
"gsst_poc_sit"
;
System
.
out
.
println
(
JsonUtils
.
serialize
(
DatabaseUtil
.
getTableData
(
host
,
port
,
username
,
password
,
database
,
"biz_member_info"
,
3
,
10
)));
}
@Test
public
void
test_getTableInfo
()
{
String
host
=
"192.168.21.31"
;
String
host
=
"192.168.21.31"
;
int
port
=
3306
;
int
port
=
3306
;
String
username
=
"poc_root"
;
String
username
=
"poc_root"
;
String
password
=
"8db58a2836dc"
;
String
password
=
"8db58a2836dc"
;
String
database
=
"gsst_poc_sit"
;
String
database
=
"gsst_poc_sit"
;
DatabaseUtil
.
getTableInfo
(
host
,
port
,
username
,
password
,
database
);
System
.
out
.
println
(
DatabaseUtil
.
getTableStructure
(
host
,
port
,
username
,
password
,
database
,
"biz_member_info"
)
);
}
}
@Test
@Test
public
void
test
(){
public
void
test
()
{
LargeModelDemandResult
result
=
new
LargeModelDemandResult
();
LargeModelDemandResult
result
=
new
LargeModelDemandResult
();
result
.
setCode
(
"0"
);
result
.
setCode
(
"0"
);
result
.
setFunction
(
null
);
result
.
setFunction
(
null
);
...
...
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