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
a6af90cb
Commit
a6af90cb
authored
May 19, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将源码打包进jar包
parent
92e9827a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
45 deletions
+40
-45
AllBeanRowMapper.java
...c/main/java/com/yanzuoguang/db/impl/AllBeanRowMapper.java
+40
-45
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/db/impl/AllBeanRowMapper.java
View file @
a6af90cb
...
...
@@ -8,7 +8,8 @@ import com.yanzuoguang.util.base.ObjectHelper;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.vo.MapRow
;
import
org.springframework.beans.*
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.TypeMismatchException
;
import
org.springframework.dao.DataRetrievalFailureException
;
import
org.springframework.jdbc.core.RowMapper
;
import
org.springframework.jdbc.support.JdbcUtils
;
...
...
@@ -29,10 +30,15 @@ import java.util.Map;
*/
public
class
AllBeanRowMapper
<
T
>
implements
RowMapper
<
T
>
{
/**
* 缓存的处理类
*/
private
static
final
Map
<
Class
<?>,
Object
>
CACHE
=
new
HashMap
<>();
/**
* 需要映射的Class
*/
private
Class
<
T
>
mappedClass
;
private
final
Class
<
T
>
mappedClass
;
/**
* 是否属于映射方式
...
...
@@ -52,20 +58,33 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 构造函数
*
* @param mappedClass
* @param mappedClass
类型
*/
public
AllBeanRowMapper
(
Class
<
T
>
mappedClass
,
ConfigDb
configDb
)
{
initialize
(
mappedClass
)
;
this
.
mappedClass
=
mappedClass
;
this
.
configDb
=
configDb
;
initialize
();
}
/**
*
初始化元素之间的映射关系
*
获取可以转化实体
*
* @param mappedClass the mapped class.
* @param cls 类型
* @param <T> 泛型类型
* @return 可转换的实体
*/
protected
void
initialize
(
Class
<
T
>
mappedClass
)
{
this
.
mappedClass
=
mappedClass
;
public
static
<
T
>
AllBeanRowMapper
<
T
>
getInstance
(
Class
<
T
>
cls
,
ConfigDb
configDb
)
{
if
(!
CACHE
.
containsKey
(
cls
))
{
CACHE
.
put
(
cls
,
new
AllBeanRowMapper
<>(
cls
,
configDb
));
}
Object
o
=
CACHE
.
get
(
cls
);
return
(
AllBeanRowMapper
<
T
>)
o
;
}
/**
* 初始化元素之间的映射关系
*/
protected
void
initialize
()
{
if
(
ObjectHelper
.
isSub
(
MapRow
.
class
,
mappedClass
)
||
ObjectHelper
.
isSub
(
Map
.
class
,
mappedClass
))
{
isMapping
=
true
;
}
else
{
...
...
@@ -107,8 +126,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 获取列明
*
* @param name
* @return
* @param name
待转换的名称
* @return
名称
*/
private
static
String
underscoreNameBase
(
String
name
)
{
if
(!
StringUtils
.
hasLength
(
name
))
{
...
...
@@ -120,8 +139,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 将行进行转换
*
* @param from
* @return
* @param from
名称
* @return
行信息
*/
public
static
MapRow
toLoweRow
(
MapRow
from
)
{
MapRow
to
=
new
MapRow
();
...
...
@@ -134,8 +153,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 获取空值字段
*
* @param from
* @return
* @param from
名称
* @return
行信息
*/
public
static
Object
getLoweRowField
(
MapRow
from
,
String
field
)
{
field
=
underscoreNameBase
(
field
);
...
...
@@ -156,13 +175,12 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
@Override
public
T
mapRow
(
ResultSet
rs
,
int
rowNumber
)
throws
SQLException
,
TypeMismatchException
{
Assert
.
state
(
this
.
mappedClass
!=
null
,
"Mapped class was not specified"
);
T
mappedObject
=
BeanUtils
.
instantiate
(
this
.
mappedClass
);
ResultSetMetaData
rsmd
=
rs
.
getMetaData
();
int
columnCount
=
rsmd
.
getColumnCount
();
T
mappedObject
=
BeanUtils
.
instantiateClass
(
this
.
mappedClass
);
ResultSetMetaData
resultSet
=
rs
.
getMetaData
();
int
columnCount
=
resultSet
.
getColumnCount
();
for
(
int
index
=
1
;
index
<=
columnCount
;
index
++)
{
String
column
=
JdbcUtils
.
lookupColumnName
(
r
smd
,
index
);
String
column
=
JdbcUtils
.
lookupColumnName
(
r
esultSet
,
index
);
String
underscoredName
=
underscoreName
(
column
);
if
(
this
.
isMapping
)
{
Object
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
);
...
...
@@ -170,12 +188,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
value
=
JdbcUtils
.
getResultSetValue
(
rs
,
index
,
String
.
class
);
}
((
Map
)
mappedObject
).
put
(
getCamelCase
(
column
),
value
);
}
else
if
(!
this
.
typeField
.
containsKey
(
underscoredName
))
{
continue
;
}
else
{
}
else
if
(
this
.
typeField
.
containsKey
(
underscoredName
))
{
MethodField
pd
=
this
.
typeField
.
get
(
underscoredName
);
Class
<?>
type
;
if
(
pd
.
getField
()
!=
null
)
{
type
=
pd
.
getField
().
getType
();
}
else
{
...
...
@@ -210,8 +225,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 设置SameCase命名方式
*
* @param column
* @return
* @param column
待转换的列名
* @return
转换后的列名
*/
private
String
getCamelCase
(
String
column
)
{
switch
(
configDb
.
getRowNameType
())
{
...
...
@@ -224,24 +239,4 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
}
return
column
;
}
/**
* 缓存的处理类
*/
private
static
final
Map
<
Class
,
Object
>
Cache
=
new
HashMap
<
Class
,
Object
>();
/**
* 获取可以转化实体
*
* @param cls 类型
* @param <T> 泛型类型
* @return 可转换的实体
*/
public
static
<
T
extends
Object
>
AllBeanRowMapper
<
T
>
getInstance
(
Class
<
T
>
cls
,
ConfigDb
configDb
)
{
if
(!
Cache
.
containsKey
(
cls
))
{
Cache
.
put
(
cls
,
new
AllBeanRowMapper
<
T
>(
cls
,
configDb
));
}
AllBeanRowMapper
<
T
>
ret
=
(
AllBeanRowMapper
<
T
>)
Cache
.
get
(
cls
);
return
ret
;
}
}
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