最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼

 更新時(shí)間:2024年12月09日 10:01:58   作者:保持充電  
本文介紹如何使用SpringBoot、MyBatis-Plus進(jìn)行逆向工程自動(dòng)生成代碼,并結(jié)合Swagger3.0實(shí)現(xiàn)API文檔的自動(dòng)生成和訪問(wèn),通過(guò)詳細(xì)步驟和配置,確保Swagger與SpringBoot版本兼容,并通過(guò)配置文件和測(cè)試類(lèi)實(shí)現(xiàn)代碼生成和Swagger文檔的訪問(wèn)

SpringBoot + MyBatis-Plus逆向工程自動(dòng)生成代碼 Swagger3.0 訪問(wèn)

廢話不多說(shuō),直接see code

1. 新建SpringBoot項(xiàng)目

我這里是SpringBoot 2.7.1版本

2. 導(dǎo)入相關(guān)依賴(lài)

 		<!-- Mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- 代碼生成器(新版本) -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- swagger3 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- 測(cè)試 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • 大家要注意 swagger 版本和 springboot 版本有沖突,大坑
  • 我這里 swagger3 + springboot 2.7.1 是沒(méi)問(wèn)題的

3. 配置文件application.yml

server:
  port: 8081

spring:
  mvc:
    pathmatch:
      # 因?yàn)镾pringfox使用的路徑匹配是基于AntPathMatcher的,而SpringBoot2.6.X以上使用的是PathPatternMatcher
      matching-strategy: ant_path_matcher

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  # 日志打印SQL開(kāi)啟,便于查找問(wèn)題
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    auto-mapping-behavior: full #自動(dòng)填充

matching-strategy: ant_path_matcher

因?yàn)镾pringfox使用的路徑匹配是基于AntPathMatcher的,而SpringBoot2.6.X以上使用的是PathPatternMatcher

不配置訪問(wèn)不到(無(wú)法訪問(wèn)此網(wǎng)站)

4. 新建測(cè)試類(lèi)(重點(diǎn))

package com.soul.zzq;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.Collections;

/**
 * mybatisPlus逆向工程(懂得都懂)
 * @Author zhouzhongqing
 * @Date 2021/10/31 17:31
 * @Version 1.0
 **/
@SpringBootTest
public class MybatisAutoGeneratorCodeTest {

    /**
     * 只需改動(dòng)以下,其他基本不動(dòng)
     * create 數(shù)據(jù)庫(kù)連接
     * outputDir 指定輸出目錄 (這個(gè)一般都是固定的)
     * parent 設(shè)置父包名 (com/cn.xxx.xxx)
     * moduleName 設(shè)置父包模塊名 (設(shè)置這個(gè)就會(huì)把entity、service、mapper、controller都放在一個(gè)單獨(dú)文件中,
     * 例如:
     *        {
     * user -   entity
     *          service
     *          mapper
     *          controller
     *        }
     *
     * )
     * pathInfo 設(shè)置mapperXml生成路徑 (這個(gè)一般都是固定的)
     * addInclude 設(shè)置需要生成的表名 (可以傳數(shù)組多個(gè),也可以單個(gè))
     * addTablePrefix 設(shè)置過(guò)濾表前綴(可選修改,也可以注釋?zhuān)鶕?jù)需求)
     *
     * @Author: zhouStart
     * @Date 2023/12/10 13:36
     */
    @Test
    public void generator() {
        // FastAutoGenerator:快速自動(dòng)生成,采用建造者設(shè)計(jì)模式
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/soul",
                        "root", "123456")
                .globalConfig(builder -> {
                    builder.author("zhouStart") // 設(shè)置作者
                            .enableSwagger() // 開(kāi)啟 swagger 模式
//                            .fileOverride() // 覆蓋已生成文件,默認(rèn)值:false
                            .disableOpenDir() //禁止打開(kāi)輸出目錄,默認(rèn)值:true
                            .dateType(DateType.ONLY_DATE) //定義生成的實(shí)體類(lèi)中日期類(lèi)型 時(shí)間策略 DateType.ONLY_DATE 默認(rèn)值: DateType.TIME_PACK
                            .outputDir("D:\\project\\soul-docker" + "/src/main/java"); // 指定輸出目錄
                })
                .packageConfig(builder -> {
                    builder.parent("com.soul.zzq") // 設(shè)置父包名
//                            .moduleName("") // 設(shè)置父包模塊名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\project\\soul-docker" + "/src/main/resources/mapper")); // 設(shè)置mapperXml生成路徑
                })
                .strategyConfig(builder -> {
                    builder.addInclude(Arrays.asList("user","product")); // 設(shè)置需要生成的表名
//                            .addTablePrefix("soul"); // 設(shè)置過(guò)濾表前綴
                    builder.entityBuilder()
                            .naming(NamingStrategy.underline_to_camel)//數(shù)據(jù)庫(kù)表映射到實(shí)體的命名策略.默認(rèn)下劃線轉(zhuǎn)駝峰命名
                            .columnNaming(NamingStrategy.underline_to_camel)//數(shù)據(jù)庫(kù)表字段映射到實(shí)體的命名策略,默認(rèn)為 null,未指定按照 naming 執(zhí)行
                            .addTableFills(new Column("createTime", FieldFill.INSERT))
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO) //主鍵策略
                            .enableLombok();//開(kāi)啟 lombok 模型,默認(rèn)值:false
                    builder.controllerBuilder()
                            .enableHyphenStyle()//開(kāi)啟駝峰轉(zhuǎn)連字符,默認(rèn)值:false
                            .enableRestStyle();//開(kāi)啟生成@RestController 控制器,默認(rèn)值:false
                    builder.serviceBuilder()
                            .formatServiceImplFileName("%sServiceImp")//格式化 service 實(shí)現(xiàn)類(lèi)文件名稱(chēng)
                            .formatServiceFileName("%sService");//格式化 service 接口文件名稱(chēng),去掉Service接口的首字母I

                })
                .strategyConfig(builder -> {
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默認(rèn)的是Velocity引擎模板
                .execute();

    }
}

運(yùn)行之前確認(rèn)好數(shù)據(jù)庫(kù)表是否已經(jīng)創(chuàng)建好了

5. 創(chuàng)建Swagger配置類(lèi)

package com.soul.zzq.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Description
 * @Author zhouzhongqing
 * @Date 2023/12/10 14:37
 * @Version 1.0
 **/
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    /**
     * 配置文檔生成最佳實(shí)踐
     *
     * @return
     */
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30).pathMapping("/")
                // 是否啟用Swagger
                .enable(true)
                // 用來(lái)創(chuàng)建該API的基本信息,展示在文檔的頁(yè)面中(自定義展示的信息)
                .apiInfo(apiInfo())
                .groupName("管理員")
                // 設(shè)置哪些接口暴露給Swagger展示
                .select()
                // 掃描所有有注解的api,用這種方式更靈活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 掃描指定包中的swagger注解
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .apis(RequestHandlerSelectors.basePackage("com.soul.zzq.controller"))
                // 掃描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 配置基本信息
     *
     * @return
     */
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger3")
                .description("swagger3")
                .version("v1.0")
                .termsOfServiceUrl("https://swagger.io/")
                .contact(new Contact("zhouStart", "https://swagger.io/", "zhouStart@163.com"))
                .build();
    }
}

6. 啟動(dòng)項(xiàng)目

瀏覽器訪問(wèn)Swagger http://localhost:8081/swagger-ui/#/

在這里插入圖片描述

完美

7. 完整pom.xml 和 項(xiàng)目文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.soul</groupId>
    <artifactId>soul-docker</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <!-- 代碼生成器(新版本) -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- swagger3 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- 測(cè)試 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>soul</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中的FutureTask實(shí)現(xiàn)代碼實(shí)例

    Java中的FutureTask實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Java中的FutureTask手寫(xiě)代碼實(shí)例,FutureTask是Future的實(shí)現(xiàn),用來(lái)異步任務(wù)的獲取結(jié)果,可以啟動(dòng)和取消異步任務(wù),查詢(xún)異步任務(wù)是否計(jì)算結(jié)束以及獲取最終的異步任務(wù)的結(jié)果,需要的朋友可以參考下
    2023-12-12
  • 使用Shell腳本實(shí)現(xiàn)SpringBoot項(xiàng)目自動(dòng)化部署到Docker的操作指南

    使用Shell腳本實(shí)現(xiàn)SpringBoot項(xiàng)目自動(dòng)化部署到Docker的操作指南

    在日常項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)將SpringBoot項(xiàng)目打包并部署到服務(wù)器上的Docker環(huán)境中,為了提升效率、減少重復(fù)操作,我們可以通過(guò)Shell腳本實(shí)現(xiàn)自動(dòng)化部署,所以本文給大家介紹了使用Shell腳本實(shí)現(xiàn)SpringBoot項(xiàng)目自動(dòng)化部署到Docker的操作指南,需要的朋友可以參考下
    2025-05-05
  • SpringBoot中的自動(dòng)裝配原理解析

    SpringBoot中的自動(dòng)裝配原理解析

    這篇文章主要介紹了SpringBoot中的自動(dòng)裝配原理解析,自動(dòng)裝配就是指 Spring 容器在不使用<constructor-arg>和<property>標(biāo)簽的情況下,可以自動(dòng)裝配(autowire)相互協(xié)作的Bean之間的關(guān)聯(lián)關(guān)系,將一個(gè) Bean注入其他Bean的Property中,需要的朋友可以參考下
    2023-08-08
  • Mybatis velocity腳本的使用教程詳解(推薦)

    Mybatis velocity腳本的使用教程詳解(推薦)

    很多朋友不清楚在mybatis可以使用各種腳本語(yǔ)言來(lái)定義Mapper文件里面的動(dòng)態(tài)SQL;目前mybatis支持的腳本語(yǔ)言有XML(默認(rèn)的);Velocity和Freemarker三種。下面通過(guò)本文給大家介紹Mybatis velocity腳本的使用,一起看看吧
    2016-11-11
  • SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類(lèi)的幾種解決方法

    SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類(lèi)的幾種解決方法

    本文主要介紹了SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類(lèi)的幾種解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • 淺談java中的重載和重寫(xiě)的區(qū)別

    淺談java中的重載和重寫(xiě)的區(qū)別

    本文主要介紹了java中的重載和重寫(xiě)的區(qū)別。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧,希望能夠給你帶來(lái)幫助
    2021-11-11
  • Spring中@Configuration注解的Full模式和Lite模式詳解

    Spring中@Configuration注解的Full模式和Lite模式詳解

    這篇文章主要介紹了Spring中@Configuration注解的Full模式和Lite模式詳解,準(zhǔn)確來(lái)說(shuō),Full?模式和?Lite?模式其實(shí)?Spring?容器在處理?Bean?時(shí)的兩種不同行為,這兩種不同的模式在使用時(shí)候的表現(xiàn)完全不同,今天就來(lái)和各位小伙伴捋一捋這兩種模式,需要的朋友可以參考下
    2023-09-09
  • Java 存儲(chǔ)模型和共享對(duì)象詳解

    Java 存儲(chǔ)模型和共享對(duì)象詳解

    這篇文章主要介紹了Java 存儲(chǔ)模型和共享對(duì)象詳解的相關(guān)資料,對(duì)Java存儲(chǔ)模型,可見(jiàn)性和安全發(fā)布的問(wèn)題是起源于Java的存儲(chǔ)結(jié)構(gòu)及共享對(duì)象安全,需要的朋友可以參考下
    2017-03-03
  • Java實(shí)現(xiàn)簡(jiǎn)單點(diǎn)餐系統(tǒng)

    Java實(shí)現(xiàn)簡(jiǎn)單點(diǎn)餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 利用hadoop查詢(xún)兩兩之間有共同好友及他倆的共同好友都是誰(shuí)

    利用hadoop查詢(xún)兩兩之間有共同好友及他倆的共同好友都是誰(shuí)

    一想到要實(shí)現(xiàn)求共同好友的功能,很多人都會(huì)想到redis來(lái)實(shí)現(xiàn)。但是redis存儲(chǔ)和數(shù)據(jù)和計(jì)算時(shí)需要耗費(fèi)較多的內(nèi)存資源。所以文本將介紹另一種方法,即利用Hadoop中的MapReduce來(lái)實(shí)現(xiàn),感興趣的可以了解一下
    2022-01-01

最新評(píng)論

济南市| 大庆市| 历史| 行唐县| 松江区| 句容市| 图木舒克市| 仙游县| 萨嘎县| 大洼县| 榆林市| 扬州市| 山丹县| 牡丹江市| 图木舒克市| 鄯善县| 山丹县| 玛多县| 成武县| 宁河县| 罗山县| 洪洞县| 万年县| 峨眉山市| 乡宁县| 台中市| 邮箱| 大同市| 江源县| 青海省| 积石山| 苗栗市| 五原县| 綦江县| 郧西县| 丰台区| 新化县| 怀柔区| 龙井市| 海门市| 庄浪县|