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
f995b489
Commit
f995b489
authored
Apr 08, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
需要处理的实体
parent
48e8f9b2
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
204 additions
and
66 deletions
+204
-66
Ref.java
yzg-util-base/src/main/java/com/yanzuoguang/util/vo/Ref.java
+14
-0
BaseDaoImpl.java
...b/src/main/java/com/yanzuoguang/dao/Impl/BaseDaoImpl.java
+1
-1
BaseDaoSql.java
...db/src/main/java/com/yanzuoguang/dao/Impl/BaseDaoSql.java
+63
-29
DbExecute.java
yzg-util-db/src/main/java/com/yanzuoguang/db/DbExecute.java
+41
-14
DbExecuteImpl.java
.../src/main/java/com/yanzuoguang/db/Impl/DbExecuteImpl.java
+66
-18
DbPrintSql.java
...-db/src/main/java/com/yanzuoguang/db/Impl/DbPrintSql.java
+6
-4
DbRow.java
yzg-util-db/src/main/java/com/yanzuoguang/db/Impl/DbRow.java
+13
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/vo/Ref.java
0 → 100644
View file @
f995b489
package
com
.
yanzuoguang
.
util
.
vo
;
/**
* 引用值,方便修改
*
* @param <T>
*/
public
class
Ref
<
T
>
{
public
T
value
;
public
Ref
(
T
value
)
{
this
.
value
=
value
;
}
}
yzg-util-db/src/main/java/com/yanzuoguang/dao/Impl/BaseDaoImpl.java
View file @
f995b489
...
...
@@ -25,7 +25,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return
*/
private
String
getIdentity
()
{
return
StringHelper
.
toString
(
this
.
db
.
queryCell
(
"SELECT @@IDENTITY"
));
return
StringHelper
.
toString
(
this
.
db
.
queryCell
(
this
.
getClass
(),
"GET_KEY"
,
"SELECT @@IDENTITY"
));
}
/**
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/Impl/BaseDaoSql.java
View file @
f995b489
...
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.dao.Impl;
import
com.yanzuoguang.dao.TableAnnotation
;
import
com.yanzuoguang.db.DbExecute
;
import
com.yanzuoguang.db.Impl.DbRow
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.exception.CodeException
;
...
...
@@ -136,6 +137,8 @@ public abstract class BaseDaoSql {
/**
* 当更新数据时
*
* @param model 更新时的数据
*/
protected
void
onUpdateSql
(
Object
model
)
{
// 删除所有查询的缓存
...
...
@@ -145,14 +148,14 @@ public abstract class BaseDaoSql {
/**
* 执行更新语句
*
* @param sqlData
* @param model
* @return
* @param sqlData
需要执行的语句
* @param model
执行的实体
* @return
影响的行数
*/
protected
int
updateSql
(
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
getQueryPara
(
paras
,
sqlData
,
model
);
int
ret
=
db
.
update
(
sql
,
paras
.
toArray
());
int
ret
=
db
.
update
(
this
.
getClass
(),
sqlData
.
Name
,
sql
,
paras
.
toArray
());
this
.
onUpdateSql
(
model
);
return
ret
;
}
...
...
@@ -180,16 +183,16 @@ public abstract class BaseDaoSql {
protected
Object
queryCell
(
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
getQueryPara
(
paras
,
sqlData
,
model
);
Object
cell
=
this
.
queryCellWithCache
(
sql
,
paras
.
toArray
());
Object
cell
=
this
.
queryCellWithCache
(
sql
,
sqlData
.
Name
,
paras
.
toArray
());
return
cell
;
}
/**
* 根据SQL名称获取第一个单元格
*
* @param sqlName
* @param model
* @return
* @param sqlName
需要执行的SQL语句的名称
* @param model
需要转化的实体
* @return
处理参数
*/
protected
Object
queryCell
(
String
sqlName
,
Object
model
)
{
return
this
.
queryCell
(
this
.
getSql
(
sqlName
),
model
);
...
...
@@ -208,10 +211,25 @@ public abstract class BaseDaoSql {
protected
<
T
extends
Object
>
List
<
T
>
queryData
(
Class
<
T
>
cls
,
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
getQueryPara
(
paras
,
sqlData
,
model
);
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sql
,
paras
.
toArray
());
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sql
Data
.
Name
,
sql
,
paras
.
toArray
());
return
list
;
}
/**
* 查询不分页数据
*
* @param cls 数据结果类型
* @param sqlData SQL数据
* @param model 前台参数
* @param <T> 返回数据类型
*/
protected
<
T
extends
Object
>
void
queryData
(
Class
<
T
>
cls
,
DbRow
<
T
>
handle
,
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
getQueryPara
(
paras
,
sqlData
,
model
);
// 查询数据
db
.
query
(
this
.
getClass
(),
cls
,
handle
,
sqlData
.
Name
,
sql
,
paras
);
}
/**
* 查询不分页数据
*
...
...
@@ -226,6 +244,20 @@ public abstract class BaseDaoSql {
return
queryData
(
cls
,
sqlData
,
model
);
}
/**
* 查询不分页数据
*
* @param cls 数据结果类型
* @param handle 需要处理的数据
* @param sqlName SQL语句名称
* @param model 前台参数
* @return 查询的结果
*/
protected
<
T
extends
Object
>
void
query
(
Class
<
T
>
cls
,
DbRow
<
T
>
handle
,
String
sqlName
,
Object
model
)
{
SqlData
sqlData
=
this
.
getSql
(
sqlName
);
queryData
(
cls
,
handle
,
sqlData
,
model
);
}
/**
* 查询第一条数据
*
...
...
@@ -290,7 +322,7 @@ public abstract class BaseDaoSql {
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
getQueryPara
(
paras
,
to
,
model
);
// 查询实体数据
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sql
,
paras
.
toArray
());
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sql
Name
,
sql
,
paras
.
toArray
());
data
.
setList
(
list
);
// 查询分页总条数的SQL语句
...
...
@@ -305,7 +337,7 @@ public abstract class BaseDaoSql {
String
sqlSize
=
"SELECT COUNT(1) FROM ("
+
sqlTo
.
replaceAll
(
"(?i)(ORDER\\s+BY\\s+[^)]+){0,1}(limit\\s+\\d+,\\d+\\s*){0,1}$"
,
""
)
+
") t"
;
data
.
setPageTotal
(
StringHelper
.
toInt
(
queryCellWithCache
(
sqlSize
,
paras
.
toArray
())));
data
.
setPageTotal
(
StringHelper
.
toInt
(
queryCellWithCache
(
String
.
format
(
"%s.Size"
,
sqlName
),
sqlSize
,
paras
.
toArray
())));
return
data
;
}
...
...
@@ -379,9 +411,9 @@ public abstract class BaseDaoSql {
protected
String
getQueryPara
(
List
<
Object
>
paras
,
SqlData
sqlData
,
Object
model
)
{
// 定义可替换片段
String
fromNext
=
"{FIELD_Front}"
;
String
fromPrev
=
"{FIELD}"
;
String
wherePrev
=
"{INNER}"
;
//
String fromNext = "{FIELD_Front}";
//
String fromPrev = "{FIELD}";
//
String wherePrev = "{INNER}";
String
[]
lastCode
=
new
String
[]{
"{WHERE}"
,
"{GROUP}"
,
"{HAVING}"
,
"{ORDER}"
,
"{LIMIT}"
};
// 将SQL语句进行代码片段追加
...
...
@@ -474,8 +506,8 @@ public abstract class BaseDaoSql {
/**
* 获取处理后的参数值
*
* @param val
* @return
* @param val
获取参数值
* @return
处理后的参数值
*/
private
Object
getParaValue
(
Object
val
)
{
if
(
val
instanceof
Boolean
)
{
...
...
@@ -644,9 +676,9 @@ public abstract class BaseDaoSql {
/**
* 将代码片段添加到SQL语句中
*
* @param codeMap
* @param name
* @param code
* @param codeMap
映射关系
* @param name
执行的代码片段
* @param code
代码片段
*/
private
void
addCodeMap
(
Map
<
String
,
List
<
String
>>
codeMap
,
String
name
,
String
code
)
{
if
(!
codeMap
.
containsKey
(
name
))
{
...
...
@@ -659,11 +691,12 @@ public abstract class BaseDaoSql {
/**
* 根据缓存查询第一个单元格
*
* @param sqlName 需要执行的SQL语句名称
* @param sql 需要执行的SQL语句
* @param paras SQL语句的参数
* @return 查询的结果
*/
protected
Object
queryCellWithCache
(
String
sql
,
Object
...
paras
)
{
protected
Object
queryCellWithCache
(
String
sql
Name
,
String
sql
,
Object
...
paras
)
{
// 生成缓存的主键
String
cacheKey
=
getCacheKey
(
Object
.
class
,
sql
,
paras
);
...
...
@@ -677,7 +710,7 @@ public abstract class BaseDaoSql {
}
// 查询数据
Object
ret
=
db
.
queryCell
(
sql
,
paras
);
Object
ret
=
db
.
queryCell
(
this
.
getClass
(),
sqlName
,
sql
,
paras
);
// 写入缓存
this
.
cacheList
.
put
(
cacheKey
,
ret
);
...
...
@@ -688,12 +721,13 @@ public abstract class BaseDaoSql {
* 执行查询SQL语句,并将数据缓存起来
*
* @param cls 返回的类型
* @param sqlName 需要执行的SQL语句名称
* @param sql 需要执行的SQL语句
* @param paras 执行的SQL语句的参数
* @param <T> 返回的泛型
* @return 需要返回的数据
*/
protected
<
T
>
List
<
T
>
queryWithCache
(
Class
<
T
>
cls
,
String
sql
,
Object
...
paras
)
{
protected
<
T
>
List
<
T
>
queryWithCache
(
Class
<
T
>
cls
,
String
sql
Name
,
String
sql
,
Object
...
paras
)
{
// 生成缓存的主键
String
cacheKey
=
getCacheKey
(
cls
,
sql
,
paras
);
...
...
@@ -706,7 +740,7 @@ public abstract class BaseDaoSql {
}
// 查询数据
List
<
T
>
ret
=
db
.
query
(
cls
,
sql
,
paras
);
List
<
T
>
ret
=
db
.
query
(
this
.
getClass
(),
cls
,
sqlName
,
sql
,
paras
);
// 写入缓存
this
.
cacheList
.
put
(
cacheKey
,
ret
);
...
...
yzg-util-db/src/main/java/com/yanzuoguang/db/DbExecute.java
View file @
f995b489
package
com
.
yanzuoguang
.
db
;
import
com.yanzuoguang.db.Impl.DbRow
;
import
com.yanzuoguang.util.vo.MapRow
;
import
java.util.List
;
...
...
@@ -11,36 +12,62 @@ public interface DbExecute {
/**
* 更新SQL语句的执行
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return
*/
int
update
(
String
sql
,
Object
...
paras
);
int
update
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询数据
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param cls 查询的结果的类型
* @param rowHandle 通过该类来处理结果
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @param <T> 返回的集合的类型
* @return 集合
*/
<
T
extends
Object
>
void
query
(
Class
targetClass
,
Class
<
T
>
cls
,
DbRow
<
T
>
rowHandle
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询数据,并返回集合
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param cls 查询的结果的类型
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @param <T> 返回的集合的类型
* @return 集合
*/
<
T
>
List
<
T
>
query
(
Class
<
T
>
cls
,
String
sql
,
Object
...
paras
);
<
T
>
List
<
T
>
query
(
Class
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询数据,并返回集合
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @return 集合
*/
List
<
MapRow
>
query
(
String
sql
,
Object
...
paras
);
List
<
MapRow
>
query
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
);
/**
* 查询第一个单元格的信息
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return 第一个单元格的数据
*/
Object
queryCell
(
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 @
f995b489
...
...
@@ -3,13 +3,18 @@ package com.yanzuoguang.db.Impl;
import
com.yanzuoguang.db.DbExecute
;
import
com.yanzuoguang.extend.ConfigDb
;
import
com.yanzuoguang.util.vo.MapRow
;
import
com.yanzuoguang.util.vo.Ref
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.RowCallbackHandler
;
import
org.springframework.jdbc.support.rowset.SqlRowSet
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
...
...
@@ -31,32 +36,70 @@ public class DbExecuteImpl implements DbExecute {
/**
* 更新SQL语句的执行
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return
*/
public
int
update
(
String
sql
,
Object
...
paras
)
{
public
int
update
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
int
row
=
0
;
long
start
=
new
Date
().
getTime
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
row
=
jdbc
.
update
(
sql
,
paras
);
return
row
;
}
finally
{
printSql
.
print
(
row
,
sql
,
paras
);
printSql
.
print
(
targetClass
,
sqlName
,
row
,
sql
,
paras
,
start
);
}
}
/**
* 查询数据
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param cls 查询的结果的类型
* @param rowHandle 通过该类来处理结果
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @param <T> 返回的集合的类型
* @return 集合
*/
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
=
new
Date
().
getTime
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
jdbc
.
query
(
sql
,
paras
,
new
RowCallbackHandler
()
{
@Override
public
void
processRow
(
ResultSet
rs
)
throws
SQLException
{
AllBeanRowMapper
<
T
>
rowMap
=
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
);
T
data
=
rowMap
.
mapRow
(
rs
,
row
.
value
);
rowHandle
.
handle
(
data
);
row
.
value
++;
}
});
}
finally
{
printSql
.
print
(
targetClass
,
sqlName
,
row
.
value
,
sql
,
paras
,
start
);
}
}
/**
* 查询数据,并返回集合
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param cls 查询的结果的类型
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @param <T> 返回的集合的类型
* @return 集合
*/
public
<
T
extends
Object
>
List
<
T
>
query
(
Class
<
T
>
cls
,
String
sql
,
Object
...
paras
)
{
public
<
T
extends
Object
>
List
<
T
>
query
(
Class
targetClass
,
Class
<
T
>
cls
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
int
row
=
0
;
long
start
=
new
Date
().
getTime
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
List
<
T
>
ret
=
jdbc
.
query
(
sql
,
paras
,
AllBeanRowMapper
.
getInstance
(
cls
,
configDb
));
...
...
@@ -66,30 +109,35 @@ public class DbExecuteImpl implements DbExecute {
row
=
ret
.
size
();
return
ret
;
}
finally
{
printSql
.
print
(
row
,
sql
,
paras
);
printSql
.
print
(
targetClass
,
sqlName
,
row
,
sql
,
paras
,
start
);
}
}
/**
* 查询数据,并返回集合
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql 需要查询的SQL语句
* @param paras 查询语句的参数
* @return 集合
*/
public
List
<
MapRow
>
query
(
String
sql
,
Object
...
paras
)
{
return
query
(
MapRow
.
class
,
sql
,
paras
);
public
List
<
MapRow
>
query
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
return
query
(
targetClass
,
MapRow
.
class
,
sql
,
sqlName
,
paras
);
}
/**
* 查询第一个单元格的信息
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数信息
* @return 第一个单元格的数据
*/
public
Object
queryCell
(
String
sql
,
Object
...
paras
)
{
public
Object
queryCell
(
Class
targetClass
,
String
sqlName
,
String
sql
,
Object
...
paras
)
{
int
row
=
0
;
long
start
=
new
Date
().
getTime
();
try
{
sql
=
this
.
handleParas
(
sql
,
paras
);
SqlRowSet
rowSet
=
jdbc
.
queryForRowSet
(
sql
,
paras
);
...
...
@@ -99,7 +147,7 @@ public class DbExecuteImpl implements DbExecute {
}
return
null
;
}
finally
{
printSql
.
print
(
row
,
sql
,
paras
);
printSql
.
print
(
targetClass
,
sqlName
,
row
,
sql
,
paras
,
start
);
}
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/db/Impl/DbPrintSql.java
View file @
f995b489
...
...
@@ -16,10 +16,12 @@ public class DbPrintSql {
/**
* 打印SQL语句
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数
*/
public
void
print
(
int
row
,
String
sql
,
Object
...
paras
)
{
public
void
print
(
Class
targetClass
,
String
sqlName
,
int
row
,
String
sql
,
Object
...
paras
)
{
// 日志表忽略打印
if
(!
configDb
.
isPrintSql
())
{
return
;
...
...
@@ -43,7 +45,7 @@ public class DbPrintSql {
sql
=
sql
.
replaceFirst
(
"\\?"
,
"'"
+
str
.
replace
(
"'"
,
"''"
)
+
"'"
);
}
}
Log
.
infoTag
(
DbPrintSql
.
class
,
String
.
format
(
"%d row
"
,
row
),
sql
);
Log
.
infoTag
(
DbPrintSql
.
class
,
String
.
format
(
"%d row
%s.%s"
,
row
,
targetClass
.
getSimpleName
(),
sqlName
),
sql
);
}
}
yzg-util-db/src/main/java/com/yanzuoguang/db/Impl/DbRow.java
0 → 100644
View file @
f995b489
package
com
.
yanzuoguang
.
db
.
Impl
;
/**
* 处理行数据
*/
public
interface
DbRow
<
T
>
{
/**
* 需要处理的行
*
* @param row
*/
void
handle
(
T
row
);
}
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