springboot整合ACTable生成數(shù)據(jù)庫表方式
springboot整合ACTable生成數(shù)據(jù)庫表
1. 痛點
我們在使用Mybatis或Mybatis-Plus時,可以用其自帶的generator插件根據(jù)已經(jīng)存在的數(shù)據(jù)庫表生成代碼(包括實體類),但是卻不能通過實體類來創(chuàng)建、更改表。
如果你使用的是JPA,那不必?zé)?,JPA有此功能。使用Mybatis系列的可以考慮使用ACTable。
2. ACTable介紹
官網(wǎng)復(fù)制的介紹:A.C.Table是對Mybatis做的增強功能,支持SpringBoot以及傳統(tǒng)的SpringMvc項目結(jié)構(gòu),簡單配置即可,該框架是為了能夠使習(xí)慣了hibernate框架的開發(fā)者能夠快速的入手Mybatis, “A.C.Table” 本意是自動建表的意思,A.C.Table是一個基于Spring和Mybatis的Maven項目,增強了Mybatis的功能,過配置model注解的方式來創(chuàng)建表,修改表結(jié)構(gòu),并且實現(xiàn)了共通的CUDR功能提升開發(fā)效率,同時能夠兼容tk.mybatis和mybatis-plus,如需使用依賴相關(guān)的pom依賴即可,目前僅支持Mysql,后續(xù)會擴展針對其他數(shù)據(jù)庫的支持。
3. 使用方式
先引入依賴(已經(jīng)有了mysql支持和Mybatis),如果沒有mysql和Mybatis請自行引入。
<!-- 生成表依賴-->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>配置文件中進行配置
#配置模板 # actable的配置信息 actable.table.auto=update actable.model.pack=com.yours.model actable.database.type=mysql actable.index.prefix=自己定義的索引前綴#該配置項不設(shè)置默認使用actable_idx_ actable.unique.prefix=自己定義的唯一約束前綴#該配置項不設(shè)置默認使用actable_uni_ # mybatis自有的配置信息,key也可能是:mybatis.mapperLocations mybatis.mapper-locations=classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml

我的配置:
#自動建表模式,選擇更新模式 mybatis.table.auto=update #要掃描的實體類路徑 mybatis.model.pack=com.study.vue.entity #數(shù)據(jù)庫類型,目前只支持mysql mybatis.database.type=mysql #要用到的xml路徑,固定的 mybatis.mapper-locations=classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
配置信息解釋:

springboot2.0+啟動類需要做如下配置(必備)
- @ComponentScan配置,路徑"com.gitee.sunchenbin.mybatis.actable.manager.*"
- @MapperScan配置,路徑"com.gitee.sunchenbin.mybatis.actable.dao.*"

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

創(chuàng)建完成后進入數(shù)據(jù)庫查看:


@Table("t_user") //創(chuàng)建表時的表名,不指定名稱時默認為類名
public class User {
//字段注解,不指定名稱時默認為字段名,會將駝峰字段用_分割
@Column(name = "id",type = MySqlTypeConstant.INT,isKey = true,isAutoIncrement = true)
private Integer id;
@Column(name = "name",type = MySqlTypeConstant.VARCHAR)
private String name;
@Column(name = "phone",type = MySqlTypeConstant.VARCHAR)
private String phone;
@Column(name = "create_time",type = MySqlTypeConstant.DATE)
private Date createTime;
@Column(name = "del",type = MySqlTypeConstant.BIT,defaultValue = "0")
private Boolean del;
}@Table
public class Dept {
@Column(type = MySqlTypeConstant.INT)
@IsKey //主鍵,相當(dāng)于isKey = true
@IsAutoIncrement //自增,相當(dāng)于isAutoIncrement = true
private Integer id;
@Column
private Integer pid;
@Column
private String name;
@Column
private LocalDateTime createTime;
@Column
private Boolean del;
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java ConcurrentHashMap鎖分段機制使用及代碼實例
ConcurrentHashMap是Java中的一種線程安全的哈希表,通過鎖分段機制提高了并發(fā)性能,在Java 8中,ConcurrentHashMap引入了CAS操作和更復(fù)雜的節(jié)點繼承結(jié)構(gòu),進一步優(yōu)化了并發(fā)操作2025-01-01

