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
d38d4566
Commit
d38d4566
authored
Jun 10, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
常规BUG的修改
parent
9fad4ad8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
103 deletions
+50
-103
ObjectHelper.java
...src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
+15
-14
AllBeanRowMapper.java
...c/main/java/com/yanzuoguang/db/impl/AllBeanRowMapper.java
+35
-89
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/base/ObjectHelper.java
View file @
d38d4566
...
@@ -5,10 +5,7 @@ import com.yanzuoguang.util.exception.CodeException;
...
@@ -5,10 +5,7 @@ import com.yanzuoguang.util.exception.CodeException;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.*
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.util.*
;
import
java.util.*
;
/**
/**
...
@@ -206,17 +203,21 @@ public class ObjectHelper {
...
@@ -206,17 +203,21 @@ public class ObjectHelper {
return
;
return
;
}
}
try
{
try
{
if
(
item
.
getField
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getField
().
getModifiers
()))
{
setByType
(
target
,
item
,
value
);
Class
toType
=
item
.
getField
().
getType
();
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getField
().
set
(
target
,
toValue
);
}
else
if
(
item
.
getSetMethod
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getSetMethod
().
getModifiers
()))
{
Class
toType
=
item
.
getSetMethod
().
getParameterTypes
()[
0
];
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getSetMethod
().
invoke
(
target
,
toValue
);
}
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ObjectHelper
.
class
,
ex
,
field
);
ExceptionHelper
.
handleException
(
ObjectHelper
.
class
,
ex
,
item
.
getName
());
}
}
public
static
void
setByType
(
Object
target
,
MethodField
item
,
Object
value
)
throws
IllegalAccessException
,
InvocationTargetException
{
if
(
item
.
getField
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getField
().
getModifiers
()))
{
Class
toType
=
item
.
getField
().
getType
();
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getField
().
set
(
target
,
toValue
);
}
else
if
(
item
.
getSetMethod
()
!=
null
&&
Modifier
.
isPublic
(
item
.
getSetMethod
().
getModifiers
()))
{
Class
toType
=
item
.
getSetMethod
().
getParameterTypes
()[
0
];
Object
toValue
=
StringHelper
.
to
(
toType
,
value
);
item
.
getSetMethod
().
invoke
(
target
,
toValue
);
}
}
}
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/AllBeanRowMapper.java
View file @
d38d4566
...
@@ -2,10 +2,12 @@ package com.yanzuoguang.db.impl;
...
@@ -2,10 +2,12 @@ package com.yanzuoguang.db.impl;
import
com.yanzuoguang.dao.DaoConst
;
import
com.yanzuoguang.dao.DaoConst
;
import
com.yanzuoguang.extend.ConfigDb
;
import
com.yanzuoguang.extend.ConfigDb
;
import
com.yanzuoguang.util.base.MethodField
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.vo.MapRow
;
import
com.yanzuoguang.util.vo.MapRow
;
import
jdk.internal.joptsimple.util.KeyValuePair
;
import
org.springframework.beans.*
;
import
org.springframework.beans.*
;
import
org.springframework.dao.DataRetrievalFailureException
;
import
org.springframework.dao.DataRetrievalFailureException
;
import
org.springframework.jdbc.core.RowMapper
;
import
org.springframework.jdbc.core.RowMapper
;
...
@@ -36,24 +38,14 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
...
@@ -36,24 +38,14 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
private
Class
<
T
>
mappedClass
;
private
Class
<
T
>
mappedClass
;
/**
/**
* Mappings是否属于字段s
* 是否属于映射方式
*/
private
Map
<
String
,
Boolean
>
mappedIsFields
;
/**
* 需要映射的字段
*/
*/
private
Map
<
String
,
Field
>
mappedFields
;
private
boolean
isMapping
=
false
;
/**
/**
*
需要映射的属性
*
获取字段映射关系
*/
*/
private
Map
<
String
,
PropertyDescriptor
>
mappedPropertys
;
private
Map
<
String
,
MethodField
>
typeField
;
/**
* 是否属于映射方式
*/
private
boolean
isMapping
=
false
;
/**
/**
* 配置信息
* 配置信息
...
@@ -77,32 +69,16 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
...
@@ -77,32 +69,16 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
*/
*/
protected
void
initialize
(
Class
<
T
>
mappedClass
)
{
protected
void
initialize
(
Class
<
T
>
mappedClass
)
{
this
.
mappedClass
=
mappedClass
;
this
.
mappedClass
=
mappedClass
;
this
.
mappedFields
=
new
HashMap
<>(
DaoConst
.
COLLECTION_INIT_SIZE
);
this
.
mappedPropertys
=
new
HashMap
<>(
DaoConst
.
COLLECTION_INIT_SIZE
);
this
.
mappedIsFields
=
new
HashMap
<>(
DaoConst
.
COLLECTION_INIT_SIZE
);
if
(
ObjectHelper
.
isSub
(
MapRow
.
class
,
mappedClass
)
||
ObjectHelper
.
isSub
(
Map
.
class
,
mappedClass
))
{
if
(
ObjectHelper
.
isSub
(
MapRow
.
class
,
mappedClass
)
||
ObjectHelper
.
isSub
(
Map
.
class
,
mappedClass
))
{
isMapping
=
true
;
isMapping
=
true
;
}
else
{
}
else
{
Field
[]
fields
=
mappedClass
.
getFields
();
Map
<
String
,
MethodField
>
temp
=
ObjectHelper
.
getTypeField
(
mappedClass
);
for
(
Field
item
:
fields
)
{
typeField
=
new
HashMap
<>(
temp
.
size
());
if
(
Modifier
.
isStatic
(
item
.
getModifiers
()))
{
continue
;
}
String
name
=
item
.
getName
();
String
underscoredName
=
underscoreName
(
name
);
this
.
mappedFields
.
put
(
underscoredName
,
item
);
this
.
mappedIsFields
.
put
(
underscoredName
,
true
);
}
PropertyDescriptor
[]
pds
=
BeanUtils
.
getPropertyDescriptors
(
mappedClass
);
for
(
Map
.
Entry
<
String
,
MethodField
>
item
:
temp
.
entrySet
())
{
for
(
PropertyDescriptor
item
:
pds
)
{
String
name
=
item
.
getKey
();
if
(
item
.
getWriteMethod
()
!=
null
)
{
String
underscoredName
=
underscoreName
(
name
);
String
name
=
item
.
getName
();
this
.
typeField
.
put
(
underscoredName
,
item
.
getValue
());
String
underscoredName
=
underscoreName
(
name
);
this
.
mappedPropertys
.
put
(
underscoredName
,
item
);
this
.
mappedIsFields
.
put
(
underscoredName
,
false
);
}
}
}
}
}
}
}
...
@@ -171,8 +147,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
...
@@ -171,8 +147,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
public
T
mapRow
(
ResultSet
rs
,
int
rowNumber
)
throws
SQLException
,
TypeMismatchException
{
public
T
mapRow
(
ResultSet
rs
,
int
rowNumber
)
throws
SQLException
,
TypeMismatchException
{
Assert
.
state
(
this
.
mappedClass
!=
null
,
"Mapped class was not specified"
);
Assert
.
state
(
this
.
mappedClass
!=
null
,
"Mapped class was not specified"
);
T
mappedObject
=
BeanUtils
.
instantiate
(
this
.
mappedClass
);
T
mappedObject
=
BeanUtils
.
instantiate
(
this
.
mappedClass
);
BeanWrapper
bw
=
PropertyAccessorFactory
.
forBeanPropertyAccess
(
mappedObject
);
initBeanWrapper
(
bw
);
ResultSetMetaData
rsmd
=
rs
.
getMetaData
();
ResultSetMetaData
rsmd
=
rs
.
getMetaData
();
int
columnCount
=
rsmd
.
getColumnCount
();
int
columnCount
=
rsmd
.
getColumnCount
();
...
@@ -186,53 +160,36 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
...
@@ -186,53 +160,36 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
String
.
class
);
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
String
.
class
);
}
}
((
Map
)
mappedObject
).
put
(
getCamelCase
(
column
),
value
);
((
Map
)
mappedObject
).
put
(
getCamelCase
(
column
),
value
);
}
else
if
(!
this
.
mappedIsFields
.
containsKey
(
underscoredName
))
{
}
else
if
(!
this
.
typeField
.
containsKey
(
underscoredName
))
{
continue
;
continue
;
}
else
if
(!
this
.
mappedIsFields
.
get
(
underscoredName
))
{
PropertyDescriptor
pd
=
this
.
mappedPropertys
.
get
(
underscoredName
);
Class
<?>
type
=
pd
.
getPropertyType
();
try
{
Object
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
type
);
if
(
configDb
.
isPrintMapper
()
&&
rowNumber
==
0
)
{
Log
.
info
(
AllBeanRowMapper
.
class
,
"Mapping column '%s' to property '%s' of type %s"
,
column
,
pd
.
getName
(),
type
);
}
try
{
bw
.
setPropertyValue
(
pd
.
getName
(),
value
);
}
catch
(
TypeMismatchException
e
)
{
if
(
value
==
null
)
{
Log
.
info
(
AllBeanRowMapper
.
class
,
"Intercepted TypeMismatchException for row %d and column '%s' with "
+
"value %s when setting property '%s' of type %s on object: %s"
,
rowNumber
,
column
,
StringHelper
.
EMPTY
,
pd
.
getName
(),
type
,
mappedObject
);
}
else
{
throw
e
;
}
}
}
catch
(
NotWritablePropertyException
ex
)
{
throw
new
DataRetrievalFailureException
(
"Unable to map column "
+
column
+
" to property "
+
pd
.
getName
(),
ex
);
}
}
else
{
}
else
{
Field
pd
=
this
.
mappedFields
.
get
(
underscoredName
);
MethodField
pd
=
this
.
typeField
.
get
(
underscoredName
);
Class
<?>
type
=
pd
.
getType
();
Class
<?>
type
;
if
(
pd
.
getField
()
!=
null
)
{
type
=
pd
.
getField
().
getType
();
}
else
{
type
=
pd
.
getGetMethod
().
getReturnType
();
}
Object
value
=
null
;
try
{
try
{
Object
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
type
);
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
type
);
if
(
configDb
.
isPrintMapper
()
&&
rowNumber
==
0
)
{
if
(
configDb
.
isPrintMapper
()
&&
rowNumber
==
0
)
{
Log
.
info
(
AllBeanRowMapper
.
class
,
"Mapping column '%s' to property '%s' of type %s"
,
column
,
pd
.
getName
(),
type
);
Log
.
info
(
AllBeanRowMapper
.
class
,
"Mapping column '%s' to property '%s' of type %s"
,
column
,
pd
.
getName
(),
type
);
}
}
try
{
ObjectHelper
.
setByType
(
mappedObject
,
pd
,
value
);
pd
.
set
(
mappedObject
,
value
);
}
catch
(
TypeMismatchException
e
)
{
}
catch
(
IllegalAccessException
e
)
{
if
(
value
==
null
)
{
if
(
value
==
null
)
{
Log
.
info
(
AllBeanRowMapper
.
class
,
Log
.
info
(
AllBeanRowMapper
.
class
,
"Intercepted TypeMismatchException for row %d and column '%s' with "
+
"Intercepted TypeMismatchException for row %d and column '%s' with "
+
"value %s when setting property '%s' of type %s on object: %s"
,
"value %s when setting property '%s' of type %s on object: %s"
,
rowNumber
,
column
,
StringHelper
.
EMPTY
,
pd
.
getName
(),
type
,
mappedObject
);
rowNumber
,
column
,
StringHelper
.
EMPTY
,
pd
.
getName
(),
type
,
mappedObject
);
}
else
{
}
else
{
throw
e
;
throw
e
;
}
}
}
}
catch
(
IllegalAccess
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
DataRetrievalFailureException
(
"Unable to map column "
+
column
+
" to
field
"
+
pd
.
getName
(),
ex
);
throw
new
DataRetrievalFailureException
(
"Unable to map column "
+
column
+
" to
property
"
+
pd
.
getName
(),
ex
);
}
}
}
}
}
}
...
@@ -258,17 +215,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
...
@@ -258,17 +215,6 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
return
column
;
return
column
;
}
}
/**
* Initialize the given BeanWrapper to be used for row mapping.
* To be called for each row.
* <p>The default implementation is empty. Can be overridden in subclasses.
*
* @param bw the BeanWrapper to initialize
*/
protected
void
initBeanWrapper
(
BeanWrapper
bw
)
{
}
/**
/**
* 缓存的处理类
* 缓存的处理类
*/
*/
...
...
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