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
4652b4fc
Commit
4652b4fc
authored
Apr 29, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: database数据表查询
parent
5c14255a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
216 additions
and
12 deletions
+216
-12
DatabaseUtil.java
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
+95
-12
BizDatabaseTableInfoDto.java
...ava/cn/com/poc/knowledge/dto/BizDatabaseTableInfoDto.java
+47
-0
DatabaseRest.java
src/main/java/cn/com/poc/knowledge/rest/DatabaseRest.java
+17
-0
DatabaseRestImpl.java
...java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
+45
-0
DatabaseUtilTest.java
src/test/java/cn/com/poc/utils/DatabaseUtilTest.java
+12
-0
No files found.
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
View file @
4652b4fc
package
cn
.
com
.
poc
.
common
.
utils
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* @author alex.yao
...
...
@@ -16,7 +21,19 @@ public class DatabaseUtil {
private
static
final
int
CONNECT_TIMEOUT
=
5000
;
// 连接超时时间(毫秒)
private
static
final
int
SOCKET_TIMEOUT
=
5000
;
// socket超时时间(毫秒)
private
static
final
int
SOCKET_TIMEOUT
=
30000
;
// socket超时时间(毫秒)
private
static
final
String
SQL_TABLE_INFO
=
"SELECT "
+
" TABLE_NAME ,"
+
" TABLE_COMMENT,"
+
" TABLE_ROWS,"
+
" (SELECT COUNT(*) "
+
" FROM INFORMATION_SCHEMA.COLUMNS "
+
" WHERE TABLE_SCHEMA = t.TABLE_SCHEMA "
+
" AND TABLE_NAME = t.TABLE_NAME) AS TABLE_COLUMN "
+
"FROM INFORMATION_SCHEMA.TABLES t "
+
"WHERE TABLE_SCHEMA = ?"
;
;
/**
* 测试数据库连接
...
...
@@ -29,24 +46,90 @@ public class DatabaseUtil {
* @return
*/
public
static
boolean
testConnect
(
String
host
,
int
port
,
String
username
,
String
password
,
String
database
)
{
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
// 连接超时(毫秒)
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
// socket超时(毫秒)
Connection
connection
=
null
;
try
{
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
try
(
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
))
{
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
}
finally
{
if
(
connection
!=
null
)
{
try
{
connection
.
close
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"关闭数据库连接失败"
,
e
);
}
}
}
/**
* 获取数据库表列表
*/
public
static
List
<
TableInfo
>
getTableInfo
(
String
host
,
int
port
,
String
username
,
String
password
,
String
database
)
{
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
// 连接超时(毫秒)
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
// socket超时(毫秒)
try
(
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
))
{
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
SQL_TABLE_INFO
);
preparedStatement
.
setString
(
1
,
database
);
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
);
}
System
.
out
.
println
(
tableInfoList
);
return
tableInfoList
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"连接数据库失败"
,
e
);
throw
new
BusinessException
(
"连接数据库失败"
);
}
}
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/knowledge/dto/BizDatabaseTableInfoDto.java
0 → 100644
View file @
4652b4fc
package
cn
.
com
.
poc
.
knowledge
.
dto
;
import
java.io.Serializable
;
/**
* @author alex.yao
* @date 2025/4/29
*/
public
class
BizDatabaseTableInfoDto
implements
Serializable
{
private
String
tableName
;
private
String
tableComment
;
private
int
tableRows
;
private
int
tableColumn
;
public
String
getTableName
()
{
return
tableName
;
}
public
void
setTableName
(
String
tableName
)
{
this
.
tableName
=
tableName
;
}
public
String
getTableComment
()
{
return
tableComment
;
}
public
void
setTableComment
(
String
tableComment
)
{
this
.
tableComment
=
tableComment
;
}
public
int
getTableRows
()
{
return
tableRows
;
}
public
void
setTableRows
(
int
tableRows
)
{
this
.
tableRows
=
tableRows
;
}
public
int
getTableColumn
()
{
return
tableColumn
;
}
public
void
setTableColumn
(
int
tableColumn
)
{
this
.
tableColumn
=
tableColumn
;
}
}
src/main/java/cn/com/poc/knowledge/rest/DatabaseRest.java
View file @
4652b4fc
package
cn
.
com
.
poc
.
knowledge
.
rest
;
import
cn.com.poc.knowledge.dto.BizDatabaseTableInfoDto
;
import
cn.com.poc.knowledge.dto.BizKnowledgeDatabaseDto
;
import
cn.com.poc.knowledge.dto.DatabaseTestContentDto
;
import
cn.com.poc.knowledge.dto.KnowledgeDatabaseSearchDto
;
...
...
@@ -40,6 +41,14 @@ public interface DatabaseRest extends BaseRest {
*/
BizKnowledgeDatabaseDto
get
(
@RequestParam
Integer
id
);
/**
* 获取数据库连接信息根据ID列表
*
* @param ids
* @return
*/
List
<
BizKnowledgeDatabaseDto
>
getByIds
(
@RequestParam
Integer
[]
ids
);
/**
* 获取数据库连接信息列表
...
...
@@ -66,5 +75,13 @@ public interface DatabaseRest extends BaseRest {
*/
BizKnowledgeDatabaseDto
update
(
@RequestBody
BizKnowledgeDatabaseDto
dto
);
/**
* 获取数据库表详情信息
*
* @param id 数据库信息ID
* @return 数据库表详情信息列表
*/
List
<
BizDatabaseTableInfoDto
>
getTableInfo
(
@RequestParam
Integer
id
);
}
\ No newline at end of file
src/main/java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
View file @
4652b4fc
...
...
@@ -5,6 +5,7 @@ import cn.com.poc.common.utils.BlContext;
import
cn.com.poc.common.utils.DatabaseUtil
;
import
cn.com.poc.common.utils.StringUtils
;
import
cn.com.poc.knowledge.convert.BizKnowledgeDatabaseConvert
;
import
cn.com.poc.knowledge.dto.BizDatabaseTableInfoDto
;
import
cn.com.poc.knowledge.dto.BizKnowledgeDatabaseDto
;
import
cn.com.poc.knowledge.dto.DatabaseTestContentDto
;
import
cn.com.poc.knowledge.dto.KnowledgeDatabaseSearchDto
;
...
...
@@ -80,6 +81,22 @@ public class DatabaseRestImpl implements DatabaseRest {
return
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
);
}
@Override
public
List
<
BizKnowledgeDatabaseDto
>
getByIds
(
Integer
[]
ids
)
{
Assert
.
notEmpty
(
ids
,
"id can not be null"
);
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
Long
userId
=
userBaseEntity
.
getUserId
();
List
<
BizKnowledgeDatabaseDto
>
result
=
new
ArrayList
<>();
for
(
Integer
id
:
ids
)
{
BizKnowledgeDatabaseEntity
bizKnowledgeDatabaseEntity
=
bizKnowledgeDatabaseService
.
get
(
id
.
longValue
());
if
(
bizKnowledgeDatabaseEntity
==
null
&&
!
bizKnowledgeDatabaseEntity
.
getMemberId
().
equals
(
userId
.
intValue
()))
{
continue
;
}
result
.
add
(
BizKnowledgeDatabaseConvert
.
entityToDto
(
bizKnowledgeDatabaseEntity
));
}
return
result
;
}
@Override
public
List
<
BizKnowledgeDatabaseDto
>
getList
(
KnowledgeDatabaseSearchDto
knowledgeDatabaseSearchDto
,
PagingInfo
pagingInfo
)
{
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
...
...
@@ -124,4 +141,32 @@ public class DatabaseRestImpl implements DatabaseRest {
}
return
BizKnowledgeDatabaseConvert
.
entityToDto
(
updateEntity
);
}
@Override
public
List
<
BizDatabaseTableInfoDto
>
getTableInfo
(
Integer
id
)
{
Assert
.
notNull
(
id
,
"id can not be null"
);
UserBaseEntity
userBaseEntity
=
BlContext
.
getCurrentUserNotException
();
Long
userId
=
userBaseEntity
.
getUserId
();
BizKnowledgeDatabaseEntity
bizKnowledgeDatabaseEntity
=
bizKnowledgeDatabaseService
.
get
(
id
.
longValue
());
if
(
bizKnowledgeDatabaseEntity
==
null
&&
!
bizKnowledgeDatabaseEntity
.
getMemberId
().
equals
(
userId
.
intValue
()))
{
throw
new
BusinessException
(
"no database found"
);
}
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseUtil
.
getTableInfo
(
bizKnowledgeDatabaseEntity
.
getDbHost
(),
bizKnowledgeDatabaseEntity
.
getDbPort
(),
bizKnowledgeDatabaseEntity
.
getDbUsername
(),
bizKnowledgeDatabaseEntity
.
getDbPassword
(),
bizKnowledgeDatabaseEntity
.
getDbName
());
if
(
CollectionUtils
.
isEmpty
(
tableInfos
))
{
throw
new
BusinessException
(
"connection database error"
);
}
return
tableInfos
.
stream
().
map
(
info
->
{
BizDatabaseTableInfoDto
dto
=
new
BizDatabaseTableInfoDto
();
dto
.
setTableName
(
info
.
getTABLE_NAME
());
dto
.
setTableComment
(
info
.
getTABLE_COMMENT
());
dto
.
setTableRows
(
info
.
getTABLE_ROWS
());
dto
.
setTableColumn
(
info
.
getTABLE_COLUMN
());
return
dto
;
}).
collect
(
Collectors
.
toList
());
}
}
\ No newline at end of file
src/test/java/cn/com/poc/utils/DatabaseUtilTest.java
View file @
4652b4fc
...
...
@@ -8,6 +8,8 @@ import org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
java.sql.SQLException
;
/**
* @author alex.yao
* @date 2025/4/27
...
...
@@ -28,4 +30,14 @@ public class DatabaseUtilTest {
System
.
out
.
println
(
DatabaseUtil
.
testConnect
(
host
,
port
,
username
,
password
,
database
));
}
@Test
public
void
test_getTableInfo
()
throws
SQLException
,
ClassNotFoundException
{
String
host
=
"192.168.21.31"
;
int
port
=
3306
;
String
username
=
"poc_root"
;
String
password
=
"8db58a2836dc"
;
String
database
=
"gsst_poc_sit"
;
DatabaseUtil
.
getTableInfo
(
host
,
port
,
username
,
password
,
database
);
}
}
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