springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表的示例
好長(zhǎng)時(shí)間沒輸出了,最近工作上也是太多事,領(lǐng)導(dǎo)動(dòng)不動(dòng)就拍腦門,那叫一個(gè)酸爽~
工作能力的提現(xiàn)不但是技術(shù)或解決問題的能力上,還體現(xiàn)在要能立刻滿足領(lǐng)導(dǎo)的各種需求,不管是哪方面的需求,這樣才能夠拍上馬屁,步步高升。
言歸正傳,作為技術(shù)從業(yè)者,還是要多深耕技術(shù)。有小伙伴問,在springboot工程中,持久層采用的mybatis框架,如何能夠自動(dòng)建表,一個(gè)團(tuán)隊(duì)中各個(gè)小伙伴針對(duì)新增需求會(huì)添加或修改表,但各自調(diào)試時(shí),數(shù)據(jù)庫(kù)表更新又不及時(shí),造成很大不便。下面記錄一下springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表。
1、環(huán)境
- springboot2.x
- mybatis-plus3.5.0
- mybatis-enhance-actable1.1.1.RELEASE
- mysql5.7.x
- idea開發(fā)工具
2、新建springboot工程
2.1、pom依賴如下
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.1.1.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.2、application配置
server: port: 9001 spring: #數(shù)據(jù)庫(kù)配置 datasource: url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root #自動(dòng)建表設(shè)置 mybatis: table: #create系統(tǒng)啟動(dòng)后,會(huì)將所有的表刪除掉,然后根據(jù)model中配置的結(jié)構(gòu)重新建表,該操作會(huì)破壞原有數(shù)據(jù); #update系統(tǒng)會(huì)自動(dòng)判斷哪些表是新建的.哪些字段要修改類型等,哪些字段要?jiǎng)h除,哪些字段要新增,該操作不會(huì)破壞原有數(shù)據(jù); #add新增表/新增字段/新增索引新增唯一約束的功能,不做做修改和刪除(只在版本1.0.9.RELEASE及以上支持); #none系統(tǒng)不做任何處理; auto: update model: #掃描用于創(chuàng)建表的對(duì)象的包名 pack: com.*.*.model database: #數(shù)據(jù)庫(kù)類型目前只支持mysql type: mysql #mybatis-plus mybatis-plus: #固定的 mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
注意,mybatis-plus是固定的,采用的是mybatis-enhance-actable插件。
3、集成mybatis-plus
啟動(dòng)類配置如下
@SpringBootApplication @MapperScan("com.*.*.mapper") @ComponentScan("com.*.*.*") @MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})//固定的 @ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")//固定的 public class SpringbootMybatisPlus2Application { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisPlus2Application.class, args); } }
注意,固定的是采用的是mybatis-enhance-actable插件。
實(shí)體類配置如下
@Data @Table(name = "t_test") @TableName(value = "t_test") public class Test { @TableId(value = "id",type = IdType.AUTO) @IsKey @IsAutoIncrement @Column(name = "id",comment = "主鍵") private Long id; @TableField(value = "name") @Column(name = "",comment = "名稱",isNull = false) private String name; @TableField(value = "create_time") @Column(name = "create_time",comment = "創(chuàng)建時(shí)間") private String creatTime; @Column(name = "update_time",comment = "修改時(shí)間") private String updateTime; }
注解分別為mybatisplus提供的、mybatis-enhance-actable提供的,前者的注解是用來進(jìn)行持久層操作的(增刪改查),后者的注解是用來進(jìn)行自動(dòng)建表的。
4、業(yè)務(wù)操作
mapper類如下
@Mapper public interface TestMapper extends BaseMapper<Test> { }
service類如下
public interface TestService extends IService<Test> {}
@Service public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService { }
controller類如下
@RestController @RequiredArgsConstructor public class TestController { private final TestServiceImpl testService; @GetMapping("/hello") public Object hello() { return "hello"; } @GetMapping("/save") public Object save() { Test test = new Test(); test.setName("caocao"); testService.save(test); return test; } @GetMapping("/list") public Object list() { List<Test> list = testService.list(); return list; } }
5、測(cè)試結(jié)果
啟動(dòng)服務(wù),結(jié)果類似如下
訪問結(jié)果如下
到此這篇關(guān)于springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表的示例的文章就介紹到這了,更多相關(guān)springboot mybatisplus自動(dòng)建表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot同時(shí)集成Mybatis和Mybatis-plus框架
- SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁示例
- Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁(使用步驟)
- SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁的項(xiàng)目實(shí)踐
- springboot集成mybatis-plus全過程
- Springboot集成Mybatis-plus、ClickHouse實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能
- springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問題
- SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)分頁接口的詳細(xì)教程
- SpringBoot?mybatis-plus使用json字段實(shí)戰(zhàn)指南
- springboot3.2整合mybatis-plus詳細(xì)代碼示例
- 全網(wǎng)最新springboot整合mybatis-plus的過程
相關(guān)文章
MyBatis-Plus如何通過注解使用TypeHandler
這篇文章主要介紹了MyBatis-Plus如何通過注解使用TypeHandler,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn)
SpringBoot內(nèi)置tomcat使用很方便,本文主要介紹了SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07Spring Cloud Gateway重試機(jī)制原理解析
這篇文章主要介紹了Spring Cloud Gateway重試機(jī)制原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08解決Springboot get請(qǐng)求是參數(shù)過長(zhǎng)的情況
這篇文章主要介紹了解決Springboot get請(qǐng)求是參數(shù)過長(zhǎng)的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09springBoot集成shiro實(shí)現(xiàn)權(quán)限刷新
在SpringBoot項(xiàng)目中集成Shiro進(jìn)行權(quán)限管理,包括基礎(chǔ)配置引入依賴、創(chuàng)建Shiro配置類以及用戶認(rèn)證與授權(quán)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-11-11