mybatis-plus根据entity自动创建表(使用A.CTable)

1、pom中依赖mybatis-enhance-actable-1.1.1.RELEASE.jar

  
      com.gitee.sunchenbin.mybatis.actable
      mybatis-enhance-actable
      1.1.1.RELEASE
  

2、在application.yml中配置


mybatis:
  table:
    auto: update
		 #create	系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
		 #update	系统会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。
		 #none 		系统不做任何处理。
		 #add		新增表/新增字段/新增索引/新增唯一约束的功能,不做做修改和删除 (只在版本1.0.9.RELEASE及以上支持)。
  model:
    pack: com.yuru.entity #扫描用于创建表的对象的包名,多个包用“,”隔开
  database:
    type: mysql #数据库类型 目前只支持mysql
    
mybatis-plus:
  #1.如果是mybatis 直接在mybatis下增加该配置。
  #2.如果使用properties配置方式,要写成mapperLocations
  mapper-locations: classpath*:xxxxxx/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml

3、在Application启动类中加上(直接复制不要进行更改)

@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"} )
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*") 

4、实体类中的配置

entity基础类

@Getter //lombok注解,不用管
@Setter //lombok注解,不用管
public class SuperEntity {
    /**
     * 主键
     */
    @TableId(type = IdType.AUTO) //mybatis-plus主键注解 
    @IsKey 						 //actable主键注解
    @IsAutoIncrement			 //自增
    @Column 					 //对应数据库字段,不配置name会直接采用属性名作为字段名
    private Long id;
    /**
     * 创建时间
     */
    @Column(name = "create_time",comment = "创建时间") // name指定数据库字段名,comment为备注
    private Date createTime;
    /**
     * 最后修改时间
     */
    @Column(name = "update_time",comment = "最后修改时间")
    private Date updateTime;
}

普通entity类,继承SuperEntity

@Data //lombok注解,不用管
@Table(name = "t_user") //对应数据库表名,如果更改表名需要同步更改数据库表名,不然会重新创建表。
public class User extends SuperEntity {
    @Column
    private String username;
    @Column
    private String password;
}

注:官方说明只支持一层父类属性继承,我试了一下可以多重继承,可能是后期版本优化过了。

5、启动项目查看数据库是否创建成功

mybatis-plus根据entity自动创建表(使用A.CTable)_第1张图片

你可能感兴趣的:(mybatis)