spring boot环境下 Mybaits的自动创建表

spring boot环境下 Mybaits的自动创建表

一、这是什么

与Hibernate相比,Mybatis没有自动创建表的功能,当我们的数据库表格比较多的时候。修改的时候将会变得极为麻烦。
而在spring boot环境下,可以使用Mybatis的ACtable框架实现表格的自动化创建,修改

二、使用说明

Mybatis配置
环境:spring boot+maven
添加依赖:

<dependency>
  <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
    <artifactId>mybatis-enhance-actable</artifactId>
  <version>1.0.3</version>
</dependency>

添加application.properties配置文件:

mybatis.table.auto=update
mybatis.model.pack=com.example.entity
mybatis.database.type=mysql

mybatis.table.auto这个配置用于配置每次系统启动时对表格操作的形式

当mybatis.table.auto=create时,系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。

当mybatis.table.auto=update时,系统会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。

当mybatis.table.auto=none时,系统不做任何处理。

mybatis.model.pack这个配置是用来配置要扫描的用于创建表的对象的包名

mybatis.database.type 数据库类型

三、怎么用

在指定包(路径为上文中的mybatis.model.pack)中创建用于建表的实体类


@Table(name = "user")
public class User {

    public interface userSimpleView extends ResultDto.fullResult{};

    @Column(name = "user_id" ,type = MySqlTypeConstant.VARCHAR , length = 24,isKey = true)
    @Id
    private String userId;

    @Column(name = "user_name", type = MySqlTypeConstant.VARCHAR , length = 24, isNull = false)
    private String userName;

    @Column(name = "user_password", type = MySqlTypeConstant.VARCHAR , length = 100, isNull = false)
    private String userPassword;

    @Column(name = "user_email", type = MySqlTypeConstant.VARCHAR , length = 50, isNull = false)
    private String userEmail;

    @Column(name = "gender",type = MySqlTypeConstant.VARCHAR,length = 1,isNull = false,isKey = false)
    private String gender;
   /**忽略getter和setter**/
  
}

注解详解:
@Table :name属性用于标注创建表的表名

@Colum:属性如下图

在这里插入图片描述

name:表格列名

type:所在列的数据类型,可选类型如下
在这里插入图片描述

length:字符串长度,默认255

decimalLength:小数点长度,默认为0

isNull:是否可以为空,默认为true

isKey:是否为主键,默认为false

AtuoIncrement:是否自增,默认为false

defaultValue:设置默认值,默认为null

isUnique:判断该列的每一条记录是否唯一,默认没flase

@Id:标明主键属性

以上配置完成后,在运行spring boot时,会自动构建“user”表

在这里插入图片描述
配置文件中mybatis.table.auto为create或者update时,修改实体类后再运行spring boot会对表格进行修改。
注:actable暂不支持双主键,会报错

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值