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
1e43c512
Commit
1e43c512
authored
Jan 20, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载视频
parent
803753fa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
40 deletions
+70
-40
DaoConst.java
yzg-util-db/src/main/java/com/yanzuoguang/dao/DaoConst.java
+3
-1
BaseDaoImpl.java
...b/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
+60
-26
BaseDaoSql.java
...db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
+7
-13
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/dao/DaoConst.java
View file @
1e43c512
package
com
.
yanzuoguang
.
dao
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.StringHelper
;
/**
...
...
@@ -104,6 +103,9 @@ public class DaoConst {
*/
public
static
final
String
SQL_LOAD
=
"{SELECT_OUTER_BEGIN}SELECT {FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1"
+
"{WHERE}{GROUP}{HAVING}{ORDER}{LIMIT}{SELECT_OUTER_END}{INNER_OUTER}{WHERE_OUTER}{GROUP_OUTER}{HAVING_OUTER}{ORDER_OUTER}{LIMIT_OUTER}"
;
public
static
final
String
SQL_LIST_FROM
=
"^[\\s\\S]+?VALUES\\s*?\\("
;
public
static
final
String
SQL_LIST_TO
=
",("
;
/**
* 表名代码片段
*/
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
View file @
1e43c512
...
...
@@ -65,6 +65,64 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return
retVal
;
}
protected
List
<
String
>
createReplaceList
(
String
sqlName
,
Collection
collection
)
{
this
.
checkTable
();
int
size
=
collection
.
size
();
// 写入主键,用于排序修改,防止互相锁数据
for
(
Object
item
:
collection
)
{
this
.
table
.
initKeyValue
(
item
);
}
// 获取排序后的数据
List
list
=
this
.
table
.
getKeySort
(
collection
);
boolean
isKeyString
=
this
.
table
.
getTable
().
getKeyType
()
==
String
.
class
;
boolean
isEmpty
=
true
;
// 循环创建
for
(
Object
item
:
list
)
{
// 判断主键是字符串和需要生成主键
String
keyString
=
this
.
table
.
initKeyValue
(
item
);
// 检测数据合法性
this
.
check
(
DaoConst
.
OPERATOR_TYPE_CREATE
,
keyString
,
item
);
// 判断是否为空
if
(!
StringHelper
.
isEmpty
(
keyString
))
{
isEmpty
=
false
;
}
}
// 获取创建语句
SqlData
sqlData
=
this
.
getSql
(
sqlName
);
// 最终sql
StringBuilder
sqlList
=
new
StringBuilder
();
// 最终列表
List
<
Object
>
paraList
=
new
ArrayList
<>();
for
(
Object
item
:
list
)
{
List
<
Object
>
paras
=
new
ArrayList
<>();
String
sql
=
this
.
getPara
(
paras
,
sqlData
,
item
);
if
(
sqlList
.
length
()
>
0
)
{
sql
=
sql
.
replaceAll
(
DaoConst
.
SQL_LIST_FROM
,
DaoConst
.
SQL_LIST_TO
);
}
sqlList
.
append
(
sql
);
paraList
.
addAll
(
paras
);
}
// 最终执行的更新语句
int
ret
=
this
.
getDb
().
update
(
this
.
getClass
(),
sqlData
.
getName
(),
sqlList
.
toString
(),
paraList
.
toArray
());
// 判断是否需要获取自增编号(主键为整形)
if
(
isEmpty
&&
!
isKeyString
)
{
long
identity
=
StringHelper
.
toInt
(
this
.
getIdentity
());
int
pos
=
0
;
// 回写主键
for
(
Object
item
:
list
)
{
pos
++;
String
keyString
=
StringHelper
.
toString
(
identity
-
size
+
pos
);
this
.
table
.
setKeyValue
(
item
,
keyString
);
}
}
// 触发更新事件
for
(
Object
item
:
list
)
{
this
.
onUpdateSql
(
item
);
}
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
}
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
...
...
@@ -164,19 +222,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
@Override
public
List
<
String
>
createList
(
Collection
collection
)
{
this
.
checkTable
();
// 写入主键,用于排序修改,防止互相锁数据
for
(
Object
item
:
collection
)
{
this
.
table
.
initKeyValue
(
item
);
}
// 获取排序后的数据
List
list
=
this
.
table
.
getKeySort
(
collection
);
// 循环创建
for
(
Object
item
:
list
)
{
this
.
create
(
item
);
}
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
return
this
.
createReplaceList
(
DaoConst
.
CREATE
,
collection
);
}
/**
...
...
@@ -228,19 +274,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
@Override
public
List
<
String
>
replaceList
(
Collection
collection
)
{
this
.
checkTable
();
// 写入主键,用于排序修改,防止互相锁数据
for
(
Object
item
:
collection
)
{
this
.
table
.
initKeyValue
(
item
);
}
// 获取排序后的数据
List
list
=
this
.
table
.
getKeySort
(
collection
);
// 循环处理排序后的数据
for
(
Object
item
:
list
)
{
this
.
replace
(
item
);
}
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
return
this
.
createReplaceList
(
DaoConst
.
REPLACE
,
collection
);
}
/**
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
View file @
1e43c512
...
...
@@ -8,7 +8,6 @@ import com.yanzuoguang.db.impl.DbRow;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.ArrayHelper
;
import
com.yanzuoguang.util.helper.StringFormatHandle
;
import
com.yanzuoguang.util.helper.StringHelper
;
...
...
@@ -188,7 +187,7 @@ public abstract class BaseDaoSql {
*/
protected
int
updateSql
(
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
get
Query
Para
(
paras
,
sqlData
,
model
);
String
sql
=
this
.
getPara
(
paras
,
sqlData
,
model
);
int
ret
=
this
.
getDb
().
update
(
this
.
getClass
(),
sqlData
.
getName
(),
sql
,
paras
.
toArray
());
this
.
onUpdateSql
(
model
);
return
ret
;
...
...
@@ -249,7 +248,7 @@ 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
.
get
Query
Para
(
paras
,
sqlData
,
model
);
String
sql
=
this
.
getPara
(
paras
,
sqlData
,
model
);
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sqlData
.
getName
(),
sql
,
paras
.
toArray
());
return
list
;
}
...
...
@@ -264,7 +263,7 @@ public abstract class BaseDaoSql {
*/
protected
<
T
extends
Object
>
void
queryData
(
Class
<
T
>
cls
,
DbRow
<
T
>
handle
,
SqlData
sqlData
,
Object
model
)
{
List
<
Object
>
paras
=
new
ArrayList
<>();
String
sql
=
this
.
get
Query
Para
(
paras
,
sqlData
,
model
);
String
sql
=
this
.
getPara
(
paras
,
sqlData
,
model
);
// 查询数据
this
.
getDb
().
query
(
this
.
getClass
(),
cls
,
handle
,
sqlData
.
getName
(),
sql
,
paras
.
toArray
());
}
...
...
@@ -303,7 +302,7 @@ public abstract class BaseDaoSql {
sqlData
=
getSqlQueryPara
(
sqlData
,
queryPara
,
true
);
List
<
Object
>
paras
=
new
ArrayList
<>();
String
sql
=
this
.
get
Query
Para
(
paras
,
sqlData
,
model
);
String
sql
=
this
.
getPara
(
paras
,
sqlData
,
model
);
Object
cell
=
this
.
queryCellWithCache
(
sqlData
.
getName
(),
sql
,
paras
.
toArray
());
return
cell
;
}
...
...
@@ -525,7 +524,7 @@ public abstract class BaseDaoSql {
// 按照分页查询数据
List
<
Object
>
baseParas
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
get
Query
Para
(
baseParas
,
to
,
model
);
String
sql
=
this
.
getPara
(
baseParas
,
to
,
model
);
// 查询实体数据
List
<
T
>
list
=
this
.
queryWithCache
(
cls
,
sqlName
,
sql
,
baseParas
.
toArray
());
data
.
setList
(
list
);
...
...
@@ -542,7 +541,7 @@ public abstract class BaseDaoSql {
// 按照分页查询数据
List
<
Object
>
baseParas
=
new
ArrayList
<
Object
>();
String
sql
=
this
.
get
Query
Para
(
baseParas
,
fromPageSize
,
model
);
String
sql
=
this
.
getPara
(
baseParas
,
fromPageSize
,
model
);
sql
=
sql
.
trim
();
// 查询总数据量
String
sqlSize
=
"SELECT COUNT(1) FROM ("
+
...
...
@@ -567,8 +566,7 @@ public abstract class BaseDaoSql {
* @param model 参数的实体
* @return SQL条件
*/
protected
String
getQueryPara
(
List
<
Object
>
paras
,
SqlData
sqlData
,
Object
model
)
{
protected
String
getPara
(
List
<
Object
>
paras
,
SqlData
sqlData
,
Object
model
)
{
// 将SQL语句进行代码片段追加
StringBuilder
sb
=
new
StringBuilder
(
sqlData
.
getSql
());
for
(
String
code
:
DaoConst
.
LAST_AUTO_ADD
)
{
...
...
@@ -581,10 +579,6 @@ public abstract class BaseDaoSql {
// 处理字段以及代码片段
String
sql
=
sb
.
toString
();
// 对查询条件进行初始化
if
(
model
instanceof
InitDaoQuery
)
{
((
InitDaoQuery
)
model
).
initCond
();
}
sql
=
handleCodeRelease
(
sql
,
sqlData
,
model
,
codeMap
);
sql
=
handleCodeReplace
(
sqlData
,
sql
,
codeMap
);
...
...
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