欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表的示例

 更新時(shí)間:2024年06月24日 09:08:58   作者:ldcaws  
本文主要介紹了springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

好長(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)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • springboot 監(jiān)控管理模塊搭建的方法

    springboot 監(jiān)控管理模塊搭建的方法

    本篇文章主要介紹了springboot 監(jiān)控管理模塊搭建的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • MyBatis-Plus如何通過注解使用TypeHandler

    MyBatis-Plus如何通過注解使用TypeHandler

    這篇文章主要介紹了MyBatis-Plus如何通過注解使用TypeHandler,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn)

    SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn)

    SpringBoot內(nèi)置tomcat使用很方便,本文主要介紹了SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • Spring Cloud Gateway重試機(jī)制原理解析

    Spring Cloud Gateway重試機(jī)制原理解析

    這篇文章主要介紹了Spring Cloud Gateway重試機(jī)制原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 從千千靜聽歌詞服務(wù)器獲取lrc歌詞示例分享

    從千千靜聽歌詞服務(wù)器獲取lrc歌詞示例分享

    這篇文章主要介紹了使用PHP從千千靜聽歌詞服務(wù)器獲取lrc歌詞的方法,大家參考使用吧
    2014-01-01
  • 解決Springboot get請(qǐng)求是參數(shù)過長(zhǎng)的情況

    解決Springboot get請(qǐng)求是參數(shù)過長(zhǎng)的情況

    這篇文章主要介紹了解決Springboot get請(qǐng)求是參數(shù)過長(zhǎng)的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • springBoot集成shiro實(shí)現(xiàn)權(quán)限刷新

    springBoot集成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
  • java ZipFile如何將多級(jí)目錄壓縮

    java ZipFile如何將多級(jí)目錄壓縮

    這篇文章主要介紹了java ZipFile如何將多級(jí)目錄壓縮問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java面試題沖刺第一天--基礎(chǔ)篇1

    Java面試題沖刺第一天--基礎(chǔ)篇1

    這篇文章主要為大家分享了最有價(jià)值的三道java面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java手寫線程池的實(shí)現(xiàn)方法

    Java手寫線程池的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Java手寫線程池的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評(píng)論