springboot整合ACTable生成數(shù)據(jù)庫(kù)表方式
springboot整合ACTable生成數(shù)據(jù)庫(kù)表
1. 痛點(diǎn)
我們?cè)谑褂肕ybatis或Mybatis-Plus時(shí),可以用其自帶的generator插件根據(jù)已經(jīng)存在的數(shù)據(jù)庫(kù)表生成代碼(包括實(shí)體類(lèi)),但是卻不能通過(guò)實(shí)體類(lèi)來(lái)創(chuàng)建、更改表。
如果你使用的是JPA,那不必?zé)?,JPA有此功能。使用Mybatis系列的可以考慮使用ACTable。
2. ACTable介紹
官網(wǎng)復(fù)制的介紹:A.C.Table是對(duì)Mybatis做的增強(qiáng)功能,支持SpringBoot以及傳統(tǒng)的SpringMvc項(xiàng)目結(jié)構(gòu),簡(jiǎn)單配置即可,該框架是為了能夠使習(xí)慣了hibernate框架的開(kāi)發(fā)者能夠快速的入手Mybatis, “A.C.Table” 本意是自動(dòng)建表的意思,A.C.Table是一個(gè)基于Spring和Mybatis的Maven項(xiàng)目,增強(qiáng)了Mybatis的功能,過(guò)配置model注解的方式來(lái)創(chuàng)建表,修改表結(jié)構(gòu),并且實(shí)現(xiàn)了共通的CUDR功能提升開(kāi)發(fā)效率,同時(shí)能夠兼容tk.mybatis和mybatis-plus,如需使用依賴(lài)相關(guān)的pom依賴(lài)即可,目前僅支持Mysql,后續(xù)會(huì)擴(kuò)展針對(duì)其他數(shù)據(jù)庫(kù)的支持。
3. 使用方式
先引入依賴(lài)(已經(jīng)有了mysql支持和Mybatis),如果沒(méi)有mysql和Mybatis請(qǐng)自行引入。
<!-- 生成表依賴(lài)--> <dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.5.0.RELEASE</version> </dependency>
配置文件中進(jìn)行配置
#配置模板 # actable的配置信息 actable.table.auto=update actable.model.pack=com.yours.model actable.database.type=mysql actable.index.prefix=自己定義的索引前綴#該配置項(xiàng)不設(shè)置默認(rèn)使用actable_idx_ actable.unique.prefix=自己定義的唯一約束前綴#該配置項(xiàng)不設(shè)置默認(rèn)使用actable_uni_ # mybatis自有的配置信息,key也可能是:mybatis.mapperLocations mybatis.mapper-locations=classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
我的配置:
#自動(dòng)建表模式,選擇更新模式 mybatis.table.auto=update #要掃描的實(shí)體類(lèi)路徑 mybatis.model.pack=com.study.vue.entity #數(shù)據(jù)庫(kù)類(lèi)型,目前只支持mysql mybatis.database.type=mysql #要用到的xml路徑,固定的 mybatis.mapper-locations=classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
配置信息解釋?zhuān)?/p>
springboot2.0+啟動(dòng)類(lèi)需要做如下配置(必備)
- @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)建完成后進(jìn)入數(shù)據(jù)庫(kù)查看:
@Table("t_user") //創(chuàng)建表時(shí)的表名,不指定名稱(chēng)時(shí)默認(rèn)為類(lèi)名 public class User { //字段注解,不指定名稱(chēng)時(shí)默認(rèn)為字段名,會(huì)將駝峰字段用_分割 @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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成方法
- SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)表的方法
- Springboot如何根據(jù)實(shí)體類(lèi)生成數(shù)據(jù)庫(kù)表
- springBoot下實(shí)現(xiàn)java自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表
- springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法
相關(guān)文章
Java ConcurrentHashMap鎖分段機(jī)制使用及代碼實(shí)例
ConcurrentHashMap是Java中的一種線(xiàn)程安全的哈希表,通過(guò)鎖分段機(jī)制提高了并發(fā)性能,在Java 8中,ConcurrentHashMap引入了CAS操作和更復(fù)雜的節(jié)點(diǎn)繼承結(jié)構(gòu),進(jìn)一步優(yōu)化了并發(fā)操作2025-01-01SpringBoot登錄攔截配置詳解(實(shí)測(cè)可用)
這篇文章主要介紹了SpringBoot登錄攔截配置詳解(實(shí)測(cè)可用),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07mybatis 攔截器添加參數(shù)的實(shí)現(xiàn)
本文主要介紹了MyBatis攔截器中添加參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12