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 个修改的文件
包含
339 行增加
和
19 行删除
+339
-19
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
+0
-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
+24
-14
没有找到文件。
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
差异被折叠。
点击展开。
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
:
username
:
root
dynamic
:
password
:
root
primary
:
master
#设置默认的数据源或者数据源组,默认值即为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
strict
:
false
#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
driver-class-name
:
com.mysql.cj.jdbc.Driver
datasource
:
hikari
:
master
:
read-only
:
false
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
connection-timeout
:
60000
username
:
root
idle-timeout
:
60000
password
:
root
validation-timeout
:
3000
driver-class-name
:
com.mysql.cj.jdbc.Driver
# 3.2.0开始支持SPI可省略此配置
max-lifetime
:
60000
# hikari:
login-timeout
:
5
# read-only: false
maximum-pool-size
:
60
# connection-timeout: 60000
minimum-idle
:
10
# 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
password
:
root
driver-class-name
:
com.mysql.cj.jdbc.Driver
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论