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

springboot集成mybaits-generator自動生成代碼的流程分析

 更新時間:2025年04月17日 09:27:06   作者:冉成未來  
這篇文章主要介紹了springboot集成mybaits-generator自動生成代碼的流程分析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,感興趣的朋友一起看看吧

概述

創(chuàng)建springboot項目,在這里使用的是springboot 2.6.13版本,引入的項目依賴包如pom文件所寫,jdk使用1.8,集成swagger。

創(chuàng)建springboot項目

使用start.aliyun.com創(chuàng)建springboot項目

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rc</groupId>
    <artifactId>generator-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>generator-demo</name>
    <description>generator-demo</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--mysql連接驅(qū)動-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <!--lombok類-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!--druid數(shù)據(jù)源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.20</version>
        </dependency>
        <!--swagger-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <!--熱啟動-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!--mybatis-plus代碼生成-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!--freemarker模板引擎-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>
        <!--lang3工具類-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!--測試-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.rc.GeneratorDemoApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

使用mybatis-plus-generator時需要注意版本號,本項目使用的是3.4.0版本

aplication.yml

server:
  port: 8096
  servlet:
    context-path: /
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/you_table?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
    username: root
    password: root

代碼生成類

public class CodeGenerator {
    public static String tables = "user";  //需要自動生成代碼的表,以英文逗號隔開
    public static void main(String[] args) {
        //代碼生成器對象
        AutoGenerator autoGenerator = new AutoGenerator();
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        //獲取項目目錄
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("rc");
        gc.setOpen(false);
        gc.setServiceName("%sService");
        autoGenerator.setGlobalConfig(gc);
        //數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://192.168.1.229:3306/rcdb?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("wangcheng");
        autoGenerator.setDataSource(dsc);
        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(null);
        pc.setParent("com.rc");
        autoGenerator.setPackageInfo(pc);
        //自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        //模板引擎是freemarker
        String templatePath = "/template/mapper.xml.ftl";
        //模版引擎是velocity
        //String templatePath = "/template/mapper.xml.vm";
        List<FileOutConfig> focList = new ArrayList<>();
        //需要生成*mapper.xml時需要配置
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath+"/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);
        //配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("template/Controller.java");
        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setInclude(tables.split(","));
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setTablePrefix("m_");
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }
}

mybatis-plus提供的變量

{
    "date": "2018-10-30",
    "superServiceImplClassPackage": "com.baomidou.mybatisplus.extension.service.impl.ServiceImpl",
    "baseResultMap": true,
    "superMapperClass": "BaseMapper",
    "activeRecord": true,
    "superServiceClass": "IService",
    "superServiceImplClass": "ServiceImpl",
    "table": {
        "comment": "表備注",
        "commonFields": [],
        "controllerName": "AirStatLiveDataController",
        "convert": false,
        "entityName": "AirStatLiveData",
        "entityPath": "airStatLiveData",
        "fieldNames": "table_id, citycode, cityname, createtime, time, info, devid, stationname, aqi, pm25, pm10, co, so2, no2, o3, prkey",
        "fields": [{
            "capitalName": "TableId",
            "columnType": "LONG",
            "comment": "表單id",
            "convert": false,
            "keyFlag": true,
            "keyIdentityFlag": true,
            "name": "table_id",
            "propertyName": "tableId",
            "propertyType": "Long",
            "type": "bigint(20)"
        }...],
        "importPackages": ["com.baomidou.mybatisplus.annotation.IdType", "com.baomidou.mybatisplus.extension.activerecord.Model", "com.baomidou.mybatisplus.annotation.TableId", "java.io.Serializable"],
        "mapperName": "xxxMapper",
        "name": "tableName",
        "serviceImplName": "xxxServiceImpl",
        "serviceName": "xxxService",
        "xmlName": "xxxMapper"
    },
    "package": {
        "Entity": "com.cy.entity",
        "Mapper": "com.cy.mapper",
        "Xml": "com.cy.mapper",
        "ServiceImpl": "com.cy.service.impl",
        "Service": "com.cy.service",
        "Controller": "com.cy.controller"
    },
    "author": "author",
    "swagger2": false,
    "baseColumnList": false,
    "kotlin": false,
    "entityLombokModel": false,
    "superMapperClassPackage": "com.baomidou.mybatisplus.core.mapper.BaseMapper",
    "restControllerStyle": false,
    "entityBuilderModel": true,
    "superServiceClassPackage": "com.baomidou.mybatisplus.extension.service.IService",
    "entityBooleanColumnRemoveIsPrefix": false,
    "entityColumnConstant": false,
    "config": {
        "globalConfig": {
            "activeRecord": true,
            "author": "author",
            "baseColumnList": false,
            "baseResultMap": true,
            "controllerName": "%sController",
            "dateType": "TIME_PACK",
            "enableCache": false,
            "fileOverride": false,
            "kotlin": false,
            "mapperName": "%sMapper",
            "open": false,
            "outputDir": "",
            "serviceImplName": "%sServiceImpl",
            "serviceName": "%sService",
            "swagger2": false,
            "xmlName": "%sMapper"
        },
        "packageInfo": {
            "$ref": "$.package"
        },
        "pathInfo": {
            "entity_path": "",
            "controller_path": "",
            "xml_path": "",
            "service_path": "",
            "mapper_path": "",
            "service_impl_path": ""
        },
        "strategyConfig": {
            "capitalMode": false,
            "columnNaming": "underline_to_camel",
            "controllerMappingHyphenStyle": false,
            "entityBooleanColumnRemoveIsPrefix": false,
            "entityBuilderModel": true,
            "entityColumnConstant": false,
            "entityLombokModel": false,
            "entityTableFieldAnnotationEnable": false,
            "include": ["air_stat_live_data"],
            "naming": "underline_to_camel",
            "restControllerStyle": false,
            "skipView": false,
            "superMapperClass": "com.baomidou.mybatisplus.core.mapper.BaseMapper",
            "superServiceClass": "com.baomidou.mybatisplus.extension.service.IService",
            "superServiceImplClass": "com.baomidou.mybatisplus.extension.service.impl.ServiceImpl"
        },
        "superMapperClass": "com.baomidou.mybatisplus.core.mapper.BaseMapper",
        "superServiceClass": "com.baomidou.mybatisplus.extension.service.IService",
        "superServiceImplClass": "com.baomidou.mybatisplus.extension.service.impl.ServiceImpl",
        "tableInfoList": [{
            "$ref": "$.table"
        }],
        "template": {
            "controller": "/templates/controller.java",
            "mapper": "/templates/mapper.java",
            "service": "/templates/service.java",
            "serviceImpl": "/templates/serviceImpl.java",
            "xml": "/templates/mapper.xml"
        }
    },
    "enableCache": false,
    "entity": "AirStatLiveData"
}

模板中的動態(tài)數(shù)據(jù)可以根據(jù)mybatis-plus提供的變量進行填充

controller模板

Controller.java.ftl

package ${package.Controller};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.rc.entity.web.PageParam;
import com.rc.entity.web.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(tags="$(table.comment)")
@RequestMapping("/${entity?uncap_first}")
public class ${entity}Controller {
    @Autowired
    private ${table.serviceName} ${entity?uncap_first}Service;
    /**
    * 根據(jù)id獲取對象
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public ${entity} getById(@PathVariable Long id) {
        return ${entity?uncap_first}Service.getById(id);
    }
    /**
    * 分頁查詢
    * @param pageParam
    * @return
    */
    @PostMapping("/queryByPage")
    public Result queryUserByPage(PageParam pageParam){
        QueryWrapper<${entity}> ${entity?uncap_first}QueryWrapper = new QueryWrapper<>();
        Map<String, String> queryMap = pageParam.getQueryMap();
        if (StringUtils.isNotBlank(queryMap.get("id"))){
            ${entity?uncap_first}QueryWrapper.like("id",queryMap.get("id"));
        }
        Page<${entity}> ${entity?uncap_first}Page = ${entity?uncap_first}Service.page(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), ${entity?uncap_first}QueryWrapper);
        return Result.SUCCESS(${entity?uncap_first}Page);
    }
}

mapper模板

mapper.xml.ftl

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
</mapper>

總結(jié)

通過繼承mybaits-generotar可以快速根據(jù)表生成后端代碼,簡化開發(fā),開發(fā)過程中只需要專注于開發(fā)業(yè)務(wù)邏輯的梳理。
項目地址:https://gitee.com/wangcheng626/generator-demo.git

到此這篇關(guān)于springboot集成mybaits-generator自動生成代碼的文章就介紹到這了,更多相關(guān)springboot集成mybaits-generator內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringCloud如何搭建一個多模塊項目

    SpringCloud如何搭建一個多模塊項目

    這篇文章主要介紹了SpringCloud如何搭建一個多模塊項目,記錄下使用SpringCloud創(chuàng)建多模塊項目,一步一步記錄搭建的過程,感興趣的可以了解一下
    2021-05-05
  • java版微信和支付寶退款接口

    java版微信和支付寶退款接口

    這篇文章主要為大家詳細介紹了java版微信退款接口和java版支付寶退款接口,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • JAVA音頻處理依賴庫示例操作大全(從格式轉(zhuǎn)換到音頻拼接)

    JAVA音頻處理依賴庫示例操作大全(從格式轉(zhuǎn)換到音頻拼接)

    在現(xiàn)代應(yīng)用開發(fā)中,音頻處理是常見需求,包括格式轉(zhuǎn)換、音頻拼接、剪輯、降噪等操作,本教程將介紹Spring?Boot中常用的音頻處理依賴庫,比較它們的特性、區(qū)別及社區(qū)活躍程度,并提供實用代碼示例,感興趣的朋友跟隨小編一起看看吧
    2025-10-10
  • 使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra

    使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra

    這篇文章主要介紹了使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • Spring AOP的底層實現(xiàn)方式-代理模式

    Spring AOP的底層實現(xiàn)方式-代理模式

    這篇文章主要介紹了Spring AOP的底層實現(xiàn)方式-代理模式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Springboot在IDEA熱部署的配置方法

    Springboot在IDEA熱部署的配置方法

    這篇文章主要介紹了Springboot在IDEA熱部署的配置方法,給大家補充介紹了Intellij IDEA 4種配置熱部署的方法,需要的朋友可以參考下
    2018-04-04
  • SpringBoot單機限流的實現(xiàn)

    SpringBoot單機限流的實現(xiàn)

    在系統(tǒng)運維中, 有時候為了避免用戶的惡意刷接口, 會加入一定規(guī)則的限流,本文主要介紹了SpringBoot單機限流的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • SpringBoot多環(huán)境配置方式的新手教程

    SpringBoot多環(huán)境配置方式的新手教程

    我們平時做項目的時候,一般都會分幾套環(huán)境,每一套環(huán)境的配置都是不一樣的,所以這篇文章就來為大家詳細介紹一下SpringBoot多環(huán)境配置方式,希望對大家有所幫助
    2023-11-11
  • Java生成二維碼的兩種實現(xiàn)方式(基于Spring?Boot)

    Java生成二維碼的兩種實現(xiàn)方式(基于Spring?Boot)

    這篇文章主要給大家介紹了關(guān)于Java生成二維碼的兩種實現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎(chǔ)開發(fā),文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • 解決idea無法導(dǎo)入識別本地類的問題

    解決idea無法導(dǎo)入識別本地類的問題

    今天做實驗不知道按了哪里不能導(dǎo)入識別本地的類,只有jar包的類,百度搜索也沒有找到合理的解決方案,經(jīng)過朋友援助問題根源找到,下面小編把解決方法分享給大家,需要的朋友參考下吧
    2021-08-08

最新評論

美姑县| 甘孜县| 溧水县| 沂源县| 浑源县| 扎兰屯市| 贡觉县| 垣曲县| 长岛县| 金沙县| 收藏| 翁牛特旗| 青浦区| 金山区| 巨鹿县| 晋宁县| 腾冲县| 仁寿县| 淮安市| 裕民县| 那曲县| 德江县| 霞浦县| 同心县| 江达县| 平湖市| 怀安县| 子洲县| 珠海市| 彭泽县| 岐山县| 吉首市| 图木舒克市| 天门市| 安达市| 合山市| 乐平市| 乃东县| 广平县| 呼伦贝尔市| 横山县|