天天看點

SpringBoot使用MybatisPlus自動建表

導入依賴

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
            <artifactId>mybatis-enhance-actable</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>
           

application.yml

spring:
 datasource:
  url: jdbc:mysql://127.0.0.1:3306/db01?serverTimezone=UTC&characterEncoding=utf8
  driver-class-name: com.mysql.jdbc.Driver
  username: root
  password: root

mybatis:
 table:
  auto: update
  #create系統啟動後,會将所有的表删除掉,然後根據model中配置的結構重建立表,該操作會破壞原有資料。
  #update系統會自動判斷哪些表是建立的.哪些字段要修改類型等,哪些字段要删除,哪些字段要新增,該操作不會破壞原有資料。#none系統不做任何處理。
  #add新增表/新增字段/新增索引新增唯一限制的功能,不做做修改和删除(隻在版本1.0.9.RELEASE及以上支援)。model:
 model:
  pack: com.qs.entity #掃描用于建立表的對象的包名,多個包用"."隔開
 database:
  type: mysql #資料庫類型目前隻支援mysql
mybatis-plus:
 #1.如果是mybatis直接在mybatis下增加該配置。
 #2.如果使用properties配置方式,要寫成mapperLocations
 mapper-locations: classpath*:mapper/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml #第一個是自己寫xml的路徑,第二個是固定的

           

啟動類上添加注解

@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})//固定的
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")//固定的
@SpringBootApplication
public class MybatistestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatistestApplication.class, args);
    }

}
           

公共父類

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsAutoIncrement;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsKey;

/**
 * @author qingshi
 * @date 2023/1/6 11:21
 * info:
 */
public class BaseModle {
    @TableId(type = IdType.AUTO)
    @IsKey
    @IsAutoIncrement
    @Column
    private Integer id;

    @Column(name = "create_time",comment = "建立時間")
    private String createTime;

    @Column(name = "updaet_time",comment = "修改時間")
    private String updaetTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getUpdaetTime() {
        return updaetTime;
    }

    public void setUpdaetTime(String updaetTime) {
        this.updaetTime = updaetTime;
    }
}

           

實體類

import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;

/**
 * @author qingshi
 * @date 2023/1/6 11:24
 * info:
 */
@Table(name = "people")
public class people extends BaseModle{
    @Column(comment = "姓名")
    private String name;

    @Column
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
           

運作後自動建表(更新表)

SpringBoot使用MybatisPlus自動建表