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
00f7aa00
Commit
00f7aa00
authored
Nov 04, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ver1.2' of
http://192.168.0.204/yzg/yzg-util
into ver1.3
parents
cb8585ba
d1f15a2a
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
212 additions
and
23 deletions
+212
-23
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+2
-2
ArrayHelper.java
...rc/main/java/com/yanzuoguang/util/helper/ArrayHelper.java
+32
-6
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+2
-2
TestJsonHelper.java
yzg-util-base/src/test/java/helper/TestJsonHelper.java
+29
-1
MapRowCache.java
yzg-util-base/src/test/java/helper/vo/MapRowCache.java
+107
-0
MapSub.java
yzg-util-base/src/test/java/helper/vo/MapSub.java
+31
-0
SqlCondEquals.java
...src/main/java/com/yanzuoguang/dao/cond/SqlCondEquals.java
+2
-5
SqlCondItem.java
...b/src/main/java/com/yanzuoguang/dao/cond/SqlCondItem.java
+2
-1
BaseDaoSql.java
...db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
+5
-6
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
00f7aa00
...
@@ -281,7 +281,7 @@ public class ObjectHelper {
...
@@ -281,7 +281,7 @@ public class ObjectHelper {
* @param fromName 来源名称
* @param fromName 来源名称
* @return 名称结果
* @return 名称结果
*/
*/
p
rivate
static
String
get
SimpleName
(
String
fromName
)
{
p
ublic
static
String
getMethod
SimpleName
(
String
fromName
)
{
String
toName
=
getSimpleFieldName
(
fromName
);
String
toName
=
getSimpleFieldName
(
fromName
);
if
(
toName
.
startsWith
(
METHOD_IS
))
{
if
(
toName
.
startsWith
(
METHOD_IS
))
{
toName
=
toName
.
substring
(
METHOD_IS
.
length
());
toName
=
toName
.
substring
(
METHOD_IS
.
length
());
...
@@ -361,7 +361,7 @@ public class ObjectHelper {
...
@@ -361,7 +361,7 @@ public class ObjectHelper {
}
}
String
methodNameSource
=
method
.
getName
();
String
methodNameSource
=
method
.
getName
();
String
methodNameSimple
=
methodNameSource
.
toLowerCase
();
String
methodNameSimple
=
methodNameSource
.
toLowerCase
();
String
toName
=
getSimpleName
(
methodNameSource
);
String
toName
=
get
Method
SimpleName
(
methodNameSource
);
if
(
"getClass"
.
equals
(
methodNameSource
))
{
if
(
"getClass"
.
equals
(
methodNameSource
))
{
continue
;
continue
;
}
}
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/ArrayHelper.java
View file @
00f7aa00
package
com
.
yanzuoguang
.
util
.
helper
;
package
com
.
yanzuoguang
.
util
.
helper
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.List
;
/**
/**
* 字符串帮主类
* 字符串帮主类
...
@@ -76,8 +73,16 @@ public class ArrayHelper {
...
@@ -76,8 +73,16 @@ public class ArrayHelper {
* @return
* @return
*/
*/
public
static
boolean
isArrayOrList
(
Object
val
)
{
public
static
boolean
isArrayOrList
(
Object
val
)
{
boolean
isArray
=
val
!=
null
&&
(
val
instanceof
List
||
val
.
getClass
().
isArray
());
return
val
!=
null
&&
(
val
instanceof
List
||
val
.
getClass
().
isArray
());
return
isArray
;
}
/**
* 判断对象是否是数组或者List
*
* @param val
* @return
*/
public
static
boolean
isArrayOrCollection
(
Object
val
)
{
return
val
!=
null
&&
(
val
instanceof
List
||
val
.
getClass
().
isArray
());
}
}
...
@@ -122,6 +127,27 @@ public class ArrayHelper {
...
@@ -122,6 +127,27 @@ public class ArrayHelper {
}
}
return
list
;
return
list
;
}
}
/**
* 获取对象为对象列表
*
* @param val 需要获取的对象,可以为List或者Array或者其他对象.
* @return
*/
public
static
Collection
getCollection
(
Object
val
)
{
boolean
isArray
=
isArrayOrCollection
(
val
);
Collection
list
;
// 判断处理
if
(
val
instanceof
Collection
)
{
list
=
(
Collection
)
val
;
}
else
if
(
isArray
)
{
Object
[]
arr
=
(
Object
[])
val
;
list
=
Arrays
.
asList
(
arr
);
}
else
{
list
=
new
ArrayList
();
list
.
add
(
val
);
}
return
list
;
}
/**
/**
* 将数组添加到列表中
* 将数组添加到列表中
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
00f7aa00
...
@@ -76,12 +76,12 @@ public class StringHelper {
...
@@ -76,12 +76,12 @@ public class StringHelper {
return
true
;
return
true
;
}
}
// 判断是否属于数组
// 判断是否属于数组
boolean
isArray
=
ArrayHelper
.
isArrayOr
List
(
val
);
boolean
isArray
=
ArrayHelper
.
isArrayOr
Collection
(
val
);
// 不是数组,则为常规值
// 不是数组,则为常规值
if
(!
isArray
)
{
if
(!
isArray
)
{
return
false
;
return
false
;
}
}
List
list
=
ArrayHelper
.
getList
(
val
);
Collection
list
=
ArrayHelper
.
getCollection
(
val
);
return
list
.
isEmpty
();
return
list
.
isEmpty
();
}
}
...
...
yzg-util-base/src/test/java/helper/TestJsonHelper.java
View file @
00f7aa00
...
@@ -2,6 +2,7 @@ package helper;
...
@@ -2,6 +2,7 @@ package helper;
import
com.alibaba.fastjson.TypeReference
;
import
com.alibaba.fastjson.TypeReference
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
helper.vo.MapSub
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -42,6 +43,32 @@ public class TestJsonHelper {
...
@@ -42,6 +43,32 @@ public class TestJsonHelper {
Assert
.
assertNotNull
(
to
.
getList
());
Assert
.
assertNotNull
(
to
.
getList
());
Assert
.
assertNull
(
to
.
getListNull
());
Assert
.
assertNull
(
to
.
getListNull
());
}
}
@Test
public
void
testMapRow
()
{
String
key
=
"age"
;
String
name
=
"颜佐光"
;
MapSub
from
=
new
MapSub
();
MapSub
from1
=
new
MapSub
();
from
.
setName
(
name
);
from
.
setName
(
name
);
from1
.
setName
(
name
);
from1
.
setName
(
name
);
from
.
setYear
(
15
);
from1
.
setYear
(
15
);
from
.
put
(
"age"
,
15
);
String
json
=
JsonHelper
.
serialize
(
from
);
MapSub
to
=
JsonHelper
.
deserialize
(
json
,
new
TypeReference
<
MapSub
>()
{
});
Assert
.
assertEquals
(
from
.
getName
(),
to
.
getName
());
Assert
.
assertEquals
(
from
.
getYear
(),
to
.
getYear
());
Assert
.
assertEquals
(
from
.
get
(
key
),
to
.
get
(
key
));
Assert
.
assertEquals
(
from
.
getName
(),
name
);
Assert
.
assertEquals
(
from
,
to
);
}
}
}
...
@@ -130,3 +157,4 @@ class FanxingData {
...
@@ -130,3 +157,4 @@ class FanxingData {
this
.
name
=
name
;
this
.
name
=
name
;
}
}
}
}
yzg-util-base/src/test/java/helper/vo/MapRowCache.java
0 → 100644
View file @
00f7aa00
package
helper
.
vo
;
import
com.alibaba.fastjson.TypeReference
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.vo.MapRow
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
MapRowCache
extends
MapRow
{
private
final
static
Map
<
String
,
TypeReference
<?>>
mapMethodReference
=
new
ConcurrentHashMap
<>();
private
final
static
Map
<
Class
<?>,
DataInfo
>
mapClassDataInfo
=
new
ConcurrentHashMap
<>();
public
MapRowCache
()
{
}
@Override
public
Object
get
(
Object
key
)
{
return
super
.
get
(
key
);
}
@Override
public
Object
put
(
String
key
,
Object
value
)
{
return
super
.
put
(
key
,
value
);
}
protected
<
T
>
T
getField
(
Get
<
T
>
getter
)
{
DataInfo
dataInfo
=
getDataInfo
(
getter
.
getClass
(),
3
);
System
.
out
.
println
(
dataInfo
.
getName
()
+
"()"
+
" called by "
+
dataInfo
.
getCls
().
getName
());
return
null
;
}
protected
<
T
>
void
putField
(
Set
<
T
>
setter
,
T
value
)
{
DataInfo
dataInfo
=
getDataInfo
(
setter
.
getClass
(),
3
);
System
.
out
.
println
(
dataInfo
.
getName
()
+
"()"
+
" called by "
+
dataInfo
.
getCls
().
getName
());
}
private
DataInfo
getDataInfo
(
Class
<?>
cls
,
int
pos
)
{
if
(!
mapClassDataInfo
.
containsKey
(
cls
))
{
StackTraceElement
[]
stackTraceElements
=
Thread
.
currentThread
().
getStackTrace
();
String
methodName
=
stackTraceElements
[
pos
].
getMethodName
();
String
fieldName
=
getMethodFieldName
(
methodName
);
String
fieldClsName
=
getMethodFieldClsName
(
this
.
getClass
(),
fieldName
);
if
(!
mapMethodReference
.
containsKey
(
fieldClsName
))
{
throw
new
IllegalArgumentException
(
"未注册类型:"
+
cls
.
getName
()
+
" 方法关联:"
+
methodName
);
}
DataInfo
dataInfo
=
new
DataInfo
(
methodName
,
cls
,
mapMethodReference
.
get
(
cls
));
mapClassDataInfo
.
put
(
cls
,
dataInfo
);
return
dataInfo
;
}
return
mapClassDataInfo
.
get
(
cls
);
}
private
static
String
getMethodFieldClsName
(
Class
<?>
cls
,
String
fieldName
)
{
return
cls
.
getName
()
+
"."
+
fieldName
;
}
protected
static
void
register
(
Class
<?>
cls
,
String
fieldName
,
TypeReference
<?>
typeReference
)
{
String
methodFieldClsName
=
getMethodFieldClsName
(
cls
,
fieldName
);
mapMethodReference
.
put
(
methodFieldClsName
,
typeReference
);
}
protected
interface
Get
<
T
>
{
T
get
();
}
protected
interface
Set
<
T
>
{
void
set
(
T
t
);
}
/**
* 字段关联的数据信息
*/
protected
static
class
DataInfo
{
private
final
String
name
;
private
final
Class
<?>
cls
;
private
final
TypeReference
<?>
typeReference
;
public
DataInfo
(
String
name
,
Class
<?>
cls
,
TypeReference
<?>
typeReference
)
{
this
.
name
=
name
;
this
.
cls
=
cls
;
this
.
typeReference
=
typeReference
;
}
public
String
getName
()
{
return
name
;
}
public
Class
<?>
getCls
()
{
return
cls
;
}
public
TypeReference
<?>
getTypeReference
()
{
return
typeReference
;
}
}
/**
* 获取方法字段名
*
* @param fullName 方法全名
* @return 方法字段名
*/
protected
String
getMethodFieldName
(
String
fullName
)
{
return
ObjectHelper
.
getMethodSimpleName
(
fullName
);
}
}
yzg-util-base/src/test/java/helper/vo/MapSub.java
0 → 100644
View file @
00f7aa00
package
helper
.
vo
;
import
com.alibaba.fastjson.TypeReference
;
public
class
MapSub
extends
MapRowCache
{
static
{
TypeReference
<
String
>
typeStringReference
=
new
TypeReference
<
String
>()
{
};
TypeReference
<
Integer
>
typeIntegerReference
=
new
TypeReference
<
Integer
>()
{
};
register
(
MapSub
.
class
,
"name"
,
typeStringReference
);
register
(
MapSub
.
class
,
"year"
,
typeIntegerReference
);
}
public
String
getName
()
{
return
this
.
getField
(
this
::
getName
);
}
public
void
setName
(
String
name
)
{
this
.
putField
(
this
::
setName
,
name
);
}
public
int
getYear
()
{
return
this
.
getField
(
this
::
getYear
);
}
public
void
setYear
(
int
age
)
{
this
.
putField
(
this
::
setYear
,
age
);
}
}
yzg-util-db/src/main/java/com/yanzuoguang/dao/cond/SqlCondEquals.java
View file @
00f7aa00
...
@@ -6,10 +6,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
...
@@ -6,10 +6,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
import
com.yanzuoguang.util.helper.ArrayHelper
;
import
com.yanzuoguang.util.helper.ArrayHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.Arrays
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
/**
* 包含的值字段
* 包含的值字段
...
@@ -90,7 +87,7 @@ public class SqlCondEquals extends SqlCondBase<SqlCondEquals> {
...
@@ -90,7 +87,7 @@ public class SqlCondEquals extends SqlCondBase<SqlCondEquals> {
val
=
StringHelper
.
toInt
(
val
);
val
=
StringHelper
.
toInt
(
val
);
}
}
// 判断值是否相等
// 判断值是否相等
List
list
=
ArrayHelper
.
getList
(
val
);
Collection
list
=
ArrayHelper
.
getCollection
(
val
);
for
(
Object
item
:
list
)
{
for
(
Object
item
:
list
)
{
// 值处理
// 值处理
Object
itemTo
=
item
;
Object
itemTo
=
item
;
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/cond/SqlCondItem.java
View file @
00f7aa00
...
@@ -9,6 +9,7 @@ import com.yanzuoguang.util.helper.ArrayHelper;
...
@@ -9,6 +9,7 @@ import com.yanzuoguang.util.helper.ArrayHelper;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -79,7 +80,7 @@ public class SqlCondItem extends SqlCondBase<SqlCondItem> {
...
@@ -79,7 +80,7 @@ public class SqlCondItem extends SqlCondBase<SqlCondItem> {
throw
YzgError
.
getRuntimeException
(
"026"
,
this
.
getClass
().
getSimpleName
(),
sqlData
.
getName
(),
fieldName
);
throw
YzgError
.
getRuntimeException
(
"026"
,
this
.
getClass
().
getSimpleName
(),
sqlData
.
getName
(),
fieldName
);
}
}
Object
value
=
ObjectHelper
.
get
(
model
,
fieldName
);
Object
value
=
ObjectHelper
.
get
(
model
,
fieldName
);
List
list
=
ArrayHelper
.
getList
(
value
);
Collection
list
=
ArrayHelper
.
getCollection
(
value
);
for
(
Object
item
:
list
)
{
for
(
Object
item
:
list
)
{
// 处理代码片段
// 处理代码片段
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
View file @
00f7aa00
...
@@ -707,7 +707,7 @@ public abstract class BaseDaoSql {
...
@@ -707,7 +707,7 @@ public abstract class BaseDaoSql {
sql
=
getSqlCodeParameter
(
paras
,
sql
,
name
,
(
SqlCodeParameter
)
val
);
sql
=
getSqlCodeParameter
(
paras
,
sql
,
name
,
(
SqlCodeParameter
)
val
);
}
else
{
}
else
{
// 判断是否为数组
// 判断是否为数组
boolean
isArray
=
ArrayHelper
.
isArrayOr
List
(
val
);
boolean
isArray
=
ArrayHelper
.
isArrayOr
Collection
(
val
);
if
(
isArray
)
{
if
(
isArray
)
{
sql
=
getListSql
(
paras
,
sql
,
name
,
val
);
sql
=
getListSql
(
paras
,
sql
,
name
,
val
);
}
else
{
}
else
{
...
@@ -747,13 +747,11 @@ public abstract class BaseDaoSql {
...
@@ -747,13 +747,11 @@ public abstract class BaseDaoSql {
* @return 获取包含列表的SQL语句的值
* @return 获取包含列表的SQL语句的值
*/
*/
private
String
getListSql
(
List
<
Object
>
paras
,
String
sql
,
String
name
,
Object
val
)
{
private
String
getListSql
(
List
<
Object
>
paras
,
String
sql
,
String
name
,
Object
val
)
{
List
list
=
ArrayHelper
.
getList
(
val
);
Collection
list
=
ArrayHelper
.
getCollection
(
val
);
int
length
=
list
.
size
();
// 进行循环
// 进行循环
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
int
i
=
0
;
Object
item
=
list
.
get
(
i
);
for
(
Object
item
:
list
)
{
// 判断列表是否为常规对象
// 判断列表是否为常规对象
if
(
item
==
null
||
item
instanceof
Number
||
item
.
getClass
().
isEnum
()
||
item
instanceof
String
||
item
instanceof
Date
)
{
if
(
item
==
null
||
item
instanceof
Number
||
item
.
getClass
().
isEnum
()
||
item
instanceof
String
||
item
instanceof
Date
)
{
addArrayIn
(
paras
,
sb
,
i
,
item
);
addArrayIn
(
paras
,
sb
,
i
,
item
);
...
@@ -762,6 +760,7 @@ public abstract class BaseDaoSql {
...
@@ -762,6 +760,7 @@ public abstract class BaseDaoSql {
}
else
{
}
else
{
addArrayTable
(
paras
,
sb
,
i
,
item
);
addArrayTable
(
paras
,
sb
,
i
,
item
);
}
}
i
++;
}
}
// 生成最终的SQL语句
// 生成最终的SQL语句
...
...
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