Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
data-house
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
data-house
Commits
89454ce0
提交
89454ce0
authored
10月 21, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
初始提交
上级
97221fc7
显示空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
571 行增加
和
16 行删除
+571
-16
pom.xml
pom.xml
+5
-0
TableInfoController.java
...va/com/tykj/datahouse/controller/TableInfoController.java
+37
-1
ColumnInfo.java
src/main/java/com/tykj/datahouse/entity/ColumnInfo.java
+5
-3
TableInfo.java
src/main/java/com/tykj/datahouse/entity/TableInfo.java
+1
-1
DataSourceManager.java
...in/java/com/tykj/datahouse/service/DataSourceManager.java
+44
-0
TableInfoService.java
...ain/java/com/tykj/datahouse/service/TableInfoService.java
+235
-0
SqlCreator.java
src/main/java/com/tykj/datahouse/sql/SqlCreator.java
+81
-0
MysqlSqlCreator.java
...ain/java/com/tykj/datahouse/sql/impl/MysqlSqlCreator.java
+142
-0
application-mysql.yml
src/main/resources/application-mysql.yml
+21
-11
没有找到文件。
pom.xml
浏览文件 @
89454ce0
...
@@ -50,6 +50,11 @@
...
@@ -50,6 +50,11 @@
<artifactId>
springfox-swagger-ui
</artifactId>
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
2.8.0
</version>
<version>
2.8.0
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
dynamic-datasource-spring-boot-starter
</artifactId>
<version>
3.4.1
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/tykj/datahouse/controller/TableInfoController.java
浏览文件 @
89454ce0
package
com
.
tykj
.
datahouse
.
controller
;
package
com
.
tykj
.
datahouse
.
controller
;
import
com.tykj.datahouse.base.result.ResultObj
;
import
com.tykj.datahouse.base.result.ResultUtil
;
import
com.tykj.datahouse.entity.TableInfo
;
import
com.tykj.datahouse.service.TableInfoService
;
import
com.tykj.datahouse.service.TableInfoService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@Api
(
tags
=
"基础模型接口"
)
@Api
(
tags
=
"基础模型接口"
)
@CrossOrigin
@CrossOrigin
@RequestMapping
(
"/table
info
"
)
@RequestMapping
(
"/table"
)
@RestController
@RestController
public
class
TableInfoController
{
public
class
TableInfoController
{
@Autowired
@Autowired
private
TableInfoService
tableInfoService
;
private
TableInfoService
tableInfoService
;
@PostMapping
@ApiOperation
(
"创建一个新表"
)
public
ResponseEntity
<
ResultObj
<
Object
>>
createTable
(
@RequestBody
TableInfo
tableInfo
)
{
tableInfoService
.
createTable
(
tableInfo
);
return
ResultUtil
.
success
(
"操作成功"
);
}
@PutMapping
@ApiOperation
(
"修改一个表"
)
public
ResponseEntity
<
ResultObj
<
Object
>>
alterTable
(
@RequestBody
TableInfo
tableInfo
)
{
tableInfoService
.
alterTable
(
tableInfo
);
return
ResultUtil
.
success
(
"操作成功"
);
}
@DeleteMapping
(
"/{id}"
)
@ApiOperation
(
"按指定id删除一个表"
)
public
ResponseEntity
<
ResultObj
<
Object
>>
dropTable
(
@PathVariable
Integer
id
)
{
tableInfoService
.
deleteTable
(
id
);
return
ResultUtil
.
success
(
"操作成功"
);
}
@GetMapping
@ApiOperation
(
"查询当前所有表"
)
public
ResponseEntity
<
ResultObj
<
List
<
TableInfo
>>>
findAll
()
{
List
<
TableInfo
>
all
=
tableInfoService
.
findAll
();
return
ResultUtil
.
success
(
all
,
"查询成功"
);
}
}
}
src/main/java/com/tykj/datahouse/entity/ColumnInfo.java
浏览文件 @
89454ce0
...
@@ -7,9 +7,7 @@ import lombok.Data;
...
@@ -7,9 +7,7 @@ import lombok.Data;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.Accessors
;
import
lombok.experimental.Accessors
;
import
javax.persistence.Entity
;
import
javax.persistence.*
;
import
javax.persistence.Id
;
import
javax.persistence.Transient
;
import
java.util.List
;
import
java.util.List
;
@Accessors
(
chain
=
true
)
@Accessors
(
chain
=
true
)
...
@@ -20,6 +18,7 @@ import java.util.List;
...
@@ -20,6 +18,7 @@ import java.util.List;
public
class
ColumnInfo
{
public
class
ColumnInfo
{
@Id
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@ApiModelProperty
(
"主键"
)
@ApiModelProperty
(
"主键"
)
@JsonIgnore
@JsonIgnore
private
Integer
id
;
private
Integer
id
;
...
@@ -41,6 +40,9 @@ public class ColumnInfo {
...
@@ -41,6 +40,9 @@ public class ColumnInfo {
@ApiModelProperty
(
value
=
"长度"
,
position
=
5
)
@ApiModelProperty
(
value
=
"长度"
,
position
=
5
)
private
Integer
length
;
private
Integer
length
;
@ApiModelProperty
(
value
=
"长度"
,
position
=
5
)
private
Boolean
notNull
;
@ApiModelProperty
(
value
=
"描述"
,
position
=
6
)
@ApiModelProperty
(
value
=
"描述"
,
position
=
6
)
private
String
description
;
private
String
description
;
...
...
src/main/java/com/tykj/datahouse/entity/TableInfo.java
浏览文件 @
89454ce0
...
@@ -19,10 +19,10 @@ import java.util.List;
...
@@ -19,10 +19,10 @@ import java.util.List;
public
class
TableInfo
{
public
class
TableInfo
{
@Id
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@ApiModelProperty
(
"主键"
)
@ApiModelProperty
(
"主键"
)
private
Integer
id
;
private
Integer
id
;
@ApiModelProperty
(
value
=
"创建时间"
,
position
=
1
)
@ApiModelProperty
(
value
=
"创建时间"
,
position
=
1
)
@Temporal
(
TemporalType
.
TIMESTAMP
)
@Temporal
(
TemporalType
.
TIMESTAMP
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd hh:mm"
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd hh:mm"
)
...
...
src/main/java/com/tykj/datahouse/service/DataSourceManager.java
0 → 100644
浏览文件 @
89454ce0
package
com
.
tykj
.
datahouse
.
service
;
import
com.baomidou.dynamic.datasource.DynamicRoutingDataSource
;
import
com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator
;
import
com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty
;
import
com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties
;
import
com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
javax.sql.DataSource
;
import
java.util.Map
;
/**
* 数据源管理Service
* 可以动态增加和切换数据源
*/
@Service
public
class
DataSourceManager
{
@Autowired
private
DataSource
dataSource
;
private
BasicDataSourceCreator
dataSourceCreator
=
new
BasicDataSourceCreator
(
new
DynamicDataSourceProperties
());
public
Map
<
String
,
DataSource
>
getDataSources
(){
return
((
DynamicRoutingDataSource
)
dataSource
).
getDataSources
();
}
public
void
addDataSource
(
String
name
)
{
DataSourceProperty
dataSourceProperty
=
new
DataSourceProperty
();
DataSource
basicDataSource
=
dataSourceCreator
.
createDataSource
(
dataSourceProperty
);
((
DynamicRoutingDataSource
)
dataSource
).
addDataSource
(
name
,
basicDataSource
);
}
public
void
switchToDataSource
(
String
dataSourceName
)
{
DynamicDataSourceContextHolder
.
push
(
dataSourceName
);
}
public
void
clear
()
{
DynamicDataSourceContextHolder
.
clear
();
}
}
src/main/java/com/tykj/datahouse/service/TableInfoService.java
浏览文件 @
89454ce0
package
com
.
tykj
.
datahouse
.
service
;
package
com
.
tykj
.
datahouse
.
service
;
import
com.tykj.datahouse.entity.ColumnInfo
;
import
com.tykj.datahouse.entity.TableInfo
;
import
com.tykj.datahouse.repository.ColumnInfoRepository
;
import
com.tykj.datahouse.repository.TableInfoRepository
;
import
com.tykj.datahouse.sql.SqlCreator
;
import
com.tykj.datahouse.sql.impl.MysqlSqlCreator
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
static
java
.
lang
.
String
.
format
;
import
static
java
.
util
.
Objects
.
nonNull
;
/**
* 表信息相关操作的Service
* 涉及到对表的操作 均先进行对实际表的操作(执行sql语句) 再更新表信息
*/
@Service
@Service
public
class
TableInfoService
{
public
class
TableInfoService
{
@Autowired
private
TableInfoRepository
tableInfoRepository
;
@Autowired
private
ColumnInfoRepository
columnInfoRepository
;
@Autowired
private
DataSourceManager
dataSourceManager
;
@Autowired
private
JdbcTemplate
jdbcTemplate
;
private
SqlCreator
sqlCreator
=
new
MysqlSqlCreator
();
/**
* 创建新表
*
* @param tableInfo 要创建的表
*/
public
void
createTable
(
TableInfo
tableInfo
)
{
dataSourceManager
.
clear
();
if
(
nonNull
(
tableInfo
.
getId
()))
{
throw
new
RuntimeException
(
"新增数据不可附带id"
);
}
long
primaryCount
=
tableInfo
.
getColumnInfos
().
stream
()
.
filter
(
ColumnInfo:
:
getIsPrimary
)
.
count
();
if
(
primaryCount
!=
1
)
{
throw
new
RuntimeException
(
"必须有且仅有1个主键"
);
}
dataSourceManager
.
switchToDataSource
(
"target"
);
String
sql
=
sqlCreator
.
createTableSql
(
tableInfo
);
executeSQL
(
sql
);
dataSourceManager
.
clear
();
tableInfo
.
setCreatedTime
(
new
Date
());
tableInfo
.
setUpdatedTime
(
new
Date
());
TableInfo
save
=
tableInfoRepository
.
save
(
tableInfo
);
Integer
id
=
save
.
getId
();
if
(
nonNull
(
tableInfo
.
getColumnInfos
()))
{
List
<
ColumnInfo
>
columnInfosForSave
=
tableInfo
.
getColumnInfos
().
stream
()
.
map
(
columnInfo
->
columnInfo
.
setTableInfoId
(
id
))
.
collect
(
Collectors
.
toList
());
columnInfoRepository
.
saveAll
(
columnInfosForSave
);
}
}
/**
* 总体思路:
* 根据id查到更新之前的原表信息
* 对比现表与原表的字段信息 筛选出新增字段、修改字段、删除字段
* 要新增的字段进行新增、要修改的字段进行修改、要删除的字段进行删除
* 更新完字段信息之后 最后再更新表信息
*/
public
void
alterTable
(
TableInfo
tableInfo
)
{
//根据id查到更新之前的原表信息
dataSourceManager
.
clear
();
TableInfo
oTableInfo
=
tableInfoRepository
.
findById
(
tableInfo
.
getId
())
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
tableInfo
.
getId
())));
//对比现表与原表的字段信息 筛选出新增字段、修改字段、删除字段
List
<
ColumnInfo
>
oColumnInfos
=
oTableInfo
.
getColumnInfos
();
List
<
ColumnInfo
>
columnInfos
=
tableInfo
.
getColumnInfos
();
Set
<
Integer
>
oColumnInfoIds
=
oColumnInfos
.
stream
()
.
map
(
ColumnInfo:
:
getId
)
.
collect
(
Collectors
.
toSet
());
Set
<
Integer
>
columnInfoIds
=
columnInfos
.
stream
()
.
map
(
ColumnInfo:
:
getId
)
.
collect
(
Collectors
.
toSet
());
Map
<
Integer
,
ColumnInfo
>
columnInfoMap
=
columnInfos
.
stream
()
.
collect
(
Collectors
.
toMap
(
ColumnInfo:
:
getId
,
columnInfo
->
columnInfo
));
List
<
ColumnInfo
>
columnsForAdd
=
columnInfos
.
stream
()
.
filter
(
columnInfo
->
isForAdd
(
columnInfo
.
getId
(),
oColumnInfoIds
))
.
collect
(
Collectors
.
toList
());
List
<
ColumnInfo
>
columnsForAlter
=
oColumnInfos
.
stream
()
.
filter
(
oColumnInfo
->
isForAlter
(
oColumnInfo
.
getId
(),
columnInfoIds
))
.
collect
(
Collectors
.
toList
());
List
<
ColumnInfo
>
columnsForDelete
=
oColumnInfos
.
stream
()
.
filter
(
oColumnInfo
->
isForDelete
(
oColumnInfo
.
getId
(),
columnInfoIds
))
.
collect
(
Collectors
.
toList
());
//要新增的字段进行新增、要修改的字段进行修改、要删除的字段进行删除
for
(
ColumnInfo
columnInfo
:
columnsForAdd
)
{
dataSourceManager
.
switchToDataSource
(
"target"
);
String
addColumnSql
=
sqlCreator
.
addColumnSql
(
tableInfo
,
columnInfo
);
jdbcTemplate
.
execute
(
addColumnSql
);
dataSourceManager
.
clear
();
columnInfoRepository
.
save
(
columnInfo
.
setTableInfoId
(
tableInfo
.
getId
()));
}
for
(
ColumnInfo
oColumnInfo
:
columnsForAlter
)
{
dataSourceManager
.
switchToDataSource
(
"target"
);
String
alterColumnSql
=
sqlCreator
.
alterColumnSql
(
tableInfo
,
oColumnInfo
,
columnInfoMap
.
get
(
oColumnInfo
.
getId
()));
jdbcTemplate
.
execute
(
alterColumnSql
);
dataSourceManager
.
clear
();
columnInfoRepository
.
save
(
columnInfoMap
.
get
(
oColumnInfo
.
getId
()));
}
for
(
ColumnInfo
oColumnInfo
:
columnsForDelete
)
{
dataSourceManager
.
switchToDataSource
(
"target"
);
String
removeColumnSql
=
sqlCreator
.
removeColumnSql
(
tableInfo
,
oColumnInfo
);
jdbcTemplate
.
execute
(
removeColumnSql
);
dataSourceManager
.
clear
();
columnInfoRepository
.
delete
(
oColumnInfo
);
}
//更新完字段信息之后 最后再更新表信息
dataSourceManager
.
clear
();
tableInfo
.
setCreatedTime
(
oTableInfo
.
getCreatedTime
());
tableInfo
.
setUpdatedTime
(
new
Date
());
tableInfoRepository
.
save
(
oTableInfo
);
}
/**
* 根据id删除已存在的表
*
* @param id 指定删除的id
*/
public
void
deleteTable
(
Integer
id
)
{
dataSourceManager
.
clear
();
TableInfo
tableInfo
=
tableInfoRepository
.
findById
(
id
)
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
id
)));
dataSourceManager
.
switchToDataSource
(
"target"
);
String
dropTableSql
=
sqlCreator
.
dropTableSql
(
tableInfo
);
executeSQL
(
dropTableSql
);
dataSourceManager
.
clear
();
tableInfoRepository
.
deleteById
(
id
);
columnInfoRepository
.
deleteAllByTableInfoId
(
id
);
}
public
List
<
TableInfo
>
findAll
()
{
return
tableInfoRepository
.
findAll
().
stream
()
.
map
(
this
::
getColumns
)
.
collect
(
Collectors
.
toList
());
}
public
TableInfo
findById
(
Integer
id
)
{
return
tableInfoRepository
.
findById
(
id
)
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
id
)));
}
public
void
deleteAll
()
{
tableInfoRepository
.
findAll
().
stream
()
.
map
(
TableInfo:
:
getId
)
.
forEach
(
this
::
deleteTable
);
columnInfoRepository
.
deleteAll
();
}
/**
* 在指定表中插入一条数据
*
* @param tableId 指定表的id
* @param row Map形式的数据对象
*/
public
void
insertData
(
Integer
tableId
,
Map
<
String
,
Object
>
row
)
{
dataSourceManager
.
clear
();
TableInfo
tableInfo
=
tableInfoRepository
.
findById
(
tableId
)
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
tableId
)));
dataSourceManager
.
switchToDataSource
(
"target"
);
String
updateSql
=
sqlCreator
.
updateSql
(
tableInfo
,
row
);
jdbcTemplate
.
execute
(
updateSql
);
dataSourceManager
.
clear
();
}
/**
* 在指定表中删除一条数据
*
* @param tableId 指定表的id
* @param row Map形式的数据对象
*/
public
void
deleteData
(
Integer
tableId
,
Map
<
String
,
Object
>
row
)
{
dataSourceManager
.
clear
();
TableInfo
tableInfo
=
tableInfoRepository
.
findById
(
tableId
)
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
tableId
)));
dataSourceManager
.
switchToDataSource
(
"target"
);
String
deleteSql
=
sqlCreator
.
deleteSql
(
tableInfo
,
row
);
jdbcTemplate
.
execute
(
deleteSql
);
dataSourceManager
.
clear
();
}
/**
* 在指定表中更新一条数据
*
* @param tableId 指定表的id
* @param row Map形式的数据对象
*/
public
void
updateData
(
Integer
tableId
,
Map
<
String
,
Object
>
row
)
{
dataSourceManager
.
clear
();
TableInfo
tableInfo
=
tableInfoRepository
.
findById
(
tableId
)
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
tableId
)));
dataSourceManager
.
switchToDataSource
(
"target"
);
String
updateSql
=
sqlCreator
.
updateSql
(
tableInfo
,
row
);
jdbcTemplate
.
execute
(
updateSql
);
dataSourceManager
.
clear
();
}
//---------------------------------------------------
private
Boolean
isForAdd
(
Integer
columnInfoId
,
Set
<
Integer
>
oColumnInfoIds
)
{
return
!
oColumnInfoIds
.
contains
(
columnInfoId
);
}
private
Boolean
isForAlter
(
Integer
oColumnInfoId
,
Set
<
Integer
>
columnInfoIds
)
{
return
columnInfoIds
.
contains
(
oColumnInfoId
);
}
private
Boolean
isForDelete
(
Integer
oColumnInfoId
,
Set
<
Integer
>
columnInfoIds
)
{
return
!
columnInfoIds
.
contains
(
oColumnInfoId
);
}
private
TableInfo
getColumns
(
TableInfo
tableInfo
)
{
List
<
ColumnInfo
>
columnInfos
=
columnInfoRepository
.
findByTableInfoId
(
tableInfo
.
getId
());
tableInfo
.
setColumnInfos
(
columnInfos
);
return
tableInfo
;
}
private
void
executeSQL
(
String
sql
)
{
System
.
out
.
println
(
sql
);
jdbcTemplate
.
execute
(
sql
);
}
}
}
src/main/java/com/tykj/datahouse/sql/SqlCreator.java
0 → 100644
浏览文件 @
89454ce0
package
com
.
tykj
.
datahouse
.
sql
;
import
com.tykj.datahouse.entity.ColumnInfo
;
import
com.tykj.datahouse.entity.TableInfo
;
import
java.util.List
;
import
java.util.Map
;
/**
* sql语句生成器
* 根据不同的操作对应不同的API
* 输入表信息或字段信息 返回对应的sql语句结果
*/
public
interface
SqlCreator
{
/**
* 创建新表
* @param tableInfo 要创建的表
* @return 对应的sql语句结果
*/
String
createTableSql
(
TableInfo
tableInfo
);
/**
* 在表中添加字段
* @param tableInfo 被修改的表
* @param columnInfo 要添加的字段
* @return 对应的sql语句结果
*/
String
addColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
columnInfo
);
/**
* 修改表中的字段
* @param tableInfo 被修改的表
* @param oColumnInfo 原字段
* @param columnInfo 现字段
* @return 对应的sql语句结果
*/
String
alterColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
oColumnInfo
,
ColumnInfo
columnInfo
);
/**
*
* @param tableInfo 被修改的表
* @param columnInfo 要删除的字段
* @return 对应的sql语句结果
*/
String
removeColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
columnInfo
);
/**
* 删除表
* @param tableInfo 被删除的表
* @return 对应的sql语句结果
*/
String
dropTableSql
(
TableInfo
tableInfo
);
/**
* 插入一条数据
* @param tableInfo 被插入数据的表
* @param row Map形式的数据对象
* @return 对应的sql语句结果
*/
String
insertSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
);
/**
* 更新一条数据
* @param tableInfo 被更新数据的表
* @param row Map形式的数据对象
* @return 对应的sql语句结果
*/
String
updateSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
);
/**
* 删除一条数据
* @param tableInfo 被删除数据的表
* @param row Map形式的数据对象
* @return 对应的sql语句结果
*/
String
deleteSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
);
String
querySql
(
String
tableName
,
List
<
String
>
columns
);
}
src/main/java/com/tykj/datahouse/sql/impl/MysqlSqlCreator.java
0 → 100644
浏览文件 @
89454ce0
package
com
.
tykj
.
datahouse
.
sql
.
impl
;
import
com.tykj.datahouse.entity.ColumnInfo
;
import
com.tykj.datahouse.entity.TableInfo
;
import
com.tykj.datahouse.sql.SqlCreator
;
import
java.util.List
;
import
java.util.Map
;
import
static
java
.
lang
.
String
.
format
;
import
static
java
.
util
.
Objects
.
nonNull
;
public
class
MysqlSqlCreator
implements
SqlCreator
{
@Override
public
String
createTableSql
(
TableInfo
tableInfo
)
{
List
<
ColumnInfo
>
columnInfos
=
tableInfo
.
getColumnInfos
();
ColumnInfo
primary
=
columnInfos
.
stream
()
.
filter
(
ColumnInfo:
:
getIsPrimary
)
.
findAny
()
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
StringBuilder
result
=
new
StringBuilder
();
//起始
result
.
append
(
format
(
"CREATE TABLE %s(\n"
,
tableInfo
.
getName
()));
//其他字段
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
result
.
append
(
format
(
"%s %s"
,
columnInfo
.
getName
(),
columnInfo
.
getType
()));
if
(
nonNull
(
columnInfo
.
getLength
()))
{
result
.
append
(
format
(
"(%s)"
,
columnInfo
.
getLength
()));
}
if
(
nonNull
(
columnInfo
.
getNotNull
()))
{
result
.
append
(
" not null"
);
}
result
.
append
(
",\n"
);
}
//主键
result
.
append
(
format
(
"PRIMARY KEY (%s)\n"
,
primary
.
getName
()));
//结尾
result
.
append
(
")"
);
return
result
.
toString
();
}
@Override
public
String
addColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
columnInfo
)
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
format
(
"ALTER TABLE %s ADD COLUMN %s %s"
,
tableInfo
.
getName
(),
columnInfo
.
getName
(),
columnInfo
.
getType
()));
if
(
nonNull
(
columnInfo
.
getLength
()))
{
result
.
append
(
format
(
"(%s)"
,
columnInfo
.
getLength
()));
}
if
(
nonNull
(
columnInfo
.
getNotNull
()))
{
result
.
append
(
" not null"
);
}
return
result
.
toString
();
}
@Override
public
String
alterColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
oColumnInfo
,
ColumnInfo
columnInfo
)
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
format
(
"ALTER TABLE %s CHANGE COLUMN %s %s %s"
,
tableInfo
.
getName
(),
oColumnInfo
.
getName
(),
columnInfo
.
getName
(),
columnInfo
.
getType
()));
if
(
nonNull
(
columnInfo
.
getLength
()))
{
result
.
append
(
format
(
"(%s)"
,
columnInfo
.
getLength
()));
}
if
(
nonNull
(
columnInfo
.
getNotNull
()))
{
result
.
append
(
" not null"
);
}
return
result
.
toString
();
}
@Override
public
String
removeColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
columnInfo
)
{
return
format
(
"ALTER TABLE %s DROP COLUMN %s"
,
tableInfo
.
getName
(),
columnInfo
.
getName
());
}
@Override
public
String
dropTableSql
(
TableInfo
tableInfo
)
{
return
format
(
"DROP TABLE %s"
,
tableInfo
.
getName
());
}
@Override
public
String
insertSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
)
{
List
<
ColumnInfo
>
columnInfos
=
tableInfo
.
getColumnInfos
();
StringBuilder
result
=
new
StringBuilder
();
StringBuilder
columns
=
new
StringBuilder
();
StringBuilder
values
=
new
StringBuilder
();
//起始
result
.
append
(
format
(
"INSERT INTO %s "
,
tableInfo
.
getName
()));
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
columns
.
append
(
columnInfo
.
getName
()).
append
(
","
);
values
.
append
(
format
(
"\'%s\',"
,
row
.
get
(
columnInfo
.
getName
())));
}
int
columnLastIndex
=
columns
.
lastIndexOf
(
","
);
if
(
columnLastIndex
>
0
)
{
columns
.
delete
(
columnLastIndex
,
columns
.
length
());
}
int
valueLastIndex
=
values
.
lastIndexOf
(
","
);
if
(
valueLastIndex
>
0
)
{
values
.
delete
(
valueLastIndex
,
values
.
length
());
}
result
.
append
(
format
(
"(%s) VALUES (%s)"
,
columns
.
toString
(),
values
.
toString
()));
return
result
.
toString
();
}
@Override
public
String
updateSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
)
{
List
<
ColumnInfo
>
columnInfos
=
tableInfo
.
getColumnInfos
();
ColumnInfo
primary
=
columnInfos
.
stream
()
.
filter
(
ColumnInfo:
:
getIsPrimary
)
.
findAny
()
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
columnInfos
.
remove
(
primary
);
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
format
(
"UPDATE %s SET "
,
tableInfo
.
getName
()));
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
result
.
append
(
format
(
"%s = \'%s\',"
,
columnInfo
.
getName
(),
row
.
get
(
columnInfo
.
getName
())));
}
int
lastIndex
=
result
.
lastIndexOf
(
","
);
if
(
lastIndex
>
0
)
{
result
.
delete
(
lastIndex
,
result
.
length
());
}
result
.
append
(
format
(
" WHERE (%s = \'%s\')"
,
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
return
result
.
toString
();
}
@Override
public
String
deleteSql
(
TableInfo
tableInfo
,
Map
<
String
,
Object
>
row
)
{
StringBuilder
result
=
new
StringBuilder
();
ColumnInfo
primary
=
tableInfo
.
getColumnInfos
().
stream
()
.
filter
(
ColumnInfo:
:
getIsPrimary
)
.
findAny
()
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
result
.
append
(
format
(
"DELETE FROM %s WHERE (%s = \'%s\')"
,
tableInfo
.
getName
(),
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
return
result
.
toString
();
}
@Override
public
String
querySql
(
String
tableName
,
List
<
String
>
columns
)
{
StringBuilder
result
=
new
StringBuilder
();
return
result
.
toString
();
}
}
src/main/resources/application-mysql.yml
浏览文件 @
89454ce0
...
@@ -3,21 +3,31 @@ spring:
...
@@ -3,21 +3,31 @@ spring:
date-format
:
yyyy-MM-dd HH:mm:ss
date-format
:
yyyy-MM-dd HH:mm:ss
time-zone
:
GMT+8
time-zone
:
GMT+8
datasource
:
datasource
:
dynamic
:
primary
:
master
#设置默认的数据源或者数据源组,默认值即为master
strict
:
false
#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource
:
master
:
url
:
jdbc:mysql://192.168.100.248:3306/datahouse?useSSL=true&verifyServerCertificate=false&useUnicode=true&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
username
:
root
password
:
root
driver-class-name
:
com.mysql.cj.jdbc.Driver
# 3.2.0开始支持SPI可省略此配置
# hikari:
# read-only: false
# connection-timeout: 60000
# idle-timeout: 60000
# validation-timeout: 3000
# max-lifetime: 60000
# login-timeout: 5
# maximum-pool-size: 60
# minimum-idle: 10
target
:
url
:
jdbc:mysql://192.168.100.248:3306/datahouse_data?useSSL=true&verifyServerCertificate=false&useUnicode=true&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
username
:
root
username
:
root
password
:
root
password
:
root
url
:
jdbc:mysql://192.168.100.248:3306/datahouse?useSSL=true&verifyServerCertificate=false&useUnicode=true&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
driver-class-name
:
com.mysql.cj.jdbc.Driver
driver-class-name
:
com.mysql.cj.jdbc.Driver
hikari
:
read-only
:
false
connection-timeout
:
60000
idle-timeout
:
60000
validation-timeout
:
3000
max-lifetime
:
60000
login-timeout
:
5
maximum-pool-size
:
60
minimum-idle
:
10
jpa
:
jpa
:
show-sql
:
tru
e
show-sql
:
fals
e
hibernate
:
hibernate
:
naming
:
naming
:
physical-strategy
:
com.tykj.datahouse.base.config.ToUpperCase
physical-strategy
:
com.tykj.datahouse.base.config.ToUpperCase
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论