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
7758a4eb
Commit
7758a4eb
authored
Jun 18, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
常规BUG的修改
parent
1f2ce7b6
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
186 additions
and
150 deletions
+186
-150
BaseDaoImpl.java
...b/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
+5
-0
BaseDaoSql.java
...db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
+2
-2
SqlData.java
...il-db/src/main/java/com/yanzuoguang/dao/impl/SqlData.java
+5
-5
SqlDataField.java
.../src/main/java/com/yanzuoguang/dao/impl/SqlDataField.java
+6
-2
TableFieldVo.java
.../src/main/java/com/yanzuoguang/dao/impl/TableFieldVo.java
+15
-1
TableSqlCache.java
...src/main/java/com/yanzuoguang/dao/impl/TableSqlCache.java
+12
-12
TableStruct.java
...b/src/main/java/com/yanzuoguang/dao/impl/TableStruct.java
+141
-128
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoImpl.java
View file @
7758a4eb
...
...
@@ -13,6 +13,11 @@ import java.util.*;
/**
* 数据库操作的基本工具类
* 1. 实现了基本的增删该查
* 2. 实现了统计的增肌和修改
* 3. 实现了一定功能的基本验证,验证数据是否存在
* 4. 数据主键的获取等功能。
* 5. 获取自增等功能
*
* @author 颜佐光
*/
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/BaseDaoSql.java
View file @
7758a4eb
...
...
@@ -19,7 +19,7 @@ import java.util.regex.Matcher;
import
java.util.regex.Pattern
;
/**
* SQL语句基本操作类
* SQL语句基本操作类
,包括SQL语句的解析,参数的组装,查询缓存的处理
*
* @author 颜佐光
*/
...
...
@@ -131,7 +131,7 @@ public abstract class BaseDaoSql {
if
(
this
.
table
==
null
)
{
throw
new
CodeException
(
"类"
+
this
.
getClass
().
getName
()
+
"未发现表结构"
);
}
SqlData
sql
=
this
.
table
.
get
Sqls
().
get
(
name
);
SqlData
sql
=
this
.
table
.
get
NameCache
().
get
(
name
);
if
(
isThrow
&&
sql
==
null
)
{
throw
new
CodeException
(
"类"
+
this
.
getClass
().
getName
()
+
"未发现SQL语句"
+
name
);
}
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/SqlData.java
View file @
7758a4eb
...
...
@@ -16,11 +16,6 @@ public class SqlData {
*/
private
int
sqlType
=
DaoConst
.
SQL_TYPE_COMMON
;
/**
* 对应的参数顺序
*/
private
List
<
SqlDataField
>
sqlDataFields
=
new
ArrayList
<
SqlDataField
>();
/**
* SQL语句的名称
*/
...
...
@@ -31,6 +26,11 @@ public class SqlData {
*/
private
String
sql
;
/**
* 对应的参数顺序
*/
private
List
<
SqlDataField
>
sqlDataFields
=
new
ArrayList
<
SqlDataField
>();
/**
* 构造函数
*/
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/SqlDataField.java
View file @
7758a4eb
package
com
.
yanzuoguang
.
dao
.
impl
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -12,9 +14,11 @@ public class SqlDataField {
/**
* 前台参数名称,当没有前台参数时,则把当前字段当常量代码片段处理
*/
public
String
paraName
=
""
;
public
String
paraName
=
StringHelper
.
EMPTY
;
/**
* 是否根据前台参数自动添加,false时保持永久添加模式
* 是否根据前台参数自动添加,false时保持永久添加模式,
* 1. true为条件模式,即前台不输入参数时,则不增加相关代码片段
* 2. false为敞亮模式,不管前台是否输入,该参数和片段必然会增加到后台执行语句中
*/
public
boolean
auto
=
true
;
/**
...
...
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/TableFieldVo.java
View file @
7758a4eb
...
...
@@ -28,14 +28,28 @@ public class TableFieldVo {
this
.
type
=
type
;
}
/**
* 表字段的原名称
*/
public
String
name
;
/**
* 表子弹的名称小写
*/
public
String
lName
;
/**
* 前台输入参数的名称
*/
public
String
inputName
;
/**
* 前台输入的参数的小写
*/
public
String
inputLName
;
/**
* 实体的参数的类型
*/
public
Class
<?>
type
=
String
.
class
;
;
}
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/TableSqlCache.java
View file @
7758a4eb
...
...
@@ -25,19 +25,19 @@ public class TableSqlCache {
public
static
final
String
PAGE_SIZE_TAG
=
"_PageSize"
;
/**
* 表信息
* 表
结构
信息
*/
private
TableStruct
table
;
/**
* 缓存的SQL信息
* 缓存的SQL信息
,按照名称进行缓存
*/
private
MemoryCache
<
SqlData
>
sqls
=
new
MemoryCache
<
SqlData
>();
private
MemoryCache
<
SqlData
>
nameCache
=
new
MemoryCache
<
SqlData
>();
/**
* 根据Sql语句类型进行缓存
* 根据Sql语句类型进行缓存
,按照类型进行缓存
*/
private
MemoryCache
<
List
<
SqlData
>>
sqlTypes
=
new
MemoryCache
<>();
private
MemoryCache
<
List
<
SqlData
>>
typeCache
=
new
MemoryCache
<>();
/**
* 构造函数
...
...
@@ -64,9 +64,9 @@ public class TableSqlCache {
*/
public
SqlData
add
(
SqlData
sql
)
{
if
(
sql
!=
null
)
{
this
.
sqls
.
put
(
sql
.
getName
(),
sql
);
this
.
nameCache
.
put
(
sql
.
getName
(),
sql
);
String
keyType
=
String
.
valueOf
(
sql
.
getSqlType
());
this
.
sqlTypes
.
get
(
keyType
,
new
ArrayList
<>()).
add
(
sql
);
this
.
typeCache
.
get
(
keyType
,
new
ArrayList
<>()).
add
(
sql
);
}
return
sql
;
}
...
...
@@ -80,7 +80,7 @@ public class TableSqlCache {
*/
public
List
<
SqlData
>
getSqlType
(
int
sqlType
)
{
String
keyType
=
String
.
valueOf
(
sqlType
);
return
sqlTypes
.
get
(
keyType
);
return
typeCache
.
get
(
keyType
);
}
/**
...
...
@@ -312,11 +312,11 @@ public class TableSqlCache {
this
.
table
=
table
;
}
public
MemoryCache
<
SqlData
>
get
Sqls
()
{
return
sqls
;
public
MemoryCache
<
SqlData
>
get
NameCache
()
{
return
nameCache
;
}
public
void
set
Sqls
(
MemoryCache
<
SqlData
>
sqls
)
{
this
.
sqls
=
sqls
;
public
void
set
NameCache
(
MemoryCache
<
SqlData
>
nameCache
)
{
this
.
nameCache
=
nameCache
;
}
}
yzg-util-db/src/main/java/com/yanzuoguang/dao/impl/TableStruct.java
View file @
7758a4eb
...
...
@@ -6,7 +6,6 @@ import com.yanzuoguang.util.helper.StringHelper;
import
com.yanzuoguang.util.base.MethodField
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
...
...
@@ -20,14 +19,143 @@ import java.util.Map;
public
class
TableStruct
{
/**
* 表名称
*
数据库中的
表名称
*/
private
String
name
;
/**
* 缓存的字段
* 缓存的字段
,根据字段的类型进行缓存,同一个字段可能会属于多个类型。
*/
private
Map
<
Integer
,
List
<
TableFieldVo
>>
mapFields
=
new
HashMap
<>();
private
Map
<
Integer
,
List
<
TableFieldVo
>>
typeFieldCache
=
new
HashMap
<>();
/**
* 构造函数
*/
public
TableStruct
()
{
this
.
name
=
""
;
}
/**
* 通过实体的字段来创建表结构信息
*
* @param name 表名称
* @param cls 关联的实体,主键放在第一位,其他字段放到后面;需要注意的是必需和表结构对应起来,会有隐性BUG,比如说在实体中增加了字段,会导致增加修改失败
*/
public
TableStruct
(
String
name
,
Class
<?>
cls
)
{
this
.
name
=
name
;
// 获取实体中的所有字段信息,包含get、set、field
HashMap
<
String
,
MethodField
>
fields
=
ObjectHelper
.
getTypeField
(
cls
);
// 遍历字段
for
(
Map
.
Entry
<
String
,
MethodField
>
entry
:
fields
.
entrySet
())
{
// 字段信息获取
MethodField
field
=
entry
.
getValue
();
if
(
field
.
getField
()
==
null
&&
field
.
getGetMethod
()
==
null
)
{
continue
;
}
addMethodField
(
field
);
}
}
/**
* 添加字段
*
* @param field 添加字段
*/
private
void
addMethodField
(
MethodField
field
)
{
int
fieldAction
=
DaoConst
.
FIELD_COMMON
;
// 默认后台数据库字段和前台参数字段为字段名,字段类型为class
String
fieldName
=
field
.
getName
();
String
fieldInputName
=
field
.
getName
();
Class
<?>
fieldType
=
String
.
class
;
// 获取注解以及返回类型
TableAnnotation
annotation
=
null
;
if
(
field
.
getField
()
!=
null
)
{
annotation
=
field
.
getField
().
getAnnotation
(
TableAnnotation
.
class
);
fieldType
=
field
.
getField
().
getType
();
}
else
if
(
field
.
getGetMethod
()
!=
null
)
{
annotation
=
field
.
getGetMethod
().
getAnnotation
(
TableAnnotation
.
class
);
fieldType
=
field
.
getGetMethod
().
getReturnType
();
}
// 注解不为空,则修改后台数据库映射字段、字段类型
if
(
annotation
!=
null
)
{
fieldName
=
annotation
.
value
();
fieldAction
=
annotation
.
type
();
}
// 将字段组合成输入字段
TableFieldVo
vo
=
new
TableFieldVo
(
fieldName
,
fieldInputName
,
fieldType
);
// 根据字段名称规则来获取名称默认类型
int
stringAction
=
getStringAction
(
vo
);
// 获取普通类型字段列表
List
<
TableFieldVo
>
commonActionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_COMMON
);
// 判断是否属于主键
if
(
fieldAction
==
DaoConst
.
FIELD_PRIMARY
||
stringAction
==
DaoConst
.
FIELD_PRIMARY
)
{
// 将历史主键添加到普通列,并且移除历史主键
List
<
TableFieldVo
>
primaryActionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_PRIMARY
);
commonActionList
.
addAll
(
primaryActionList
);
primaryActionList
.
clear
();
// 将现有列添加到主键
primaryActionList
.
add
(
vo
);
}
else
{
// 将所有非主键列添加到普通列
commonActionList
.
add
(
vo
);
boolean
isTypeMany
=
fieldAction
==
DaoConst
.
FIELD_REMOVE_UPDATE
||
stringAction
==
DaoConst
.
FIELD_REMOVE_UPDATE
||
fieldAction
==
DaoConst
.
FIELD_CREATE
||
stringAction
==
DaoConst
.
FIELD_CREATE
;
if
(
isTypeMany
)
{
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_REMOVE_UPDATE
);
actionList
.
add
(
vo
);
}
// 处理其他特殊列
else
if
(
fieldAction
!=
DaoConst
.
FIELD_COMMON
)
{
// 假如特殊列已经存在,则将已经存在的特殊列删除,并且添加新的特殊列
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
fieldAction
);
actionList
.
clear
();
actionList
.
add
(
vo
);
}
else
if
(
stringAction
!=
DaoConst
.
FIELD_COMMON
)
{
// 假如是默认的,并且特殊列已经存在,则不进行任何处理
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
stringAction
);
if
(
actionList
.
isEmpty
())
{
actionList
.
add
(
vo
);
}
}
}
}
/**
* 获取字符串动作类型
*
* @param vo 输入子弹
* @return
*/
private
int
getStringAction
(
TableFieldVo
vo
)
{
if
(
getKey
()
==
null
)
{
return
DaoConst
.
FIELD_PRIMARY
;
}
else
if
(
DaoConst
.
REMOVE_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_REMOVE
;
}
else
if
(
DaoConst
.
VERSION_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_VERSION
;
}
// 判断是否属于统计字段
else
if
(
DaoConst
.
MD5_KEY_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_MD5
;
}
else
if
(
vo
.
inputLName
.
startsWith
(
DaoConst
.
UPDATE_FLAG
))
{
return
DaoConst
.
FIELD_REMOVE_UPDATE
;
}
else
if
(
vo
.
inputLName
.
startsWith
(
DaoConst
.
CREATE_FLAG
))
{
return
DaoConst
.
FIELD_CREATE
;
}
else
{
return
DaoConst
.
FIELD_COMMON
;
}
}
/**
* 获取某个类型的所有字段
...
...
@@ -36,10 +164,10 @@ public class TableStruct {
* @return
*/
private
List
<
TableFieldVo
>
getFieldActionList
(
int
type
)
{
if
(!
mapFields
.
containsKey
(
type
))
{
mapFields
.
put
(
type
,
new
ArrayList
<>());
if
(!
typeFieldCache
.
containsKey
(
type
))
{
typeFieldCache
.
put
(
type
,
new
ArrayList
<>());
}
return
mapFields
.
get
(
type
);
return
typeFieldCache
.
get
(
type
);
}
/**
...
...
@@ -154,125 +282,6 @@ public class TableStruct {
return
getFieldActionList
(
DaoConst
.
FIELD_REMOVE_UPDATE
);
}
/**
* 构造函数
*/
public
TableStruct
()
{
this
.
name
=
""
;
}
/**
* 通过实体的字段来创建表结构信息
*
* @param name 表名称
* @param cls 关联的实体,主键放在第一位,其他字段放到后面;需要注意的是必需和表结构对应起来,会有隐性BUG,比如说在实体中增加了字段,会导致增加修改失败
*/
public
TableStruct
(
String
name
,
Class
<?>
cls
)
{
this
.
name
=
name
;
// 获取实体中的所有字段信息,包含get、set、field
HashMap
<
String
,
MethodField
>
fields
=
ObjectHelper
.
getTypeField
(
cls
);
// 遍历字段
for
(
Map
.
Entry
<
String
,
MethodField
>
entry
:
fields
.
entrySet
())
{
// 字段信息获取
MethodField
field
=
entry
.
getValue
();
if
(
field
.
getField
()
==
null
&&
field
.
getGetMethod
()
==
null
)
{
continue
;
}
addMethodField
(
field
);
}
}
/**
* 添加字段
*
* @param field 添加字段
*/
private
void
addMethodField
(
MethodField
field
)
{
int
fieldAction
=
DaoConst
.
FIELD_COMMON
;
String
fieldName
=
field
.
getName
();
String
fieldInputName
=
field
.
getName
();
Class
<?>
fieldType
=
String
.
class
;
TableAnnotation
annotation
=
null
;
if
(
field
.
getField
()
!=
null
)
{
annotation
=
field
.
getField
().
getAnnotation
(
TableAnnotation
.
class
);
fieldType
=
field
.
getField
().
getType
();
}
else
if
(
field
.
getGetMethod
()
!=
null
)
{
annotation
=
field
.
getGetMethod
().
getAnnotation
(
TableAnnotation
.
class
);
fieldType
=
field
.
getGetMethod
().
getReturnType
();
}
if
(
annotation
!=
null
)
{
fieldName
=
annotation
.
value
();
fieldAction
=
annotation
.
type
();
}
TableFieldVo
vo
=
new
TableFieldVo
(
fieldName
,
fieldInputName
,
fieldType
);
int
stringAction
=
getStringAction
(
vo
);
// 判断是否属于主键
List
<
TableFieldVo
>
commonActionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_COMMON
);
if
(
fieldAction
==
DaoConst
.
FIELD_PRIMARY
||
stringAction
==
DaoConst
.
FIELD_PRIMARY
)
{
// 移除历史主键,并且将历史主键添加到普通列
List
<
TableFieldVo
>
primaryActionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_PRIMARY
);
commonActionList
.
addAll
(
primaryActionList
);
primaryActionList
.
clear
();
// 将现有列添加到主键
primaryActionList
.
add
(
vo
);
}
else
{
// 将所有非主键列添加到普通列
commonActionList
.
add
(
vo
);
if
(
fieldAction
==
DaoConst
.
FIELD_REMOVE_UPDATE
||
stringAction
==
DaoConst
.
FIELD_REMOVE_UPDATE
)
{
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
DaoConst
.
FIELD_REMOVE_UPDATE
);
actionList
.
add
(
vo
);
}
// 处理其他特殊列
else
if
(
fieldAction
!=
DaoConst
.
FIELD_COMMON
)
{
// 假如特殊列已经存在,则将已经存在的特殊列删除,并且添加新的特殊列
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
fieldAction
);
actionList
.
clear
();
actionList
.
add
(
vo
);
}
else
if
(
stringAction
!=
DaoConst
.
FIELD_COMMON
)
{
// 假如是默认的,并且特殊列已经存在,则不进行任何处理
List
<
TableFieldVo
>
actionList
=
this
.
getFieldActionList
(
stringAction
);
if
(
actionList
.
isEmpty
())
{
actionList
.
add
(
vo
);
}
}
}
}
/**
* 获取字符串动作类型
*
* @param vo 输入子弹
* @return
*/
private
int
getStringAction
(
TableFieldVo
vo
)
{
if
(
getKey
()
==
null
)
{
return
DaoConst
.
FIELD_PRIMARY
;
}
else
if
(
DaoConst
.
REMOVE_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_REMOVE
;
}
else
if
(
DaoConst
.
VERSION_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_VERSION
;
}
// 判断是否属于统计字段
else
if
(
DaoConst
.
MD5_KEY_FLAG
.
equals
(
vo
.
inputLName
))
{
return
DaoConst
.
FIELD_MD5
;
}
else
if
(
vo
.
inputLName
.
startsWith
(
DaoConst
.
UPDATE_FLAG
))
{
return
DaoConst
.
FIELD_REMOVE_UPDATE
;
}
else
if
(
vo
.
inputLName
.
startsWith
(
DaoConst
.
CREATE_FLAG
))
{
return
DaoConst
.
FIELD_CREATE
;
}
else
{
return
DaoConst
.
FIELD_COMMON
;
}
}
/**
* 获取某一列
*
...
...
@@ -335,28 +344,32 @@ public class TableStruct {
String
text
=
DaoConst
.
UPDATE_MODEL
.
replace
(
DaoConst
.
TABLE_CODE
,
this
.
name
);
SqlData
sql
=
new
SqlData
(
DaoConst
.
UPDATE
,
text
);
sql
.
setSqlType
(
DaoConst
.
SQL_TYPE_UPDATE
);
// 特殊字段操作
TableFieldVo
removeField
=
this
.
getRemove
();
TableFieldVo
versionField
=
this
.
getVersion
();
sql
.
addParaConst
(
this
.
getKey
().
inputName
,
DaoConst
.
FIELD_CODE
,
"a."
+
this
.
getKey
().
name
+
"=a."
+
this
.
getKey
().
name
,
DaoConst
.
WHERE_CODE
,
" AND a."
+
this
.
getKey
().
name
+
"=?"
);
// 删除时更新字段
List
<
TableFieldVo
>
createFields
=
getFieldActionList
(
DaoConst
.
FIELD_CREATE
);
Map
<
TableFieldVo
,
Boolean
>
mapCreate
=
new
HashMap
<>(
createFields
.
size
());
for
(
TableFieldVo
field
:
createFields
)
{
mapCreate
.
put
(
field
,
true
);
}
// 增加普通代码片段字段
for
(
TableFieldVo
field
:
this
.
getFields
())
{
if
(
field
==
removeField
||
field
==
versionField
||
mapCreate
.
containsKey
(
field
))
{
continue
;
}
sql
.
addParaConst
(
field
.
inputName
,
DaoConst
.
FIELD_CODE
,
",a."
+
field
.
name
+
"=?"
);
}
// 添加删除字段
if
(
removeField
!=
null
)
{
sql
.
addParaConst
(
removeField
.
inputName
,
DaoConst
.
WHERE_CODE
,
" AND a."
+
removeField
.
name
+
"=0"
);
}
// 添加版本字段
if
(
versionField
!=
null
)
{
sql
.
addParaConst
(
versionField
.
inputName
,
DaoConst
.
FIELD_CODE
,
",a."
+
versionField
.
name
+
"=1+a."
+
versionField
.
name
,
...
...
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