Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
后端镜像
java镜像
Smart Framework
提交
e15177a7
S
Smart Framework
项目概览
后端镜像
/
java镜像
/
Smart Framework
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Smart Framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e15177a7
编写于
6月 22, 2014
作者:
黄
黄勇
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
v2.3.1
上级
49683ea7
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
108 addition
and
69 deletion
+108
-69
pom.xml
pom.xml
+1
-1
src/main/java/org/smart4j/framework/aop/AspectProxy.java
src/main/java/org/smart4j/framework/aop/AspectProxy.java
+5
-0
src/main/java/org/smart4j/framework/dao/SqlHelper.java
src/main/java/org/smart4j/framework/dao/SqlHelper.java
+5
-10
src/main/java/org/smart4j/framework/dao/impl/DefaultDataAccessor.java
...a/org/smart4j/framework/dao/impl/DefaultDataAccessor.java
+6
-6
src/main/java/org/smart4j/framework/orm/DataSet.java
src/main/java/org/smart4j/framework/orm/DataSet.java
+26
-22
src/main/java/org/smart4j/framework/orm/EntityHelper.java
src/main/java/org/smart4j/framework/orm/EntityHelper.java
+59
-27
src/main/java/org/smart4j/framework/util/MapUtil.java
src/main/java/org/smart4j/framework/util/MapUtil.java
+6
-3
未找到文件。
pom.xml
浏览文件 @
e15177a7
...
...
@@ -14,7 +14,7 @@
</parent>
<artifactId>
smart-framework
</artifactId>
<version>
2.3
</version>
<version>
2.3
.1
</version>
<name>
smart-framework
</name>
<description>
Smart Framework is a lightweight Java Web Framework
</description>
...
...
src/main/java/org/smart4j/framework/aop/AspectProxy.java
浏览文件 @
e15177a7
package
org.smart4j.framework.aop
;
import
java.lang.reflect.Method
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.smart4j.framework.aop.proxy.Proxy
;
import
org.smart4j.framework.aop.proxy.ProxyChain
;
...
...
@@ -12,6 +14,8 @@ import org.smart4j.framework.aop.proxy.ProxyChain;
*/
public
abstract
class
AspectProxy
implements
Proxy
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AspectProxy
.
class
);
@Override
public
final
Object
doProxy
(
ProxyChain
proxyChain
)
throws
Throwable
{
Object
result
=
null
;
...
...
@@ -30,6 +34,7 @@ public abstract class AspectProxy implements Proxy {
result
=
proxyChain
.
doProxyChain
();
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"AOP 异常"
,
e
);
error
(
cls
,
method
,
params
,
e
);
throw
e
;
}
finally
{
...
...
src/main/java/org/smart4j/framework/dao/SqlHelper.java
浏览文件 @
e15177a7
...
...
@@ -4,7 +4,7 @@ import java.util.Collection;
import
java.util.Map
;
import
java.util.Properties
;
import
org.smart4j.framework.FrameworkConstant
;
import
org.smart4j.framework.orm.
annotation.Table
;
import
org.smart4j.framework.orm.
EntityHelper
;
import
org.smart4j.framework.util.CollectionUtil
;
import
org.smart4j.framework.util.MapUtil
;
import
org.smart4j.framework.util.PropsUtil
;
...
...
@@ -56,7 +56,7 @@ public class SqlHelper {
StringBuilder
columns
=
new
StringBuilder
(
" "
);
StringBuilder
values
=
new
StringBuilder
(
" values "
);
for
(
String
fieldName
:
fieldNames
)
{
String
columnName
=
StringUtil
.
camelhumpToUnderline
(
fieldName
);
String
columnName
=
EntityHelper
.
getColumnName
(
entityClass
,
fieldName
);
if
(
i
==
0
)
{
columns
.
append
(
"("
).
append
(
columnName
);
values
.
append
(
"(?"
);
...
...
@@ -93,7 +93,8 @@ public class SqlHelper {
sql
.
append
(
" set "
);
int
i
=
0
;
for
(
Map
.
Entry
<
String
,
Object
>
fieldEntry
:
fieldMap
.
entrySet
())
{
String
columnName
=
StringUtil
.
camelhumpToUnderline
(
fieldEntry
.
getKey
());
String
fieldName
=
fieldEntry
.
getKey
();
String
columnName
=
EntityHelper
.
getColumnName
(
entityClass
,
fieldName
);
if
(
i
==
0
)
{
sql
.
append
(
columnName
).
append
(
" = ?"
);
}
else
{
...
...
@@ -139,13 +140,7 @@ public class SqlHelper {
}
private
static
String
getTable
(
Class
<?>
entityClass
)
{
String
tableName
;
if
(
entityClass
.
isAnnotationPresent
(
Table
.
class
))
{
tableName
=
entityClass
.
getAnnotation
(
Table
.
class
).
value
();
}
else
{
tableName
=
StringUtil
.
camelhumpToUnderline
(
entityClass
.
getSimpleName
());
}
return
tableName
;
return
EntityHelper
.
getTableName
(
entityClass
);
}
private
static
String
generateWhere
(
String
condition
)
{
...
...
src/main/java/org/smart4j/framework/dao/impl/DefaultDataAccessor.java
浏览文件 @
e15177a7
...
...
@@ -52,9 +52,9 @@ public class DefaultDataAccessor implements DataAccessor {
public
<
T
>
T
queryEntity
(
Class
<
T
>
entityClass
,
String
sql
,
Object
...
params
)
{
T
result
;
try
{
Map
<
String
,
String
>
fieldMap
=
EntityHelper
.
getEntityMap
().
get
(
entityClass
);
if
(
MapUtil
.
isNotEmpty
(
field
Map
))
{
result
=
queryRunner
.
query
(
sql
,
new
BeanHandler
<
T
>(
entityClass
,
new
BasicRowProcessor
(
new
BeanProcessor
(
field
Map
))),
params
);
Map
<
String
,
String
>
columnMap
=
EntityHelper
.
getColumnMap
(
entityClass
);
if
(
MapUtil
.
isNotEmpty
(
column
Map
))
{
result
=
queryRunner
.
query
(
sql
,
new
BeanHandler
<
T
>(
entityClass
,
new
BasicRowProcessor
(
new
BeanProcessor
(
column
Map
))),
params
);
}
else
{
result
=
queryRunner
.
query
(
sql
,
new
BeanHandler
<
T
>(
entityClass
),
params
);
}
...
...
@@ -70,9 +70,9 @@ public class DefaultDataAccessor implements DataAccessor {
public
<
T
>
List
<
T
>
queryEntityList
(
Class
<
T
>
entityClass
,
String
sql
,
Object
...
params
)
{
List
<
T
>
result
;
try
{
Map
<
String
,
String
>
fieldMap
=
EntityHelper
.
getEntityMap
().
get
(
entityClass
);
if
(
MapUtil
.
isNotEmpty
(
field
Map
))
{
result
=
queryRunner
.
query
(
sql
,
new
BeanListHandler
<
T
>(
entityClass
,
new
BasicRowProcessor
(
new
BeanProcessor
(
field
Map
))),
params
);
Map
<
String
,
String
>
columnMap
=
EntityHelper
.
getColumnMap
(
entityClass
);
if
(
MapUtil
.
isNotEmpty
(
column
Map
))
{
result
=
queryRunner
.
query
(
sql
,
new
BeanListHandler
<
T
>(
entityClass
,
new
BasicRowProcessor
(
new
BeanProcessor
(
column
Map
))),
params
);
}
else
{
result
=
queryRunner
.
query
(
sql
,
new
BeanListHandler
<
T
>(
entityClass
),
params
);
}
...
...
src/main/java/org/smart4j/framework/orm/DataSet.java
浏览文件 @
e15177a7
...
...
@@ -24,7 +24,7 @@ public class DataSet {
* 查询单条数据,并转为相应类型的实体
*/
public
static
<
T
>
T
select
(
Class
<
T
>
entityClass
,
String
condition
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
condition
=
transferCondition
(
entityClass
,
condition
);
String
sql
=
SqlHelper
.
generateSelectSql
(
entityClass
,
condition
,
""
);
return
DatabaseHelper
.
queryEntity
(
entityClass
,
sql
,
params
);
}
...
...
@@ -54,8 +54,8 @@ public class DataSet {
* 查询多条数据,并转为相应类型的实体列表(带有查询条件、排序方式与查询参数)
*/
public
static
<
T
>
List
<
T
>
selectListWithConditionAndSort
(
Class
<
T
>
entityClass
,
String
condition
,
String
sort
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
sort
=
transferSort
(
sort
);
condition
=
transferCondition
(
entityClass
,
condition
);
sort
=
transferSort
(
entityClass
,
sort
);
String
sql
=
SqlHelper
.
generateSelectSql
(
entityClass
,
condition
,
sort
);
return
DatabaseHelper
.
queryEntityList
(
entityClass
,
sql
,
params
);
}
...
...
@@ -64,7 +64,7 @@ public class DataSet {
* 查询数据条数
*/
public
static
long
selectCount
(
Class
<?>
entityClass
,
String
condition
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
condition
=
transferCondition
(
entityClass
,
condition
);
String
sql
=
SqlHelper
.
generateSelectSqlForCount
(
entityClass
,
condition
);
return
DatabaseHelper
.
queryCount
(
sql
,
params
);
}
...
...
@@ -73,8 +73,8 @@ public class DataSet {
* 查询多条数据,并转为列表(分页方式)
*/
public
static
<
T
>
List
<
T
>
selectListForPager
(
int
pageNumber
,
int
pageSize
,
Class
<
T
>
entityClass
,
String
condition
,
String
sort
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
sort
=
transferCondition
(
sort
);
condition
=
transferCondition
(
entityClass
,
condition
);
sort
=
transferCondition
(
entityClass
,
sort
);
String
sql
=
SqlHelper
.
generateSelectSqlForPager
(
pageNumber
,
pageSize
,
entityClass
,
condition
,
sort
);
return
DatabaseHelper
.
queryEntityList
(
entityClass
,
sql
,
params
);
}
...
...
@@ -118,7 +118,7 @@ public class DataSet {
* 根据列名查询单条数据,并转为相应类型的实体
*/
public
static
<
T
>
T
selectColumn
(
Class
<
T
>
entityClass
,
String
columnName
,
String
condition
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
condition
=
transferCondition
(
entityClass
,
condition
);
String
sql
=
SqlHelper
.
generateSelectSql
(
entityClass
,
condition
,
""
);
return
DatabaseHelper
.
queryColumn
(
columnName
,
sql
,
params
);
}
...
...
@@ -127,8 +127,8 @@ public class DataSet {
* 根据列名查询多条数据,并转为相应类型的实体列表
*/
public
static
<
T
>
List
<
T
>
selectColumnList
(
Class
<?>
entityClass
,
String
columnName
,
String
condition
,
String
sort
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
sort
=
transferCondition
(
sort
);
condition
=
transferCondition
(
entityClass
,
condition
);
sort
=
transferCondition
(
entityClass
,
sort
);
String
sql
=
SqlHelper
.
generateSelectSql
(
entityClass
,
condition
,
sort
);
return
DatabaseHelper
.
queryColumnList
(
columnName
,
sql
,
params
);
}
...
...
@@ -164,7 +164,7 @@ public class DataSet {
if
(
MapUtil
.
isEmpty
(
fieldMap
))
{
return
true
;
}
condition
=
transferCondition
(
condition
);
condition
=
transferCondition
(
entityClass
,
condition
);
String
sql
=
SqlHelper
.
generateUpdateSql
(
entityClass
,
fieldMap
,
condition
);
int
rows
=
DatabaseHelper
.
update
(
sql
,
ArrayUtil
.
concat
(
fieldMap
.
values
().
toArray
(),
params
));
return
rows
>
0
;
...
...
@@ -195,7 +195,7 @@ public class DataSet {
* 删除相关数据
*/
public
static
boolean
delete
(
Class
<?>
entityClass
,
String
condition
,
Object
...
params
)
{
condition
=
transferCondition
(
condition
);
condition
=
transferCondition
(
entityClass
,
condition
);
String
sql
=
SqlHelper
.
generateDeleteSql
(
entityClass
,
condition
);
int
rows
=
DatabaseHelper
.
update
(
sql
,
params
);
return
rows
>
0
;
...
...
@@ -221,33 +221,37 @@ public class DataSet {
return
delete
(
entityClass
,
condition
,
params
);
}
private
static
String
transferCondition
(
String
condition
)
{
StringBuffer
buffer
=
new
StringBuffer
();
private
static
String
transferCondition
(
Class
<?>
entityClass
,
String
condition
)
{
if
(
StringUtil
.
isNotEmpty
(
condition
))
{
String
regex
=
"([a-z_]+([A-Z]+[a-z|0-9|_]+)+)\\s+(=|!=|<>|>|>=|<|<=|like)\\s+"
;
StringBuffer
buffer
=
new
StringBuffer
();
String
regex
=
"([a-z_]+([a-z|A-Z|0-9|_]*)+)\\s*(=|!=|<>|>|>=|<|<=|like)\\s*"
;
Matcher
matcher
=
Pattern
.
compile
(
regex
).
matcher
(
condition
.
trim
());
while
(
matcher
.
find
())
{
String
column
=
StringUtil
.
camelhumpToUnderline
(
matcher
.
group
(
1
));
String
fieldName
=
matcher
.
group
(
1
);
String
columnName
=
EntityHelper
.
getColumnName
(
entityClass
,
fieldName
);
String
operator
=
matcher
.
group
(
3
);
matcher
.
appendReplacement
(
buffer
,
column
);
matcher
.
appendReplacement
(
buffer
,
column
Name
);
buffer
.
append
(
" "
).
append
(
operator
).
append
(
" "
);
}
matcher
.
appendTail
(
buffer
);
return
buffer
.
toString
();
}
return
buffer
.
toString
()
;
return
condition
;
}
private
static
String
transferSort
(
String
sort
)
{
StringBuffer
buffer
=
new
StringBuffer
();
private
static
String
transferSort
(
Class
<?>
entityClass
,
String
sort
)
{
if
(
StringUtil
.
isNotEmpty
(
sort
))
{
StringBuffer
buffer
=
new
StringBuffer
();
String
regex
=
"([a-z_]+([A-Z]+[a-z|0-9|_]+)+)"
;
Matcher
matcher
=
Pattern
.
compile
(
regex
).
matcher
(
sort
.
trim
());
while
(
matcher
.
find
())
{
String
column
=
StringUtil
.
camelhumpToUnderline
(
matcher
.
group
(
1
));
matcher
.
appendReplacement
(
buffer
,
column
);
String
fieldName
=
matcher
.
group
(
1
);
String
columnName
=
EntityHelper
.
getColumnName
(
entityClass
,
fieldName
);
matcher
.
appendReplacement
(
buffer
,
columnName
);
}
matcher
.
appendTail
(
buffer
);
return
buffer
.
toString
();
}
return
buffer
.
toString
()
;
return
sort
;
}
}
src/main/java/org/smart4j/framework/orm/EntityHelper.java
浏览文件 @
e15177a7
...
...
@@ -7,6 +7,7 @@ import java.util.Map;
import
org.smart4j.framework.core.ClassHelper
;
import
org.smart4j.framework.orm.annotation.Column
;
import
org.smart4j.framework.orm.annotation.Entity
;
import
org.smart4j.framework.orm.annotation.Table
;
import
org.smart4j.framework.util.ArrayUtil
;
import
org.smart4j.framework.util.MapUtil
;
import
org.smart4j.framework.util.StringUtil
;
...
...
@@ -20,42 +21,73 @@ import org.smart4j.framework.util.StringUtil;
public
class
EntityHelper
{
/**
*
Entity Map(Entity 类 => (列名 => 字段名))
*
实体类 => 表名
*/
private
static
final
Map
<
Class
<?>,
Map
<
String
,
String
>>
entityMap
=
new
HashMap
<
Class
<?>,
Map
<
String
,
String
>>();
private
static
final
Map
<
Class
<?>,
String
>
entityClassTableNameMap
=
new
HashMap
<
Class
<?>,
String
>();
/**
* 实体类 => (字段名 => 列名)
*/
private
static
final
Map
<
Class
<?>,
Map
<
String
,
String
>>
entityClassFieldMapMap
=
new
HashMap
<
Class
<?>,
Map
<
String
,
String
>>();
static
{
// 获取并遍历所有
Entity
类
// 获取并遍历所有
实体
类
List
<
Class
<?>>
entityClassList
=
ClassHelper
.
getClassListByAnnotation
(
Entity
.
class
);
for
(
Class
<?>
entityClass
:
entityClassList
)
{
// 获取并遍历该 Entity 类中所有的字段(不包括父类中的方法)
Field
[]
fields
=
entityClass
.
getDeclaredFields
();
if
(
ArrayUtil
.
isNotEmpty
(
fields
))
{
// 创建一个 Field Map(用于存放列名与字段名的映射关系)
Map
<
String
,
String
>
fieldMap
=
new
HashMap
<
String
,
String
>();
for
(
Field
field
:
fields
)
{
String
fieldName
=
field
.
getName
();
String
columnName
;
// 若该字段上存在 Column 注解,则优先获取注解中的列名
if
(
field
.
isAnnotationPresent
(
Column
.
class
))
{
columnName
=
field
.
getAnnotation
(
Column
.
class
).
value
();
}
else
{
columnName
=
StringUtil
.
camelhumpToUnderline
(
fieldName
);
// 将驼峰风格替换为下划线风格
}
// 若字段名与列名不同,则需要进行映射
if
(!
fieldName
.
equals
(
columnName
))
{
fieldMap
.
put
(
columnName
,
fieldName
);
}
}
// 将 Entity 类与 Field Map 放入 Entity Map 中
if
(
MapUtil
.
isNotEmpty
(
fieldMap
))
{
entityMap
.
put
(
entityClass
,
fieldMap
);
initEntityNameMap
(
entityClass
);
initEntityFieldMapMap
(
entityClass
);
}
}
private
static
void
initEntityNameMap
(
Class
<?>
entityClass
)
{
// 判断该实体类上是否存在 Table 注解
String
tableName
;
if
(
entityClass
.
isAnnotationPresent
(
Table
.
class
))
{
// 若已存在,则使用该注解中定义的表名
tableName
=
entityClass
.
getAnnotation
(
Table
.
class
).
value
();
}
else
{
// 若不存在,则将实体类名转换为下划线风格的表名
tableName
=
StringUtil
.
camelhumpToUnderline
(
entityClass
.
getSimpleName
());
}
entityClassTableNameMap
.
put
(
entityClass
,
tableName
);
}
private
static
void
initEntityFieldMapMap
(
Class
<?>
entityClass
)
{
// 获取并遍历该实体类中所有的字段(不包括父类中的方法)
Field
[]
fields
=
entityClass
.
getDeclaredFields
();
if
(
ArrayUtil
.
isNotEmpty
(
fields
))
{
// 创建一个 fieldMap(用于存放列名与字段名的映射关系)
Map
<
String
,
String
>
fieldMap
=
new
HashMap
<
String
,
String
>();
for
(
Field
field
:
fields
)
{
String
fieldName
=
field
.
getName
();
String
columnName
;
// 判断该字段上是否存在 Column 注解
if
(
field
.
isAnnotationPresent
(
Column
.
class
))
{
// 若已存在,则使用该注解中定义的列名
columnName
=
field
.
getAnnotation
(
Column
.
class
).
value
();
}
else
{
// 若不存在,则将字段名转换为下划线风格的列名
columnName
=
StringUtil
.
camelhumpToUnderline
(
fieldName
);
}
fieldMap
.
put
(
fieldName
,
columnName
);
}
entityClassFieldMapMap
.
put
(
entityClass
,
fieldMap
);
}
}
public
static
Map
<
Class
<?>,
Map
<
String
,
String
>>
getEntityMap
()
{
return
entityMap
;
public
static
String
getTableName
(
Class
<?>
entityClass
)
{
return
entityClassTableNameMap
.
get
(
entityClass
);
}
public
static
Map
<
String
,
String
>
getFieldMap
(
Class
<?>
entityClass
)
{
return
entityClassFieldMapMap
.
get
(
entityClass
);
}
public
static
Map
<
String
,
String
>
getColumnMap
(
Class
<?>
entityClass
)
{
return
MapUtil
.
invert
(
getFieldMap
(
entityClass
));
}
public
static
String
getColumnName
(
Class
<?>
entityClass
,
String
fieldName
)
{
return
getFieldMap
(
entityClass
).
get
(
fieldName
);
}
}
src/main/java/org/smart4j/framework/util/MapUtil.java
浏览文件 @
e15177a7
...
...
@@ -30,9 +30,12 @@ public class MapUtil {
* 转置 Map
*/
public
static
<
K
,
V
>
Map
<
V
,
K
>
invert
(
Map
<
K
,
V
>
source
)
{
Map
<
V
,
K
>
target
=
new
LinkedHashMap
<
V
,
K
>(
source
.
size
());
for
(
Map
.
Entry
<
K
,
V
>
entry
:
source
.
entrySet
())
{
target
.
put
(
entry
.
getValue
(),
entry
.
getKey
());
Map
<
V
,
K
>
target
=
null
;
if
(
isNotEmpty
(
source
))
{
target
=
new
LinkedHashMap
<
V
,
K
>(
source
.
size
());
for
(
Map
.
Entry
<
K
,
V
>
entry
:
source
.
entrySet
())
{
target
.
put
(
entry
.
getValue
(),
entry
.
getKey
());
}
}
return
target
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录