1.MyBatis通过接口调用自定义创建数据表
实例代码:
<update id="createCustomTable" parameterType=
"com.rabbitmq.mq.entity.CreateTableAndTabbleName"> CREATE TABLE IF NOT EXISTS
${tableName}( `id` int NOT NULL AUTO_INCREMENT COMMENT '编号', <foreach collection
="customTables" item="item" separator=""> <if test="item.choose == true"> ${item
.createTableFiledName} ${item.fieldType} <choose> <when test="item.isNotNull !=
null and item.isNotNull !=''"> NOT NULL </when> </choose> DEFAULT <choose> <when
test="item.defaultValue != null and item.defaultValue !=''"> ${item.
defaultValue}</when> <otherwise> NULL </otherwise> </choose> COMMENT <choose> <
when test="item.comment != null and item.comment !=''"> '${item.comment}' </when
> <otherwise> '${item.createTableFiledName}' </otherwise> </choose> , </if> </
foreach> PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; </update>
2.接口调用参数实例:
URL:localhost:8080/createCustomTable requestMethods: Post body: { "tableName":
"custom_table_1", "customTables":[{ "createTableFiledName":"creater",
"fieldType":"varchar(255)", "comment":"创建者", "choose":true },{
"createTableFiledName":"create_time", "fieldType":"timestamp", "defaultValue":
"CURRENT_TIMESTAMP", "isNotNull":true, "comment":"创建时间", "choose":true },{
"createTableFiledName":"updater", "fieldType":"varchar(255)", "comment":"更新者",
"choose":true },{ "createTableFiledName":"update_time", "fieldType":"timestamp",
"defaultValue":"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", "isNotNull":true
, "comment":"更新时间", "choose":true },{ "createTableFiledName":"is_delete",
"fieldType":"tinyint(1)", "defaultValue":0, "comment":"是否删除", "choose":true },{
"createTableFiledName":"ip", "fieldType":"varchar(64)", "comment":"IP地址",
"choose":false },{ "createTableFiledName":"operation", "fieldType":"varchar(50)"
, "comment":"用户操作", "choose":false }] }
3.实体类对于参数说明
CreateTableAndTabbleName 和 CustomTable
`1` public class CreateTableAndTabbleName { private String tableName; private
List<CustomTable> customTables; public String getTableName() { return tableName;
} public void setTableName(String tableName) { this.tableName = tableName; }
public List<CustomTable> getCustomTables() { return customTables; } public void
setCustomTables(List<CustomTable> customTables) { this.customTables =
customTables; } } `2` public class CustomTable { /*外部数据字段*/ private String
externalDataField; /*是否为空*/ private boolean isNotNull; /*默认值*/ private String
defaultValue; /*评论*/ private String comment; /*建表数据字段*/ private String
createTableFiledName; /*字段类型*/ private String fieldType; /*字段长度*/ private int
lengthLimit; /*选择,choose如果为true说明是可添加*/ private boolean choose; public String
getExternalDataField() { return externalDataField; } public void
setExternalDataField(String externalDataField) { this.externalDataField =
externalDataField; } public boolean isNotNull() { return isNotNull; } public
void setNotNull(boolean notNull) { isNotNull = notNull; } public String
getDefaultValue() { return defaultValue; } public void setDefaultValue(String
defaultValue) { this.defaultValue = defaultValue; } public String getComment() {
return comment; } public void setComment(String comment) { this.comment =
comment; } public String getCreateTableFiledName() { return createTableFiledName
; } public void setCreateTableFiledName(String createTableFiledName) { this.
createTableFiledName= createTableFiledName; } public String getFieldType() {
return fieldType; } public void setFieldType(String fieldType) { this.fieldType
= fieldType; } public int getLengthLimit() { return lengthLimit; } public void
setLengthLimit(int lengthLimit) { this.lengthLimit = lengthLimit; } public
boolean isChoose() { return choose; } public void setChoose(boolean choose) {
this.choose = choose; } }
//可添加 外键等操作
//添加外键 alter table 【tableName】 add constraint 【FK】 foreign key(【filed】)
REFERENCES 【REFERENCEStable】(【REFERENCESfield】)
//查看表结构desc custom_table_1;
//查看所有表 show tables;

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信