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
74bb1ffa
Commit
74bb1ffa
authored
Jan 01, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公式计算处理
parent
723caa19
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
268 additions
and
132 deletions
+268
-132
BaseDaoImpl.java
...b/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
+86
-128
BaseDaoSql.java
...db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
+7
-0
TableSqlCache.java
...src/main/java/com/yanzuoguang/dao/impl/TableSqlCache.java
+145
-4
TestDbPrintSql.java
yzg-util-db/src/test/java/TestDbPrintSql.java
+23
-0
TestTableGroupVo.java
yzg-util-db/src/test/java/TestTableGroupVo.java
+7
-0
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
View file @
74bb1ffa
...
@@ -44,95 +44,22 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -44,95 +44,22 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return
StringHelper
.
toString
(
this
.
getDb
().
queryCell
(
this
.
getClass
(),
"GET_KEY"
,
"SELECT @@IDENTITY"
));
return
StringHelper
.
toString
(
this
.
getDb
().
queryCell
(
this
.
getClass
(),
"GET_KEY"
,
"SELECT @@IDENTITY"
));
}
}
/**
* 获取主键名称
*
* @return 获取主键名称
*/
protected
String
getKey
()
{
if
(
this
.
table
==
null
)
{
throw
YzgError
.
getRuntimeException
(
"002"
);
}
return
this
.
table
.
getTable
().
getKeyName
();
}
/**
* 获取主键值
*
* @param model 需要获取主键的实体
* @return
*/
protected
String
getKeyString
(
Object
model
)
{
String
keyField
=
this
.
getKey
();
Object
key
=
ObjectHelper
.
get
(
model
,
keyField
);
if
(
StringHelper
.
isEmpty
(
key
))
{
return
""
;
}
String
keyString
=
key
.
toString
();
if
(
DaoConst
.
ZERO
.
equals
(
keyString
))
{
keyString
=
""
;
}
return
keyString
;
}
/**
* 设置主键值
*
* @param model 需要设置的实体
* @param key 需要设置的主键值
*/
protected
void
setKeyString
(
Object
model
,
String
key
)
{
String
keyField
=
this
.
getKey
();
ObjectHelper
.
set
(
model
,
keyField
,
key
);
}
/**
* 根据输入参数来获取主键
*
* @param from 可以为实体或字符串。为实体时必须包含 主键 字段 或者 id 字段。
* @return
*/
protected
String
getInputKey
(
Object
from
)
{
String
key
;
if
(
from
!=
null
&&
from
.
getClass
()
==
String
.
class
)
{
key
=
StringHelper
.
toString
(
from
);
}
else
{
key
=
this
.
getKeyString
(
from
);
}
if
(
StringHelper
.
isEmpty
(
key
))
{
key
=
StringHelper
.
toString
(
ObjectHelper
.
get
(
from
,
"id"
));
}
if
(
StringHelper
.
isEmpty
(
key
))
{
key
=
""
;
}
return
key
;
}
protected
String
createReplace
(
String
sqlName
,
Object
model
)
{
protected
String
createReplace
(
String
sqlName
,
Object
model
)
{
this
.
checkTable
();
// 判断主键是字符串和需要生成主键
// 判断主键是字符串和需要生成主键
boolean
isKeyString
=
this
.
table
.
getTable
().
getKeyType
()
==
String
.
class
;
boolean
isKeyString
=
this
.
table
.
getTable
().
getKeyType
()
==
String
.
class
;
String
keyString
=
this
.
getKeyString
(
model
);
String
keyString
=
this
.
table
.
initKeyValue
(
model
);
// 生成主键
if
(
StringHelper
.
isEmpty
(
keyString
)
&&
isKeyString
)
{
keyString
=
StringHelper
.
getNewID
();
this
.
setKeyString
(
model
,
keyString
);
}
// 检测数据合法性
// 检测数据合法性
this
.
check
(
DaoConst
.
OPERATOR_TYPE_CREATE
,
keyString
,
model
);
this
.
check
(
DaoConst
.
OPERATOR_TYPE_CREATE
,
keyString
,
model
);
// 执行创建的SQL语句
// 执行创建的SQL语句
int
ret
=
updateSql
(
sqlName
,
model
);
int
ret
=
updateSql
(
sqlName
,
model
);
// 判断是否需要获取自增编号(主键为整形)
// 判断是否需要获取自增编号(主键为整形)
if
(
StringHelper
.
isEmpty
(
keyString
)
&&
!
isKeyString
)
{
if
(
StringHelper
.
isEmpty
(
keyString
)
&&
!
isKeyString
)
{
keyString
=
this
.
getIdentity
();
keyString
=
this
.
getIdentity
();
this
.
setKeyString
(
model
,
keyString
);
this
.
table
.
setKeyValue
(
model
,
keyString
);
}
}
// 最终处理
// 最终处理
this
.
created
(
model
);
this
.
created
(
model
);
// 返回执行结果
// 返回执行结果
String
retVal
=
ret
>
0
?
keyString
:
""
;
String
retVal
=
ret
>
0
?
keyString
:
""
;
return
retVal
;
return
retVal
;
...
@@ -169,7 +96,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -169,7 +96,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
*/
@Override
@Override
public
String
update
(
Object
model
)
{
public
String
update
(
Object
model
)
{
String
keyString
=
this
.
getKeyString
(
model
);
this
.
checkTable
();
String
keyString
=
this
.
table
.
getKeyValue
(
model
);
if
(
StringHelper
.
isEmpty
(
keyString
))
{
if
(
StringHelper
.
isEmpty
(
keyString
))
{
throw
YzgError
.
getRuntimeException
(
"029"
,
this
.
table
.
getTable
().
getName
());
throw
YzgError
.
getRuntimeException
(
"029"
,
this
.
table
.
getTable
().
getName
());
}
}
...
@@ -191,7 +119,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -191,7 +119,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
*/
@Override
@Override
public
String
save
(
Object
model
)
{
public
String
save
(
Object
model
)
{
String
id
=
this
.
getKeyString
(
model
);
this
.
checkTable
();
String
id
=
this
.
table
.
getKeyValue
(
model
);
if
(
StringHelper
.
isEmpty
(
id
))
{
if
(
StringHelper
.
isEmpty
(
id
))
{
return
create
(
model
);
return
create
(
model
);
}
else
{
}
else
{
...
@@ -207,17 +136,14 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -207,17 +136,14 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
*/
@Override
@Override
public
int
remove
(
Object
model
)
{
public
int
remove
(
Object
model
)
{
this
.
checkTable
();
// 获取来源主键
// 获取来源主键
Object
from
=
model
;
Object
from
=
model
;
String
keyString
=
getInputKey
(
from
);
String
keyString
=
this
.
table
.
getInputKey
(
from
);
// 调用删除日志
// 调用删除日志
this
.
check
(
DaoConst
.
OPERATOR_TYPE_REMOVE
,
keyString
,
from
);
this
.
check
(
DaoConst
.
OPERATOR_TYPE_REMOVE
,
keyString
,
from
);
// 当主键存在值时,直接通过主键删除
// 当主键存在值时,直接通过主键删除
if
(!
StringHelper
.
isEmpty
(
keyString
))
{
from
=
this
.
table
.
getKeyObject
(
from
,
keyString
);
// 去掉其他非主键的属性
from
=
new
HashMap
<
String
,
Object
>(
DaoConst
.
COLLECTION_INIT_SIZE
);
this
.
setKeyString
(
from
,
keyString
);
}
// 处理来源值
// 处理来源值
for
(
TableFieldVo
fieldVo
:
this
.
table
.
getTable
().
getRemoveUpdate
())
{
for
(
TableFieldVo
fieldVo
:
this
.
table
.
getTable
().
getRemoveUpdate
())
{
...
@@ -233,16 +159,24 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -233,16 +159,24 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
/**
/**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
*
*
* @param
model
需要创建的数据
* @param
collection
需要创建的数据
* @return 创建的主键编号
* @return 创建的主键编号
*/
*/
@Override
@Override
public
List
<
String
>
createList
(
Collection
model
)
{
public
List
<
String
>
createList
(
Collection
collection
)
{
List
<
String
>
ret
=
new
ArrayList
<>();
this
.
checkTable
();
for
(
Object
item
:
model
)
{
// 写入主键,用于排序修改,防止互相锁数据
ret
.
add
(
this
.
create
(
item
));
for
(
Object
item
:
collection
)
{
this
.
table
.
initKeyValue
(
item
);
}
}
return
ret
;
// 获取排序后的数据
List
list
=
this
.
table
.
getKeySort
(
collection
);
// 循环创建
for
(
Object
item
:
list
)
{
this
.
create
(
item
);
}
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
}
}
/**
/**
...
@@ -259,16 +193,19 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -259,16 +193,19 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
/**
/**
* 修改数据
* 修改数据
*
*
* @param
model
需要修改的数据
* @param
collection
需要修改的数据
* @return 删除的主键编号
* @return 删除的主键编号
*/
*/
@Override
@Override
public
List
<
String
>
updateList
(
Collection
model
)
{
public
List
<
String
>
updateList
(
Collection
collection
)
{
List
<
String
>
ret
=
new
ArrayList
<>();
this
.
checkTable
();
for
(
Object
item
:
model
)
{
// 获取排序后的数据
ret
.
add
(
this
.
update
(
item
));
List
list
=
this
.
table
.
getKeySort
(
collection
);
for
(
Object
item
:
list
)
{
this
.
update
(
item
);
}
}
return
ret
;
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
}
}
...
@@ -286,17 +223,24 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -286,17 +223,24 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
/**
/**
* 修改数据
* 修改数据
*
*
* @param
model
需要修改的数据
* @param
collection
需要修改的数据
* @return 删除的主键编号
* @return 删除的主键编号
*/
*/
@Override
@Override
public
List
<
String
>
replaceList
(
Collection
model
)
{
public
List
<
String
>
replaceList
(
Collection
collection
)
{
List
<
String
>
ret
=
new
ArrayList
<>();
this
.
checkTable
();
for
(
Object
item
:
model
)
{
// 写入主键,用于排序修改,防止互相锁数据
ret
.
add
(
this
.
replace
(
item
));
for
(
Object
item
:
collection
)
{
this
.
table
.
initKeyValue
(
item
);
}
}
return
ret
;
// 获取排序后的数据
List
list
=
this
.
table
.
getKeySort
(
collection
);
// 循环处理排序后的数据
for
(
Object
item
:
list
)
{
this
.
replace
(
item
);
}
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
}
}
/**
/**
...
@@ -313,16 +257,30 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -313,16 +257,30 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
/**
/**
* 保存数据,有主键时修改,无主键时创建
* 保存数据,有主键时修改,无主键时创建
*
*
* @param
model
需要保存的数据
* @param
collection
需要保存的数据
* @return 保存的主键编号
* @return 保存的主键编号
*/
*/
@Override
@Override
public
List
<
String
>
saveList
(
Collection
model
)
{
public
List
<
String
>
saveList
(
Collection
collection
)
{
List
<
String
>
ret
=
new
ArrayList
<>();
// 创建列表
for
(
Object
item
:
model
)
{
List
<
Object
>
creates
=
new
ArrayList
<>();
ret
.
add
(
this
.
save
(
item
));
// 修改列表
List
<
Object
>
updates
=
new
ArrayList
<>();
// 将保存的集合数据划分为创建列表和修改列表
for
(
Object
item
:
collection
)
{
String
keyValue
=
this
.
table
.
getKeyValue
(
item
);
if
(
StringHelper
.
isEmpty
(
keyValue
))
{
creates
.
add
(
item
);
}
else
{
updates
.
add
(
item
);
}
}
}
return
ret
;
// 首先执行更新,防止和创建时发生冲突
this
.
updateList
(
updates
);
// 然后执行创建SQL语句
this
.
createList
(
creates
);
// 最后获取主键集合
return
this
.
table
.
getCollectionRet
(
collection
);
}
}
/**
/**
...
@@ -341,13 +299,17 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -341,13 +299,17 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* 删除数据,可以用于父子表删除,如通过订单删除游客信息 visitorDao.remove({orderId:1});
* 删除数据,可以用于父子表删除,如通过订单删除游客信息 visitorDao.remove({orderId:1});
* int field;
* int field;
*
*
* @param
model
需要删除的数据,可以是主键字符串(Int),或者是包含主键的实体,或者是包含其他非主键的实体完全匹配.
* @param
collection
需要删除的数据,可以是主键字符串(Int),或者是包含主键的实体,或者是包含其他非主键的实体完全匹配.
* @return 删除的记录数量
* @return 删除的记录数量
*/
*/
@Override
@Override
public
int
removeList
(
Collection
model
)
{
public
int
removeList
(
Collection
collection
)
{
this
.
checkTable
();
// 获取删除的列表,会按照主键顺序进行删除,没有主键时,不进行排序
List
list
=
this
.
table
.
getKeySort
(
collection
);
// 返回影响的行数
int
ret
=
0
;
int
ret
=
0
;
for
(
Object
item
:
model
)
{
for
(
Object
item
:
list
)
{
ret
+=
this
.
remove
(
item
);
ret
+=
this
.
remove
(
item
);
}
}
return
ret
;
return
ret
;
...
@@ -399,37 +361,29 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -399,37 +361,29 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return
* @return
*/
*/
protected
Object
getLoadFrom
(
Object
model
,
QueryPara
queryPara
)
{
protected
Object
getLoadFrom
(
Object
model
,
QueryPara
queryPara
)
{
this
.
checkTable
();
if
(
queryPara
!=
null
&&
queryPara
.
isFullCond
())
{
if
(
queryPara
!=
null
&&
queryPara
.
isFullCond
())
{
return
model
;
return
model
;
}
}
// 对查询条件进行初始化
// 对查询条件进行初始化
if
(
model
instanceof
InitDaoQuery
)
{
if
(
model
instanceof
InitDaoQuery
)
{
((
InitDaoQuery
)
model
).
initCond
();
((
InitDaoQuery
)
model
).
initCond
();
}
}
// 获取来源主键
// 获取来源主键
Object
from
=
model
;
Object
from
=
this
.
table
.
getKeyObject
(
model
);
String
key
=
this
.
getInputKey
(
from
);
// 当主键存在时,只通过主键加载
if
(!
StringHelper
.
isEmpty
(
key
))
{
from
=
new
HashMap
<
String
,
Object
>(
DaoConst
.
COLLECTION_INIT_SIZE
);
this
.
setKeyString
(
from
,
key
);
}
return
from
;
return
from
;
}
}
private
<
T
extends
Object
>
void
checkLoadResult
(
T
toItem
,
List
<
T
>
tos
)
{
private
<
T
extends
Object
>
void
checkLoadResult
(
T
toItem
,
List
<
T
>
tos
)
{
this
.
checkTable
();
// 判断来源主键是否存在,不存在则获取加载后的主键
// 判断来源主键是否存在,不存在则获取加载后的主键
if
(
toItem
!=
null
)
{
if
(
toItem
!=
null
)
{
String
key
=
this
.
getKeyString
(
toItem
);
String
key
=
this
.
table
.
getKeyValue
(
toItem
);
check
(
DaoConst
.
OPERATOR_TYPE_LOAD
,
key
,
toItem
);
check
(
DaoConst
.
OPERATOR_TYPE_LOAD
,
key
,
toItem
);
}
}
if
(
tos
!=
null
)
{
if
(
tos
!=
null
)
{
for
(
T
item
:
tos
)
{
for
(
T
item
:
tos
)
{
String
key
=
this
.
getKeyString
(
item
);
String
key
=
this
.
table
.
getKeyValue
(
item
);
check
(
DaoConst
.
OPERATOR_TYPE_LOAD
,
key
,
item
);
check
(
DaoConst
.
OPERATOR_TYPE_LOAD
,
key
,
item
);
}
}
}
}
...
@@ -656,6 +610,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -656,6 +610,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
*/
@Override
@Override
public
<
T
extends
Object
>
String
setGroupId
(
T
model
)
{
public
<
T
extends
Object
>
String
setGroupId
(
T
model
)
{
this
.
checkTable
();
// 判断前台实体
// 判断前台实体
if
(
model
==
null
)
{
if
(
model
==
null
)
{
return
StringHelper
.
EMPTY
;
return
StringHelper
.
EMPTY
;
...
@@ -672,7 +627,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -672,7 +627,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
ObjectHelper
.
set
(
model
,
md5Field
,
md5
);
ObjectHelper
.
set
(
model
,
md5Field
,
md5
);
}
}
if
(
this
.
table
.
getTable
().
getKeyType
()
==
String
.
class
)
{
if
(
this
.
table
.
getTable
().
getKeyType
()
==
String
.
class
)
{
this
.
setKeyString
(
model
,
md5
);
this
.
table
.
setKeyValue
(
model
,
md5
);
}
}
return
md5
;
return
md5
;
}
}
...
@@ -711,7 +666,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -711,7 +666,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return
this
.
create
(
model
);
return
this
.
create
(
model
);
}
else
{
}
else
{
// 获取后台的编号,并写入到前台传入的数据上面
// 获取后台的编号,并写入到前台传入的数据上面
String
keyField
=
this
.
getKey
();
String
keyField
=
this
.
table
.
getKeyField
();
Object
key
=
ObjectHelper
.
get
(
from
,
keyField
);
Object
key
=
ObjectHelper
.
get
(
from
,
keyField
);
ObjectHelper
.
set
(
model
,
keyField
,
key
);
ObjectHelper
.
set
(
model
,
keyField
,
key
);
if
(
model
instanceof
InitDao
)
{
if
(
model
instanceof
InitDao
)
{
...
@@ -843,7 +798,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -843,7 +798,8 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/
*/
@Override
@Override
public
<
T
extends
Object
>
String
saveByLoad
(
Class
<
T
>
cls
,
Object
request
)
{
public
<
T
extends
Object
>
String
saveByLoad
(
Class
<
T
>
cls
,
Object
request
)
{
String
key
=
this
.
getInputKey
(
request
);
this
.
checkTable
();
String
key
=
this
.
table
.
getInputKey
(
request
);
T
from
=
null
;
T
from
=
null
;
if
(!
StringHelper
.
isEmpty
(
key
))
{
if
(!
StringHelper
.
isEmpty
(
key
))
{
from
=
this
.
load
(
request
,
cls
);
from
=
this
.
load
(
request
,
cls
);
...
@@ -862,6 +818,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -862,6 +818,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return 创建成功的编号
* @return 创建成功的编号
*/
*/
protected
<
T
extends
Object
>
String
saveFromCreate
(
Class
<
T
>
cls
,
T
from
,
Object
request
)
{
protected
<
T
extends
Object
>
String
saveFromCreate
(
Class
<
T
>
cls
,
T
from
,
Object
request
)
{
this
.
checkTable
();
if
(
from
==
null
)
{
if
(
from
==
null
)
{
try
{
try
{
from
=
cls
.
newInstance
();
from
=
cls
.
newInstance
();
...
@@ -893,6 +850,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
...
@@ -893,6 +850,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
* @return 保存成功,返回保存的ID,保存失败,返回空值
* @return 保存成功,返回保存的ID,保存失败,返回空值
*/
*/
public
<
T
extends
Object
>
String
saveWith
(
Class
<
T
>
cls
,
String
sqlName
,
Object
request
)
{
public
<
T
extends
Object
>
String
saveWith
(
Class
<
T
>
cls
,
String
sqlName
,
Object
request
)
{
this
.
checkTable
();
if
(
request
instanceof
InitDao
)
{
if
(
request
instanceof
InitDao
)
{
((
InitDao
)
request
).
init
();
((
InitDao
)
request
).
init
();
}
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
View file @
74bb1ffa
...
@@ -8,6 +8,7 @@ import com.yanzuoguang.db.impl.DbRow;
...
@@ -8,6 +8,7 @@ import com.yanzuoguang.db.impl.DbRow;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.ArrayHelper
;
import
com.yanzuoguang.util.helper.ArrayHelper
;
import
com.yanzuoguang.util.helper.StringFormatHandle
;
import
com.yanzuoguang.util.helper.StringFormatHandle
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
...
@@ -87,6 +88,12 @@ public abstract class BaseDaoSql {
...
@@ -87,6 +88,12 @@ public abstract class BaseDaoSql {
return
table
;
return
table
;
}
}
protected
void
checkTable
(){
if
(
this
.
table
==
null
){
throw
YzgError
.
getRuntimeException
(
"002"
);
}
}
/**
/**
* 初始化当前类对应的SQL语句对象
* 初始化当前类对应的SQL语句对象
*/
*/
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/TableSqlCache.java
View file @
74bb1ffa
...
@@ -2,13 +2,11 @@ package com.yanzuoguang.dao.impl;
...
@@ -2,13 +2,11 @@ package com.yanzuoguang.dao.impl;
import
com.yanzuoguang.dao.DaoConst
;
import
com.yanzuoguang.dao.DaoConst
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
...
@@ -350,4 +348,147 @@ public class TableSqlCache {
...
@@ -350,4 +348,147 @@ public class TableSqlCache {
public
void
setNameCache
(
MemoryCache
<
SqlData
>
nameCache
)
{
public
void
setNameCache
(
MemoryCache
<
SqlData
>
nameCache
)
{
this
.
nameCache
=
nameCache
;
this
.
nameCache
=
nameCache
;
}
}
/**
* 获取主键名称
*
* @return 获取主键名称
*/
public
String
getKeyField
()
{
if
(
this
.
table
==
null
)
{
throw
YzgError
.
getRuntimeException
(
"002"
);
}
return
this
.
table
.
getKeyName
();
}
/**
* 设置主键值
*
* @param model 需要设置的实体
* @param key 需要设置的主键值
*/
public
void
setKeyValue
(
Object
model
,
String
key
)
{
String
keyField
=
this
.
getKeyField
();
ObjectHelper
.
set
(
model
,
keyField
,
key
);
}
/**
* 获取主键值
*
* @param model 需要获取主键的实体
* @return
*/
public
String
getKeyValue
(
Object
model
)
{
String
keyField
=
this
.
getKeyField
();
Object
key
=
ObjectHelper
.
get
(
model
,
keyField
);
if
(
StringHelper
.
isEmpty
(
key
))
{
return
""
;
}
String
keyString
=
key
.
toString
();
if
(
DaoConst
.
ZERO
.
equals
(
keyString
))
{
keyString
=
""
;
}
return
keyString
;
}
/**
* 初始化key
*
* @param model
* @return
*/
public
String
initKeyValue
(
Object
model
)
{
// 判断主键是字符串和需要生成主键
boolean
isKeyString
=
this
.
table
.
getKeyType
()
==
String
.
class
;
String
keyString
=
this
.
getKeyValue
(
model
);
// 生成主键
if
(
StringHelper
.
isEmpty
(
keyString
)
&&
isKeyString
)
{
keyString
=
StringHelper
.
getNewID
();
this
.
setKeyValue
(
model
,
keyString
);
}
return
keyString
;
}
/**
* 根据输入参数来获取主键
*
* @param from 可以为实体或字符串。为实体时必须包含 主键 字段 或者 id 字段。
* @return
*/
public
String
getInputKey
(
Object
from
)
{
String
key
;
if
(
from
!=
null
&&
from
.
getClass
()
==
String
.
class
)
{
key
=
StringHelper
.
toString
(
from
);
}
else
{
key
=
this
.
getKeyValue
(
from
);
}
if
(
StringHelper
.
isEmpty
(
key
))
{
key
=
StringHelper
.
toString
(
ObjectHelper
.
get
(
from
,
"id"
));
}
if
(
StringHelper
.
isEmpty
(
key
))
{
key
=
""
;
}
return
key
;
}
/**
* 获取请求对象的主键对象
*
* @param from
* @return
*/
public
Object
getKeyObject
(
Object
from
)
{
String
keyValue
=
this
.
getInputKey
(
from
);
return
getKeyObject
(
from
,
keyValue
);
}
/**
* 根据输入参数来获取主键
*
* @param keyValue 根据主键获取对象
* @return
*/
public
Object
getKeyObject
(
Object
from
,
String
keyValue
)
{
if
(!
StringHelper
.
isEmpty
(
keyValue
))
{
// 去掉其他非主键的属性
from
=
new
HashMap
<
String
,
Object
>(
DaoConst
.
COLLECTION_INIT_SIZE
);
this
.
setKeyValue
(
from
,
keyValue
);
}
return
from
;
}
/**
* 按照主键对集合进行排序
*
* @param collection
* @return
*/
public
List
getKeySort
(
Collection
collection
)
{
// 集合数据
List
list
=
new
ArrayList
();
list
.
addAll
(
collection
);
// 对集合进行排序
Collections
.
sort
(
list
,
new
Comparator
<
Object
>()
{
@Override
public
int
compare
(
Object
o1
,
Object
o2
)
{
String
key1
=
StringHelper
.
getFirst
(
TableSqlCache
.
this
.
getKeyValue
(
o1
));
String
key2
=
StringHelper
.
getFirst
(
TableSqlCache
.
this
.
getKeyValue
(
o2
));
return
key1
.
compareTo
(
key2
);
}
});
return
list
;
}
/**
* 获取返回值数组
* @param collection
* @return
*/
public
List
<
String
>
getCollectionRet
(
Collection
collection
)
{
List
<
String
>
rets
=
new
ArrayList
<>();
for
(
Object
item
:
collection
)
{
rets
.
add
(
this
.
getKeyValue
(
item
));
}
return
rets
;
}
}
}
yzg-util-db/src/test/java/TestDbPrintSql.java
View file @
74bb1ffa
...
@@ -6,6 +6,9 @@ import com.yanzuoguang.db.impl.DbPrintSql;
...
@@ -6,6 +6,9 @@ import com.yanzuoguang.db.impl.DbPrintSql;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
TestDbPrintSql
{
public
class
TestDbPrintSql
{
@Test
@Test
public
void
test
()
{
public
void
test
()
{
...
@@ -44,4 +47,24 @@ public class TestDbPrintSql {
...
@@ -44,4 +47,24 @@ public class TestDbPrintSql {
System
.
out
.
println
(
JsonHelper
.
serialize
(
cache
.
getNameCache
(),
true
));
System
.
out
.
println
(
JsonHelper
.
serialize
(
cache
.
getNameCache
(),
true
));
}
}
@Test
public
void
testTableStructSort
()
{
TableSqlCache
cache
=
new
TableSqlCache
();
TableStruct
table
=
new
TableStruct
(
"test"
,
TestTableGroupVo
.
class
);
table
.
init
(
cache
);
List
<
TestTableGroupVo
>
list
=
new
ArrayList
<>();
list
.
add
(
new
TestTableGroupVo
(
"2"
));
list
.
add
(
new
TestTableGroupVo
(
"1"
));
list
.
add
(
new
TestTableGroupVo
(
"3"
));
List
listTo
=
cache
.
getKeySort
(
list
);
String
json
=
JsonHelper
.
serialize
(
listTo
,
true
);
System
.
out
.
println
(
json
);
}
}
}
yzg-util-db/src/test/java/TestTableGroupVo.java
View file @
74bb1ffa
...
@@ -23,6 +23,13 @@ public class TestTableGroupVo {
...
@@ -23,6 +23,13 @@ public class TestTableGroupVo {
@TableAnnotation
(
value
=
"has1"
,
type
=
DaoConst
.
FIELD_ADD_GROUP
)
@TableAnnotation
(
value
=
"has1"
,
type
=
DaoConst
.
FIELD_ADD_GROUP
)
private
int
has1
;
private
int
has1
;
public
TestTableGroupVo
()
{
}
public
TestTableGroupVo
(
String
id
)
{
this
.
id
=
id
;
}
public
String
getId
()
{
public
String
getId
()
{
return
id
;
return
id
;
}
}
...
...
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