Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
dataDeclaration
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
dataDeclaration
Commits
561d0a6c
提交
561d0a6c
authored
5月 24, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[模型模块]修改表语法改动
上级
43b8cd86
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
50 行增加
和
33 行删除
+50
-33
ModelImpl.java
...ain/java/com/tykj/model_layer/service/impl/ModelImpl.java
+24
-28
SqlUtil.java
src/main/java/com/tykj/model_layer/utils/SqlUtil.java
+26
-5
没有找到文件。
src/main/java/com/tykj/model_layer/service/impl/ModelImpl.java
浏览文件 @
561d0a6c
package
com
.
tykj
.
model_layer
.
service
.
impl
;
package
com
.
tykj
.
model_layer
.
service
.
impl
;
import
ch.qos.logback.core.joran.util.beans.BeanUtil
;
import
com.github.wenhao.jpa.PredicateBuilder
;
import
com.github.wenhao.jpa.PredicateBuilder
;
import
com.github.wenhao.jpa.Specifications
;
import
com.github.wenhao.jpa.Specifications
;
...
@@ -15,19 +14,14 @@ import com.tykj.model_layer.entity.customEnums.ConnectionType;
...
@@ -15,19 +14,14 @@ import com.tykj.model_layer.entity.customEnums.ConnectionType;
import
com.tykj.model_layer.entity.vo.*
;
import
com.tykj.model_layer.entity.vo.*
;
import
com.tykj.model_layer.myEnum.ModelType
;
import
com.tykj.model_layer.myEnum.ModelType
;
import
com.tykj.model_layer.service.ModelService
;
import
com.tykj.model_layer.service.ModelService
;
import
com.tykj.model_layer.utils.ObjectUtil
;
import
com.tykj.model_layer.utils.SessionUtil
;
import
com.tykj.model_layer.utils.SessionUtil
;
import
com.tykj.model_layer.utils.SqlUtil
;
import
com.tykj.model_layer.utils.SqlUtil
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.logging.log4j.util.Strings
;
import
org.apache.logging.log4j.util.Strings
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.hibernate.Session
;
import
org.hibernate.StaleStateException
;
import
org.hibernate.StaleStateException
;
import
org.hibernate.dialect.lock.OptimisticEntityLockException
;
import
org.hibernate.internal.SessionImpl
;
import
org.hibernate.internal.SessionImpl
;
import
org.hibernate.persister.entity.EntityPersister
;
import
org.hibernate.persister.entity.EntityPersister
;
import
org.hibernate.query.Query
;
import
org.hibernate.query.Query
;
...
@@ -35,8 +29,8 @@ import org.hibernate.type.IntegerType;
...
@@ -35,8 +29,8 @@ import org.hibernate.type.IntegerType;
import
org.hibernate.type.StringType
;
import
org.hibernate.type.StringType
;
import
org.hibernate.type.TimestampType
;
import
org.hibernate.type.TimestampType
;
import
org.hibernate.type.Type
;
import
org.hibernate.type.Type
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.env.Environment
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.jpa.domain.Specification
;
import
org.springframework.data.jpa.domain.Specification
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.JdbcTemplate
;
...
@@ -45,7 +39,6 @@ import org.springframework.stereotype.Service;
...
@@ -45,7 +39,6 @@ import org.springframework.stereotype.Service;
import
javax.persistence.criteria.Path
;
import
javax.persistence.criteria.Path
;
import
javax.persistence.criteria.Predicate
;
import
javax.persistence.criteria.Predicate
;
import
java.lang.reflect.Field
;
import
java.text.ParseException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.*
;
...
@@ -87,6 +80,9 @@ public class ModelImpl implements ModelService {
...
@@ -87,6 +80,9 @@ public class ModelImpl implements ModelService {
@Autowired
@Autowired
private
SessionUtil
sessionUtil
;
private
SessionUtil
sessionUtil
;
@Autowired
private
Environment
env
;
/**
/**
* @param
* @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
...
@@ -205,7 +201,7 @@ public class ModelImpl implements ModelService {
...
@@ -205,7 +201,7 @@ public class ModelImpl implements ModelService {
* @Date 16:17 2021/3/5
* @Date 16:17 2021/3/5
**/
**/
@Override
@Override
public
int
putValueByEntityName
(
Map
<
String
,
Object
>
map
,
Boolean
isDelete
)
{
public
int
putValueByEntityName
(
Map
<
String
,
Object
>
map
,
Boolean
isDelete
)
{
for
(
String
tableName
:
for
(
String
tableName
:
map
.
keySet
())
{
map
.
keySet
())
{
//查找对应的表
//查找对应的表
...
@@ -225,23 +221,23 @@ public class ModelImpl implements ModelService {
...
@@ -225,23 +221,23 @@ public class ModelImpl implements ModelService {
Object
values
=
map
.
get
(
tableName
);
Object
values
=
map
.
get
(
tableName
);
if
(
values
instanceof
Map
)
{
if
(
values
instanceof
Map
)
{
//插入数据
//插入数据
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
values
,
session
,
true
,
false
,
isDelete
);
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
values
,
session
,
true
,
false
,
isDelete
);
}
else
{
}
else
{
//循环插入数据
//循环插入数据
List
valuesList
=
(
List
)
values
;
List
valuesList
=
(
List
)
values
;
for
(
int
i
=
0
;
i
<
valuesList
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
valuesList
.
size
();
i
++)
{
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
valuesList
.
get
(
i
),
session
,
true
,
false
,
isDelete
);
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
valuesList
.
get
(
i
),
session
,
true
,
false
,
isDelete
);
}
}
}
}
try
{
try
{
session
.
getTransaction
().
commit
();
session
.
getTransaction
().
commit
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
//todo 等会捕捉一下
//todo 等会捕捉一下
if
(
e
.
getMessage
().
contains
(
"Batch update returned unexpected row count from"
)){
if
(
e
.
getMessage
().
contains
(
"Batch update returned unexpected row count from"
))
{
throw
new
ApiException
(
"要操作对象的主键不存在"
);
throw
new
ApiException
(
"要操作对象的主键不存在"
);
}
}
}
finally
{
}
finally
{
session
.
close
();
session
.
close
();
}
}
...
@@ -258,7 +254,7 @@ public class ModelImpl implements ModelService {
...
@@ -258,7 +254,7 @@ public class ModelImpl implements ModelService {
* @Date 16:17 2021/3/5
* @Date 16:17 2021/3/5
**/
**/
@Override
@Override
public
int
putValueByEntityNameList
(
List
<
Map
<
String
,
Object
>>
mapList
,
Boolean
isDelete
)
{
public
int
putValueByEntityNameList
(
List
<
Map
<
String
,
Object
>>
mapList
,
Boolean
isDelete
)
{
SessionImpl
session
=
(
SessionImpl
)
sessionUtil
.
getSession
();
SessionImpl
session
=
(
SessionImpl
)
sessionUtil
.
getSession
();
session
.
getTransaction
().
begin
();
session
.
getTransaction
().
begin
();
for
(
Map
<
String
,
Object
>
map
:
mapList
)
{
for
(
Map
<
String
,
Object
>
map
:
mapList
)
{
...
@@ -279,12 +275,12 @@ public class ModelImpl implements ModelService {
...
@@ -279,12 +275,12 @@ public class ModelImpl implements ModelService {
Object
values
=
map
.
get
(
tableName
);
Object
values
=
map
.
get
(
tableName
);
if
(
values
instanceof
Map
)
{
if
(
values
instanceof
Map
)
{
//插入数据
//插入数据
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
values
,
session
,
true
,
false
,
isDelete
);
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
values
,
session
,
true
,
false
,
isDelete
);
}
else
{
}
else
{
//循环插入数据
//循环插入数据
List
valuesList
=
(
List
)
values
;
List
valuesList
=
(
List
)
values
;
for
(
int
i
=
0
;
i
<
valuesList
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
valuesList
.
size
();
i
++)
{
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
valuesList
.
get
(
i
),
session
,
true
,
false
,
isDelete
);
insertValue
(
tableInfo
.
getModelName
(),
(
Map
)
valuesList
.
get
(
i
),
session
,
true
,
false
,
isDelete
);
}
}
}
}
...
@@ -292,11 +288,11 @@ public class ModelImpl implements ModelService {
...
@@ -292,11 +288,11 @@ public class ModelImpl implements ModelService {
}
}
try
{
try
{
session
.
getTransaction
().
commit
();
session
.
getTransaction
().
commit
();
}
catch
(
StaleStateException
staleStateException
)
{
}
catch
(
StaleStateException
staleStateException
)
{
if
(
staleStateException
.
getMessage
().
contains
(
"Batch update returned unexpected row count from"
)){
if
(
staleStateException
.
getMessage
().
contains
(
"Batch update returned unexpected row count from"
))
{
throw
new
ApiException
(
"要操作对象的主键不存在"
);
throw
new
ApiException
(
"要操作对象的主键不存在"
);
}
}
}
finally
{
}
finally
{
session
.
close
();
session
.
close
();
}
}
return
0
;
return
0
;
...
@@ -311,12 +307,12 @@ public class ModelImpl implements ModelService {
...
@@ -311,12 +307,12 @@ public class ModelImpl implements ModelService {
* @Description 新增参数的方法
* @Description 新增参数的方法
* @Date 16:17 2021/3/5
* @Date 16:17 2021/3/5
**/
**/
public
void
insertValue
(
String
tableName
,
Map
map
,
SessionImpl
session
,
boolean
back
,
boolean
saveOrSaveAndUpdate
,
boolean
isDelete
)
{
public
void
insertValue
(
String
tableName
,
Map
map
,
SessionImpl
session
,
boolean
back
,
boolean
saveOrSaveAndUpdate
,
boolean
isDelete
)
{
EntityPersister
entityPersister
=
session
.
getEntityPersister
(
tableName
,
map
);
EntityPersister
entityPersister
=
session
.
getEntityPersister
(
tableName
,
map
);
//主键类型推断
//主键类型推断
String
identifierPropertyName
=
entityPersister
.
getIdentifierPropertyName
();
String
identifierPropertyName
=
entityPersister
.
getIdentifierPropertyName
();
if
(
map
.
containsKey
(
identifierPropertyName
)){
if
(
map
.
containsKey
(
identifierPropertyName
))
{
changeValueToTargetType
(
map
,
map
.
get
(
identifierPropertyName
),
entityPersister
.
getIdentifierType
(),
identifierPropertyName
);
changeValueToTargetType
(
map
,
map
.
get
(
identifierPropertyName
),
entityPersister
.
getIdentifierType
(),
identifierPropertyName
);
}
}
//类型推断 根据目标值的类型强转目标值的目标类型
//类型推断 根据目标值的类型强转目标值的目标类型
Type
[]
propertyTypes
=
entityPersister
.
getPropertyTypes
();
Type
[]
propertyTypes
=
entityPersister
.
getPropertyTypes
();
...
@@ -326,12 +322,12 @@ public class ModelImpl implements ModelService {
...
@@ -326,12 +322,12 @@ public class ModelImpl implements ModelService {
Object
value
=
propertyValuesToInsert
[
i
];
Object
value
=
propertyValuesToInsert
[
i
];
Type
propertyType
=
propertyTypes
[
i
];
Type
propertyType
=
propertyTypes
[
i
];
//根据目标Type转换
//根据目标Type转换
changeValueToTargetType
(
map
,
value
,
propertyType
,
propertyNames
[
i
]);
changeValueToTargetType
(
map
,
value
,
propertyType
,
propertyNames
[
i
]);
}
}
HashMap
hashMap
=
new
HashMap
();
HashMap
hashMap
=
new
HashMap
();
hashMap
.
putAll
(
map
);
hashMap
.
putAll
(
map
);
//到这里直接执行删除操作
//到这里直接执行删除操作
if
(
isDelete
){
if
(
isDelete
)
{
session
.
delete
(
tableName
,
map
);
session
.
delete
(
tableName
,
map
);
return
;
return
;
}
}
...
@@ -570,6 +566,7 @@ public class ModelImpl implements ModelService {
...
@@ -570,6 +566,7 @@ public class ModelImpl implements ModelService {
* 根据ColumnInfo的id来匹配字段变化
* 根据ColumnInfo的id来匹配字段变化
*/
*/
private
List
<
String
>
getTableSqls
(
String
table
,
List
<
ColumnInfo
>
origin
,
List
<
ColumnInfo
>
current
)
{
private
List
<
String
>
getTableSqls
(
String
table
,
List
<
ColumnInfo
>
origin
,
List
<
ColumnInfo
>
current
)
{
String
driverType
=
env
.
getProperty
(
"spring.datasource.driver-class-name"
);
List
<
String
>
result
=
new
ArrayList
<>();
List
<
String
>
result
=
new
ArrayList
<>();
for
(
ColumnInfo
columnInfo
:
current
)
{
for
(
ColumnInfo
columnInfo
:
current
)
{
ColumnInfo
originColumnInfo
=
origin
.
stream
()
ColumnInfo
originColumnInfo
=
origin
.
stream
()
...
@@ -580,7 +577,7 @@ public class ModelImpl implements ModelService {
...
@@ -580,7 +577,7 @@ public class ModelImpl implements ModelService {
String
sql
=
SqlUtil
.
addColumn
(
table
,
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
());
String
sql
=
SqlUtil
.
addColumn
(
table
,
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
());
result
.
add
(
sql
);
result
.
add
(
sql
);
}
else
{
}
else
{
String
sql
=
SqlUtil
.
updateColumn
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
());
String
sql
=
SqlUtil
.
updateColumn
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
()
,
driverType
);
result
.
add
(
sql
);
result
.
add
(
sql
);
}
}
}
}
...
@@ -689,7 +686,7 @@ public class ModelImpl implements ModelService {
...
@@ -689,7 +686,7 @@ public class ModelImpl implements ModelService {
}
}
void
changeValueToTargetType
(
Map
map
,
Object
value
,
Type
propertyType
,
String
propertyName
)
{
void
changeValueToTargetType
(
Map
map
,
Object
value
,
Type
propertyType
,
String
propertyName
)
{
if
(
propertyType
instanceof
TimestampType
)
{
if
(
propertyType
instanceof
TimestampType
)
{
try
{
try
{
if
(
value
instanceof
String
)
{
if
(
value
instanceof
String
)
{
...
@@ -714,5 +711,4 @@ public class ModelImpl implements ModelService {
...
@@ -714,5 +711,4 @@ public class ModelImpl implements ModelService {
}
}
}
}
src/main/java/com/tykj/model_layer/utils/SqlUtil.java
浏览文件 @
561d0a6c
...
@@ -3,9 +3,12 @@ package com.tykj.model_layer.utils;
...
@@ -3,9 +3,12 @@ package com.tykj.model_layer.utils;
import
com.google.common.base.Strings
;
import
com.google.common.base.Strings
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.env.Environment
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Objects
;
import
static
java
.
util
.
Objects
.
isNull
;
import
static
java
.
util
.
Objects
.
isNull
;
import
static
java
.
util
.
Objects
.
nonNull
;
import
static
java
.
util
.
Objects
.
nonNull
;
...
@@ -16,6 +19,9 @@ import static java.util.Objects.nonNull;
...
@@ -16,6 +19,9 @@ import static java.util.Objects.nonNull;
@Slf4j
@Slf4j
public
class
SqlUtil
{
public
class
SqlUtil
{
@Autowired
private
Environment
env
;
/**
/**
* java类型对应sql类型Map
* java类型对应sql类型Map
*/
*/
...
@@ -96,7 +102,7 @@ public class SqlUtil {
...
@@ -96,7 +102,7 @@ public class SqlUtil {
* @param newLength 字段长度
* @param newLength 字段长度
* @return SQL语句
* @return SQL语句
*/
*/
public
static
String
updateColumn
(
String
table
,
String
name
,
String
newName
,
String
newType
,
Integer
newLength
)
{
public
static
String
updateColumn
(
String
table
,
String
name
,
String
newName
,
String
newType
,
Integer
newLength
,
String
driverType
)
{
newType
=
TYPE_MAP
.
get
(
newType
);
newType
=
TYPE_MAP
.
get
(
newType
);
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
&&
nonNull
(
newType
);
&&
nonNull
(
newType
);
...
@@ -104,11 +110,26 @@ public class SqlUtil {
...
@@ -104,11 +110,26 @@ public class SqlUtil {
if
(
hasValue
)
{
if
(
hasValue
)
{
if
(
isNull
(
newName
))
{
if
(
isNull
(
newName
))
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s MODIFY %s %s"
,
table
,
name
,
newType
));
result
.
append
(
String
.
format
(
"ALTER TABLE %s MODIFY %s %s"
,
table
,
name
,
newType
));
switch
(
driverType
){
case
"com.mysql.cj.jdbc.Driver"
:
result
.
append
(
String
.
format
(
"ALTER TABLE %s MODIFY %s %s"
,
table
,
name
,
newType
));
break
;
case
"org.h2.Driver"
:
case
"com.oscar.Driver"
:
result
.
append
(
String
.
format
(
"ALTER TABLE %s ALTER COLUMN %s %s"
,
table
,
name
,
newType
));
}
if
(
nonNull
(
newLength
))
{
result
.
append
(
String
.
format
(
"(%s)"
,
newLength
));
}
}
else
{
}
else
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s CHANGE COLUMN %s %s %s"
,
table
,
name
,
newName
,
newType
));
switch
(
driverType
){
}
case
"com.mysql.cj.jdbc.Driver"
:
if
(
nonNull
(
newLength
))
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s CHANGE COLUMN %s %s %s"
,
table
,
name
,
newName
,
newType
));
result
.
append
(
String
.
format
(
"(%s)"
,
newLength
));
break
;
case
"org.h2.Driver"
:
case
"com.oscar.Driver"
:
result
.
append
(
String
.
format
(
"ALTER TABLE %s RENAME COLUMN %s TO %s"
,
table
,
name
,
newName
));
}
}
}
}
else
{
}
else
{
log
.
error
(
"参数缺失"
);
log
.
error
(
"参数缺失"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论