Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
80f52bb6
Commit
80f52bb6
authored
May 18, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将源码打包进jar包
parent
9a92cec7
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
191 additions
and
87 deletions
+191
-87
DbExecute.java
yzg-util-db/src/main/java/com/yanzuoguang/db/DbExecute.java
+6
-6
DbExecuteImpl.java
.../src/main/java/com/yanzuoguang/db/impl/DbExecuteImpl.java
+61
-67
DbPrintSql.java
...-db/src/main/java/com/yanzuoguang/db/impl/DbPrintSql.java
+13
-14
DbSqlFunction.java
.../src/main/java/com/yanzuoguang/db/impl/DbSqlFunction.java
+43
-0
SqlInfo.java
...til-db/src/main/java/com/yanzuoguang/db/impl/SqlInfo.java
+68
-0
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/db/DbExecute.java
View file @
80f52bb6
...
...
@@ -19,9 +19,9 @@ public interface DbExecute {
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return
* @return
影响行数
*/
int
update
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
int
update
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
...
...
@@ -36,7 +36,7 @@ public interface DbExecute {
* @param <T> 返回的集合的类型
* @return 集合
*/
<
T
extends
Object
>
void
query
(
Class
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
);
<
T
extends
Object
>
void
query
(
Class
<?>
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询数据,并返回集合
...
...
@@ -49,7 +49,7 @@ public interface DbExecute {
* @param <T> 返回的集合的类型
* @return 集合
*/
<
T
>
List
<
T
>
query
(
Class
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
);
<
T
>
List
<
T
>
query
(
Class
<?>
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询数据,并返回集合
...
...
@@ -60,7 +60,7 @@ public interface DbExecute {
* @param paras 查询语句的参数
* @return 集合
*/
List
<
MapRow
>
query
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
List
<
MapRow
>
query
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询第一个单元格的信息
...
...
@@ -71,5 +71,5 @@ public interface DbExecute {
* @param paras 参数信息
* @return 第一个单元格的数据
*/
Object
queryCell
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
Object
queryCell
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
}
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/DbExecuteImpl.java
View file @
80f52bb6
...
...
@@ -42,20 +42,16 @@ public class DbExecuteImpl implements DbExecute {
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return
* @return
影响行数
*/
@Override
public
int
update
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
Ref
<
Integer
>
row
=
new
Ref
<>(
0
);
long
start
=
System
.
currentTimeMillis
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
String
finalSql
=
sql
;
YzgTimeout
.
timeOut
(
targetClass
,
sqlName
,
()
->
row
.
value
=
getJdbc
().
update
(
finalSql
,
paras
));
return
row
.
value
;
}
finally
{
printSql
.
print
(
targetClass
,
sqlName
,
start
,
row
.
value
,
sql
,
paras
);
}
public
int
update
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
SqlInfo
sqlInfo
=
new
SqlInfo
(
targetClass
,
sqlName
,
sql
,
paras
);
Ref
<
Integer
>
ret
=
new
Ref
<>(
0
);
executeSql
(
sqlInfo
,
(
row
,
start
)
->
ret
.
value
=
getJdbc
().
update
(
sqlInfo
.
getSql
(),
sqlInfo
.
getParas
())
);
return
ret
.
value
;
}
/**
...
...
@@ -70,25 +66,17 @@ public class DbExecuteImpl implements DbExecute {
* @param <T> 返回的集合的类型
*/
@Override
public
<
T
extends
Object
>
void
query
(
Class
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
Ref
<
Integer
>
row
=
new
Ref
<
Integer
>(
0
);
long
start
=
System
.
currentTimeMillis
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
String
finalSql
=
sql
;
YzgTimeout
.
timeOut
(
targetClass
,
sqlName
,
()
->
{
RowCallbackHandler
rowCallbackHandler
=
rs
->
{
AllBeanRowMapper
<
T
>
rowMap
=
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
);
T
data
=
rowMap
.
mapRow
(
rs
,
row
.
value
);
rowHandle
.
handle
(
data
);
row
.
value
++;
};
this
.
getJdbc
().
query
(
finalSql
,
rowCallbackHandler
,
paras
);
});
}
finally
{
printSql
.
print
(
targetClass
,
sqlName
,
start
,
row
.
value
,
sql
,
paras
);
}
public
<
T
extends
Object
>
void
query
(
Class
<?>
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
SqlInfo
sqlInfo
=
new
SqlInfo
(
targetClass
,
sqlName
,
sql
,
paras
);
executeSql
(
sqlInfo
,
(
row
,
start
)
->
{
RowCallbackHandler
rowCallbackHandler
=
rs
->
{
AllBeanRowMapper
<
T
>
rowMap
=
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
);
T
data
=
rowMap
.
mapRow
(
rs
,
row
.
value
);
rowHandle
.
handle
(
data
);
row
.
value
++;
};
this
.
getJdbc
().
query
(
sqlInfo
.
getSql
(),
rowCallbackHandler
,
sqlInfo
.
getParas
());
});
}
/**
...
...
@@ -103,25 +91,17 @@ public class DbExecuteImpl implements DbExecute {
* @return 集合
*/
@Override
public
<
T
extends
Object
>
List
<
T
>
query
(
Class
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
int
row
=
0
;
long
start
=
System
.
currentTimeMillis
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
Ref
<
List
<
T
>>
ret
=
new
Ref
<>(
null
);
String
finalSql
=
sql
;
YzgTimeout
.
timeOut
(
targetClass
,
sqlName
,
()
->
{
ret
.
value
=
this
.
getJdbc
().
query
(
finalSql
,
paras
,
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
));
});
public
<
T
extends
Object
>
List
<
T
>
query
(
Class
<?>
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
SqlInfo
sqlInfo
=
new
SqlInfo
(
targetClass
,
sqlName
,
sql
,
paras
);
Ref
<
List
<
T
>>
ret
=
new
Ref
<>(
null
);
executeSql
(
sqlInfo
,
(
row
,
start
)
->
{
ret
.
value
=
this
.
getJdbc
().
query
(
sqlInfo
.
getSql
(),
sqlInfo
.
getParas
(),
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
));
if
(
ret
.
value
==
null
)
{
ret
.
value
=
new
ArrayList
<
T
>();
}
row
=
ret
.
value
.
size
();
return
ret
.
value
;
}
finally
{
printSql
.
print
(
targetClass
,
sqlName
,
start
,
row
,
sql
,
paras
);
}
row
.
value
=
ret
.
value
.
size
();
});
return
ret
.
value
;
}
/**
...
...
@@ -134,7 +114,7 @@ public class DbExecuteImpl implements DbExecute {
* @return 集合
*/
@Override
public
List
<
MapRow
>
query
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
public
List
<
MapRow
>
query
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
return
query
(
targetClass
,
MapRow
.
class
,
sqlName
,
sql
,
paras
);
}
...
...
@@ -148,36 +128,50 @@ public class DbExecuteImpl implements DbExecute {
* @return 第一个单元格的数据
*/
@Override
public
Object
queryCell
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
public
Object
queryCell
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
SqlInfo
sqlInfo
=
new
SqlInfo
(
targetClass
,
sqlName
,
sql
,
paras
);
Ref
<
Object
>
ret
=
new
Ref
<>(
null
);
executeSql
(
sqlInfo
,
(
row
,
start
)
->
{
SqlRowSet
rowSet
=
getJdbc
().
queryForRowSet
(
sqlInfo
.
getSql
(),
sqlInfo
.
getParas
());
if
(
rowSet
.
next
())
{
row
.
value
=
1
;
ret
.
value
=
rowSet
.
getObject
(
1
);
}
});
return
ret
.
value
;
}
/**
* 执行sql语句
*
* @param sqlInfo sql语句信息
* @param sqlFunction 执行函数,传入参数为影响的行数引用和开始执行时间
*/
private
void
executeSql
(
SqlInfo
sqlInfo
,
DbSqlFunction
<
Ref
<
Integer
>,
Long
>
sqlFunction
)
{
Ref
<
Integer
>
row
=
new
Ref
<>(
0
);
long
start
=
System
.
currentTimeMillis
();
sqlInfo
.
setSql
(
this
.
handleParas
(
sqlInfo
.
getSql
()));
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
Ref
<
Object
>
ret
=
new
Ref
<>(
null
);
String
finalSql
=
sql
;
YzgTimeout
.
timeOut
(
targetClass
,
sqlName
,
()
->
{
SqlRowSet
rowSet
=
this
.
getJdbc
().
queryForRowSet
(
finalSql
,
paras
);
while
(
rowSet
.
next
())
{
row
.
value
=
1
;
ret
.
value
=
rowSet
.
getObject
(
1
);
break
;
}
YzgTimeout
.
timeOut
(
sqlInfo
.
getTargetClass
(),
sqlInfo
.
getSqlName
(),
()
->
{
sqlFunction
.
accept
(
row
,
start
);
});
return
null
;
}
finally
{
printSql
.
print
(
targetClass
,
sqlName
,
start
,
row
.
value
,
sql
,
paras
);
printSql
.
print
(
sqlInfo
,
start
,
row
.
value
);
}
}
/**
* 处理SQL语句和参数值
*
* @param sql
* @param paras
* @return
* @param sql 处理sql语句
* @return 可执行的sql语句
*/
protected
String
handleParas
(
String
sql
,
Object
...
paras
)
{
return
sql
.
replaceAll
(
"1\\s*?=\\s*?1\\s*?(?i)AND"
,
""
).
replaceAll
(
"(?i)WHERE\\s*?1\\s*?=\\s*?1"
,
""
).
replaceAll
(
"((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,"
,
"$1"
).
replaceAll
(
"(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?"
,
""
).
replaceAll
(
"((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,"
,
"$1"
).
replaceAll
(
"(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?"
,
""
);
protected
String
handleParas
(
String
sql
)
{
return
sql
.
replaceAll
(
"1\\s*?=\\s*?1\\s*?(?i)AND"
,
""
)
.
replaceAll
(
"(?i)WHERE\\s*?1\\s*?=\\s*?1"
,
""
)
.
replaceAll
(
"((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,"
,
"$1"
)
.
replaceAll
(
"(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?"
,
""
)
.
replaceAll
(
"((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,"
,
"$1"
)
.
replaceAll
(
"(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?"
,
""
);
}
}
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/DbPrintSql.java
View file @
80f52bb6
...
...
@@ -3,7 +3,6 @@ package com.yanzuoguang.db.impl;
import
com.yanzuoguang.db.ConfigDb
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
/**
...
...
@@ -14,8 +13,11 @@ import org.springframework.stereotype.Component;
@Component
public
class
DbPrintSql
{
@Autowired
private
ConfigDb
configDb
;
private
final
ConfigDb
configDb
;
public
DbPrintSql
(
ConfigDb
configDb
)
{
this
.
configDb
=
configDb
;
}
/**
* 获取参数处理之后的SQL语句
...
...
@@ -61,30 +63,27 @@ public class DbPrintSql {
/**
* 打印SQL语句
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param start 开始执行的语句
* @param row SQL语句
* @param sql SQL语句
* @param paras 参数
* @param sqlInfo sql语句嘻嘻你
* @param start 开始执行的语句
* @param row SQL语句
*/
public
void
print
(
Class
targetClass
,
String
sqlName
,
long
start
,
int
row
,
String
sql
,
Object
...
paras
)
{
public
void
print
(
SqlInfo
sqlInfo
,
long
start
,
int
row
)
{
try
{
// 日志表忽略打印
if
(!
configDb
.
isPrintSql
())
{
return
;
}
if
(!
StringHelper
.
isEmpty
(
configDb
.
getPrintSqlFilter
()))
{
if
(
sql
.
matches
(
configDb
.
getPrintSqlFilter
()))
{
if
(
sql
Info
.
getSql
()
.
matches
(
configDb
.
getPrintSqlFilter
()))
{
return
;
}
}
sql
=
getStringSql
(
sql
,
paras
);
String
sql
=
getStringSql
(
sqlInfo
.
getSql
(),
sqlInfo
.
getParas
());
long
end
=
System
.
currentTimeMillis
();
long
time
=
end
-
start
;
// 打印SQL语句
Log
.
infoTag
(
DbPrintSql
.
class
,
String
.
format
(
"%d row %d ms %s.%s"
,
row
,
time
,
targetClass
.
getSimpleName
(),
sqlName
),
sql
);
String
tag
=
String
.
format
(
"%d row %d ms %s.%s"
,
row
,
time
,
sqlInfo
.
getTargetClass
().
getSimpleName
(),
sqlInfo
.
getSqlName
());
Log
.
infoTag
(
DbPrintSql
.
class
,
tag
,
sql
);
}
catch
(
Exception
ex
)
{
Log
.
error
(
DbPrintSql
.
class
,
ex
);
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/DbSqlFunction.java
0 → 100644
View file @
80f52bb6
package
com
.
yanzuoguang
.
db
.
impl
;
import
java.util.Objects
;
/**
* 处理2个参数的函数接口
*
* @param <H> 影响的函数
* @param <I> 开始执行的时间
* @author 颜佐光
*/
@FunctionalInterface
public
interface
DbSqlFunction
<
H
,
I
>
{
/**
* Performs this operation on the given argument.
*
* @param row the input argument
* @param start the input argument
*/
void
accept
(
H
row
,
I
start
);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default
DbSqlFunction
<
H
,
I
>
andThen
(
DbSqlFunction
<?
super
H
,
?
super
I
>
after
)
{
Objects
.
requireNonNull
(
after
);
return
(
H
h
,
I
i
)
->
{
accept
(
h
,
i
);
after
.
accept
(
h
,
i
);
};
}
}
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/SqlInfo.java
0 → 100644
View file @
80f52bb6
package
com
.
yanzuoguang
.
db
.
impl
;
/**
* Sql语句信息
*
* @author 颜佐光
*/
public
class
SqlInfo
{
/**
* 触发的类
*/
private
Class
<?>
targetClass
;
/**
* Sql语句的名称
*/
private
String
sqlName
;
/**
* 执行的sql语句
*/
private
String
sql
;
/**
* 执行的参数
*/
private
Object
[]
paras
;
public
SqlInfo
(
Class
<?>
targetClass
,
String
sqlName
,
String
sql
,
Object
[]
paras
)
{
this
.
targetClass
=
targetClass
;
this
.
sqlName
=
sqlName
;
this
.
sql
=
sql
;
this
.
paras
=
paras
;
}
public
Class
<?>
getTargetClass
()
{
return
targetClass
;
}
public
void
setTargetClass
(
Class
<?>
targetClass
)
{
this
.
targetClass
=
targetClass
;
}
public
String
getSqlName
()
{
return
sqlName
;
}
public
void
setSqlName
(
String
sqlName
)
{
this
.
sqlName
=
sqlName
;
}
public
String
getSql
()
{
return
sql
;
}
public
void
setSql
(
String
sql
)
{
this
.
sql
=
sql
;
}
public
Object
[]
getParas
()
{
return
paras
;
}
public
void
setParas
(
Object
[]
paras
)
{
this
.
paras
=
paras
;
}
}
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