- 引言
- 1.引入组件
- 2. 配置组件
- 2.1 配置mybatisplus
- 2.2 配置启动属性
- 启动类型追加包扫描
- 3.模型加表注解
- 4.启动完成
在一些小型项目或者新建项目中,可以通过代码注解配置,在项目启动时自动生成或更新表结构。现已经已有开源组件实现了这个目标。ACTable:官网地址。
好处:对于经常修改表,创建表的场景,可以不用双向更新表结构和原型;
缺点:代码入侵,需要像配置swagger文档注释一样编写注释(这是必然的,组件需要获取表字段信息)
下面介绍如何引入到SpringBoot+MybatisPlus项目中。
maven配置:
com.gitee.sunchenbin.mybatis.actable
mybatis-enhance-actable
1.5.0.RELEASE
com.baomidou
mybatis-plus-annotation
2. 配置组件
2.1 配置mybatisplus
(ps.如果是mybatis则按照mybatis配置)
在mybatis-plus的mapper路径配置上追加actable的配置:classpath*:com/gitee/sunchenbin/mybatis/actable/mapping//.xml
示例:
#配置mapper xml文件的路径 mybatis-plus.mapper-locations=classpath:com/atguigu/educenter/mapper/xml*.xml2.2 配置启动属性
示例:
actable.table.auto=update #(ps:要扫描的model目录) actable.model.pack=com.atguigu.educenter.entity actable.database.type=mysql启动类型追加包扫描
MapperScan:com.gitee.sunchenbin.mybatis.actable.dao.*
ComponentScan:com.gitee.sunchenbin
示例:
@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu","com.gitee.sunchenbin"})
@MapperScan({"com.atguigu.educenter.mapper","com.gitee.sunchenbin.mybatis.actable.dao.*"})
public class UcenterApplication {
public static void main(String[] args) {
SpringApplication.run(UcenterApplication.class, args);
}
}
3.模型加表注解
示例:
@Data
@TableName(value = "ucenter_member")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Column(isNull = false, length = 32, comment = "会员id")
@TableId(value = "id", type = IdType.ID_WORKER_STR)
private String id;
@TableField
@ColumnComment("微信openid")
private String openid;
@Column(length = 12, comment = "手机号")
private String mobile;
@Column(length = 32,comment = "密码")
private String password;
}
4.启动完成



