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
Expand all
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
This diff is collapsed.
Click to expand it.
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;
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
;
...
...
@@ -87,6 +88,12 @@ public abstract class BaseDaoSql {
return
table
;
}
protected
void
checkTable
(){
if
(
this
.
table
==
null
){
throw
YzgError
.
getRuntimeException
(
"002"
);
}
}
/**
* 初始化当前类对应的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;
import
com.yanzuoguang.dao.DaoConst
;
import
com.yanzuoguang.util.YzgError
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -350,4 +348,147 @@ public class TableSqlCache {
public
void
setNameCache
(
MemoryCache
<
SqlData
>
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;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
org.junit.Test
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
TestDbPrintSql
{
@Test
public
void
test
()
{
...
...
@@ -44,4 +47,24 @@ public class TestDbPrintSql {
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 {
@TableAnnotation
(
value
=
"has1"
,
type
=
DaoConst
.
FIELD_ADD_GROUP
)
private
int
has1
;
public
TestTableGroupVo
()
{
}
public
TestTableGroupVo
(
String
id
)
{
this
.
id
=
id
;
}
public
String
getId
()
{
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