MyBatis动态自定义创建表

在这里插入图片描述

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;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值