Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Springboot Plus
Commits
93d09500
Commit
93d09500
authored
Sep 23, 2019
by
trumansdo
Browse files
初步完成结果映射的自定义BeanProcessor编码
parent
94679e6a
Changes
3
Hide whitespace changes
Inline
Side-by-side
plus-admin/admin-console/src/test/java/CustomBeanProcessorTest.java
View file @
93d09500
...
@@ -84,7 +84,6 @@ public class CustomBeanProcessorTest {
...
@@ -84,7 +84,6 @@ public class CustomBeanProcessorTest {
testMapping
.
put
(
"password"
,
"password"
);
testMapping
.
put
(
"password"
,
"password"
);
testMapping
.
put
(
"resultType"
,
entity
.
Test
.
class
.
getCanonicalName
());
testMapping
.
put
(
"resultType"
,
entity
.
Test
.
class
.
getCanonicalName
());
routeMapping
.
put
(
"test"
,
testMapping
);
routeMapping
.
put
(
"meta"
,
metaMapping
);
routeMapping
.
put
(
"meta"
,
metaMapping
);
routeMapping
.
put
(
"resultType"
,
CoreRoute
.
class
.
getCanonicalName
());
routeMapping
.
put
(
"resultType"
,
CoreRoute
.
class
.
getCanonicalName
());
...
...
plus-admin/admin-console/src/test/java/processor/JsonBeanProcessor.java
View file @
93d09500
package
processor
;
package
processor
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.map.MapUtil
;
import
cn.hutool.core.map.MapUtil
;
import
cn.hutool.core.util.ClassUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.ibeetl.admin.core.util.CacheUtil
;
import
com.ibeetl.admin.core.util.CacheUtil
;
import
java.beans.IntrospectionException
;
import
java.beans.IntrospectionException
;
import
java.beans.PropertyDescriptor
;
import
java.beans.PropertyDescriptor
;
...
@@ -9,6 +13,7 @@ import java.sql.ResultSet;
...
@@ -9,6 +13,7 @@ import java.sql.ResultSet;
import
java.sql.ResultSetMetaData
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
...
@@ -37,64 +42,112 @@ public class JsonBeanProcessor extends BeanProcessor {
...
@@ -37,64 +42,112 @@ public class JsonBeanProcessor extends BeanProcessor {
ResultSetMetaData
rsmd
=
rs
.
getMetaData
();
ResultSetMetaData
rsmd
=
rs
.
getMetaData
();
int
[]
columnToProperty
=
this
.
mapColumnsToProperties
(
type
,
rsmd
,
props
);
int
[]
columnToProperty
=
this
.
mapColumnsToProperties
(
type
,
rsmd
,
props
);
results
.
add
(
this
.
createBean
(
sqlId
,
rs
,
type
,
props
,
columnToProperty
));
GridMapping
mapping
=
(
GridMapping
)
CacheUtil
.
get
(
"Route_Mapping"
);
if
(
null
==
mapping
)
{
do
{
results
.
add
(
super
.
createBean
(
sqlId
,
rs
,
type
,
props
,
columnToProperty
));
}
while
(
rs
.
next
());
}
else
{
fillMappingRow
(
rs
,
mapping
);
results
=
convertMapping
(
mapping
,
type
);
}
return
results
;
return
results
;
}
}
/** 创建 一个新的对象,并从ResultSet初始化 */
/**
@Override
* 循环转换整个网格映射
protected
<
T
>
T
createBean
(
*
String
sqlId
,
ResultSet
rs
,
Class
<
T
>
type
,
PropertyDescriptor
[]
props
,
int
[]
columnToProperty
)
* @param mapping
throws
SQLException
{
* @param objType
GridMapping
gridMapping
=
(
GridMapping
)
CacheUtil
.
get
(
"Route_Mapping"
);
* @param <T>
if
(
null
==
gridMapping
)
{
* @return
return
super
.
createBean
(
sqlId
,
rs
,
type
,
props
,
columnToProperty
);
*/
public
<
T
>
List
<
T
>
convertMapping
(
GridMapping
mapping
,
Class
<
T
>
objType
)
throws
SQLException
{
List
<
T
>
results
=
new
ArrayList
<
T
>();
List
<
GridRow
>
mappingNestedRows
=
mapping
.
getNestedRows
();
for
(
GridRow
mappingNestedRow
:
mappingNestedRows
)
{
T
obj
=
convertRow
(
mappingNestedRow
,
objType
);
results
.
add
(
obj
);
}
}
return
results
;
fillMappingRow
(
rs
,
gridMapping
);
System
.
out
.
println
(
gridMapping
);
return
null
;
}
}
public
String
print
(
GridColumn
column
)
{
/**
List
<
GridRow
>
nestedRows
=
column
.
getNestedRows
();
* 递归转换整个网格行
String
print
=
""
;
*
for
(
GridRow
nestedRow
:
nestedRows
)
{
* @param row
List
<
GridColumn
>
nestedColumns
=
nestedRow
.
getNestedColumns
();
* @param objType
GridColumn
gridColumn
=
nestedColumns
.
get
(
0
);
* @param <T>
print
=
print
.
concat
(
gridColumn
.
getBeanMap
().
toString
()
+
"\n\t"
);
* @return
for
(
int
i
=
1
;
i
<
nestedColumns
.
size
();
i
++)
{
*/
GridColumn
tempCol
=
nestedColumns
.
get
(
i
);
public
<
T
>
T
convertRow
(
GridRow
row
,
Class
<
T
>
objType
)
throws
SQLException
{
if
(
tempCol
.
getBeanMap
().
isEmpty
()
&&
!
tempCol
.
getNestedRows
().
isEmpty
())
{
T
obj
=
super
.
newInstance
(
objType
);
/*是一个容器性质的列*/
print
=
print
.
concat
(
print
(
tempCol
));
List
<
GridColumn
>
nestedColumns
=
row
.
getNestedColumns
();
}
else
if
(!
tempCol
.
getBeanMap
().
isEmpty
()
&&
tempCol
.
getNestedRows
().
isEmpty
())
{
GridColumn
curObjCol
=
nestedColumns
.
get
(
0
);
/*对象存储性质的列*/
print
=
print
.
concat
(
tempCol
.
getBeanMap
().
toString
()
+
"\n\t"
);
for
(
int
i
=
1
;
i
<
nestedColumns
.
size
();
i
++)
{
GridColumn
nestedColumn
=
nestedColumns
.
get
(
i
);
GridHeader
mappingHeader
=
nestedColumn
.
getMappingHeader
();
String
resultType
=
mappingHeader
.
getResultType
();
List
<
GridRow
>
nestedRows
=
nestedColumn
.
getNestedRows
();
for
(
GridRow
nestedRow
:
nestedRows
)
{
if
(
StrUtil
.
isNotBlank
(
resultType
))
{
/*在映射中标明类型,证明是复杂类型*/
Class
nestedPropType
=
ClassUtil
.
loadClass
(
resultType
);
String
nestedPropName
=
mappingHeader
.
getNestedPropName
();
boolean
isCollection
=
mappingHeader
.
getIsCollection
();
Object
resultObj
=
BeanUtil
.
getFieldValue
(
obj
,
nestedPropName
);
Object
nestedPropObj
=
convertRow
(
nestedRow
,
nestedPropType
);
if
(
isCollection
)
{
((
Collection
)
resultObj
).
add
(
nestedPropObj
);
resultObj
=
CollUtil
.
removeNull
(((
Collection
)
resultObj
));
}
else
{
resultObj
=
nestedPropObj
;
}
BeanUtil
.
setFieldValue
(
obj
,
nestedPropName
,
resultObj
);
}
else
{
/*在映射中没有标明类型,证明是基本类型*/
String
nestedPropName
=
mappingHeader
.
getNestedPropName
();
boolean
isCollection
=
mappingHeader
.
getIsCollection
();
Object
resultObj
=
BeanUtil
.
getFieldValue
(
obj
,
nestedPropName
);
Map
<
String
,
Object
>
beanMap
=
nestedRow
.
getNestedColumns
().
get
(
0
).
getBeanMap
();
/*几乎此处说明内嵌的字段是一个基本类型的集合,所以beanmap中应该只有一个值*/
Object
nestedPropObj
=
CollUtil
.
getFirst
(
beanMap
.
values
());
if
(
isCollection
)
{
((
Collection
)
resultObj
).
add
(
nestedPropObj
);
resultObj
=
CollUtil
.
removeNull
(((
Collection
)
resultObj
));
}
else
{
resultObj
=
nestedPropObj
;
}
BeanUtil
.
setFieldValue
(
obj
,
nestedPropName
,
resultObj
);
}
}
}
}
}
}
return
print
;
}
BeanUtil
.
fillBeanWithMap
(
curObjCol
.
getBeanMap
(),
obj
,
false
,
true
);
return
obj
;
}
/**
* 填充整个网格映射mapping数据结构
*
* @param resultSet
* @param mapping
* @throws SQLException
*/
protected
void
fillMappingRow
(
ResultSet
resultSet
,
GridMapping
mapping
)
throws
SQLException
{
protected
void
fillMappingRow
(
ResultSet
resultSet
,
GridMapping
mapping
)
throws
SQLException
{
GridHeader
header
=
mapping
.
getHeader
();
GridHeader
header
=
mapping
.
getHeader
();
GridColumn
column
=
new
GridColumn
();
GridColumn
column
=
new
GridColumn
();
/** flag:用来判断结果集是否应该通过 mapping.nextRow(); 生成新的行 */
Boolean
flag
=
true
;
while
(
resultSet
.
next
())
{
while
(
resultSet
.
next
())
{
List
<
GridRow
>
mappingNestedRows
=
mapping
.
getNestedRows
();
List
<
GridRow
>
mappingNestedRows
=
mapping
.
getNestedRows
();
GridRow
row
=
fillRowColumn
(
resultSet
,
header
,
column
);
GridRow
row
=
fillRowColumn
(
resultSet
,
header
,
column
);
if
(!
mappingNestedRows
.
contains
(
row
))
{
if
(!
mappingNestedRows
.
contains
(
row
))
{
mappingNestedRows
.
add
(
row
);
mappingNestedRows
.
add
(
row
);
}
}
}
}
System
.
out
.
println
(
11
);
}
}
/**
flag:用来判断结果集是否 next()
*/
/**
递归填充mapping结构中的每一行
*/
protected
GridRow
fillRowColumn
(
ResultSet
resultSet
,
GridHeader
header
,
GridColumn
column
)
protected
GridRow
fillRowColumn
(
ResultSet
resultSet
,
GridHeader
header
,
GridColumn
column
)
throws
SQLException
{
throws
SQLException
{
/*搜寻已经存在的row,如果没有,则插入一个*/
/*搜寻已经存在的row,如果没有,则插入一个*/
...
@@ -118,7 +171,6 @@ public class JsonBeanProcessor extends BeanProcessor {
...
@@ -118,7 +171,6 @@ public class JsonBeanProcessor extends BeanProcessor {
nestedRows
.
add
(
nestedRow
);
nestedRows
.
add
(
nestedRow
);
}
}
}
}
row
.
getNestedColumns
().
get
(
0
).
setBeanMap
(
beanMap
);
row
.
getNestedColumns
().
get
(
0
).
setBeanMap
(
beanMap
);
return
row
;
return
row
;
}
}
...
...
plus-admin/admin-console/src/test/java/resultmap/GridColumn.java
View file @
93d09500
...
@@ -65,7 +65,7 @@ public class GridColumn implements Serializable {
...
@@ -65,7 +65,7 @@ public class GridColumn implements Serializable {
if
(
ObjectUtil
.
isNull
(
entry
.
getValue
()))
continue
;
if
(
ObjectUtil
.
isNull
(
entry
.
getValue
()))
continue
;
hs
+=
entry
.
getValue
().
hashCode
();
hs
+=
entry
.
getValue
().
hashCode
();
}
}
return
hs
/
42
;
return
hs
*
42
;
}
}
public
List
<
GridRow
>
getNestedRows
()
{
public
List
<
GridRow
>
getNestedRows
()
{
...
...
Write
Preview
Supports
Markdown
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