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;
    }
}

运行后自动建表(更新表)
在这里插入图片描述

你可能感兴趣的:(spring,boot,mybatis,java)