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

SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動(dòng)生成相關(guān)文件的示例代碼

 更新時(shí)間:2021年12月15日 16:35:55   作者:荒城謀生  
Mybatis-Plus是一個(gè)優(yōu)秀的Mybatis增強(qiáng)工具,目前更新到3.1.1,本文通過示例代碼給大家介紹SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動(dòng)生成相關(guān)文件的問題,感興趣的朋友跟隨小編一起看看吧

Mybatis-Plus是一個(gè)優(yōu)秀的Mybatis增強(qiáng)工具,目前更新到3.1.1。Mybatis-Plus原生提供了很多單表操作的方法,極大簡(jiǎn)化了繁瑣的curd的操作,同時(shí)又支持xml配置、自定義sql的編寫。這篇文章介紹SpringBoot2集成Mybatis-Plus 3.1.0,同時(shí)介紹mybatis提供MysqlGenerator.java,你可以通過指定的數(shù)據(jù)庫表生成對(duì)應(yīng)的bean、mapper.xml、mapper.java、service.java、serviceImpl.java,甚至controller

1.pom.xml添加相關(guān)依賴,請(qǐng)注意版本號(hào):

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.asiainfo</groupId>
    <artifactId>rocketmq-producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rocketmq-producer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--Springboot集成mybatis-plus開始-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.5</version>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 添加代碼 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-filtering</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

2.application.yml

server:
  port: 9999
spring:
  application:
    name: springboot-mybatisPlus
  # database 部分注釋
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/rocketmq?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconect=true&serverTimezone=GMT%2b8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 50
    initialSize: 0
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
    removeAbandoned: true
    removeAbandonedTimeout: 180
mybatis-plus:
  global-config:
    # 邏輯刪除配置
    db-config:
      # 刪除前
      logic-not-delete-value: 1
      # 刪除后
      logic-delete-value: 0
  configuration:
    map-underscore-to-camel-case: true
    auto-mapping-behavior: full
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mybatisplus/mapper/*.xml

3.MyBatisPlusConfig.java

package com.asiainfo.crm.rocketmq.config;

import com.baomidou.mybatisplus.mapper.ISqlInjector;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * @author zhangpb
 * @date 2021/12/15 14:13
 * @Description:
 */
@Configuration
@Slf4j
public class MyBatisPlusConfig {
    /**
     * @description: 配置分頁插件
     *
     * @author: zhangpb
     * @date: 2019/1/15 10:17
     * @param: []
     * @return: com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        log.debug("注冊(cè)分頁插件");
        return new PaginationInterceptor();
    }

    /**
     * @description: SQL執(zhí)行效率插件
     *
     * @author: zhangpb
     * @date: 19-1-24 下午4:59
     * @param: []
     * @return: com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor
     */
    @Bean
    @Profile({"test"})// 設(shè)置 dev test 環(huán)境開啟
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

    /**
     * 邏輯刪除用,3.1.1之后的版本可不需要配置該bean,但項(xiàng)目這里用的是3.1.0的
     *
     * @author zhangpb
     *
     * @return com.baomidou.mybatisplus.core.injector.ISqlInjector
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

4.MysqlGenerator.java

package com.asiainfo.crm.rocketmq.config;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangpb
 * @date 2021/12/15 14:14
 * @Description:
 */
public class MysqlGenerator {

    /**
     * 每次只生成一張表的
     * @param args
     */
    public static void main(String[] args) {
        // 代碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
//        gc.setOutputDir(projectPath + "/src/main/java");
        String projectPath = "E://java-workspace//rocketmq//rocketmq_space//rocketmq//rocketmq-producer";
        gc.setOutputDir(projectPath + "\\src\\main\\java\\com\\asiainfo\\crm\\rocketmq\\cell");

        // TODO 設(shè)置用戶名
        gc.setAuthor("zhangpb");
        gc.setOpen(true);
        // service 命名方式
        gc.setServiceName("%sService");
        // service impl 命名方式
        gc.setServiceImplName("%sServiceImpl");
        // 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性!
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        // XML 二級(jí)緩存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(false);
        mpg.setGlobalConfig(gc);

        // TODO 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/rocketmq?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // TODO 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模塊名"));
//        pc.setParent("com.zhangpb.demodruid");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);

        // 自定義需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();
        //如 每張表都有一個(gè)創(chuàng)建時(shí)間、修改時(shí)間
        //而且這基本上就是通用的了,新增時(shí),創(chuàng)建時(shí)間和修改時(shí)間同時(shí)修改
        //修改時(shí),修改時(shí)間會(huì)修改,
        //雖然像Mysql數(shù)據(jù)庫有自動(dòng)更新幾只,但像ORACLE的數(shù)據(jù)庫就沒有了,
        //使用公共字段填充功能,就可以實(shí)現(xiàn),自動(dòng)按場(chǎng)景更新了。
        //如下是配置
        //TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);
        //TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        //tableFillList.add(createField);
        //tableFillList.add(modifiedField);

        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸入文件名稱
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + ".xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        // 設(shè)置邏輯刪除鍵
        strategy.setLogicDeleteFieldName("deleted");
        // TODO 指定生成的bean的數(shù)據(jù)庫表名
        strategy.setInclude("trade_coupon");
        //strategy.setSuperEntityColumns("id");
        // 駝峰轉(zhuǎn)連字符
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        // 選擇 freemarker 引擎需要指定如下加,注意 pom 依賴必須有!
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

到此這篇關(guān)于SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動(dòng)生成相關(guān)文件的文章就介紹到這了,更多相關(guān)SpringBoot集成Mybatis-plus自動(dòng)生成相關(guān)文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java線程優(yōu)先級(jí)變量及功能

    Java線程優(yōu)先級(jí)變量及功能

    這篇文章主要介紹了Java線程優(yōu)先級(jí)變量及功能,關(guān)于優(yōu)先級(jí)的問可能有兩個(gè)或更多線程被分配了相同的優(yōu)先級(jí),那么它們的執(zhí)行取決于操作系統(tǒng),更多相關(guān)介紹,需要的小伙伴可以參考一下
    2022-06-06
  • SpringBoot下無節(jié)制和數(shù)據(jù)庫建立連接的問題及解決方法

    SpringBoot下無節(jié)制和數(shù)據(jù)庫建立連接的問題及解決方法

    本文介紹了無節(jié)制建立MySQL連接的危害,包括數(shù)據(jù)庫服務(wù)端資源耗盡、應(yīng)用端性能劣化和監(jiān)控與運(yùn)維困境,提出了系統(tǒng)性解決方案,包括連接池標(biāo)準(zhǔn)化配置、代碼規(guī)范與防御式編程、全鏈路監(jiān)控體系和架構(gòu)級(jí)優(yōu)化,感興趣的朋友一起看看吧
    2025-03-03
  • SpringBoot整合Shiro和Redis的示例代碼

    SpringBoot整合Shiro和Redis的示例代碼

    這篇文章主要介紹了SpringBoot整合Shiro和Redis的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • Springboot 自定義校驗(yàn)代碼實(shí)例

    Springboot 自定義校驗(yàn)代碼實(shí)例

    這篇文章主要介紹了Springboot 自定義校驗(yàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程詳解

    SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程詳解

    這篇文章主要介紹了SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • Java?BOI與NIO超詳細(xì)實(shí)例精講

    Java?BOI與NIO超詳細(xì)實(shí)例精講

    在Java的軟件設(shè)計(jì)開發(fā)中,通信架構(gòu)是不可避免的,我們?cè)谶M(jìn)行不同系統(tǒng)或者不同進(jìn)程之間的數(shù)據(jù)交互,或者在高并發(fā)下的通信場(chǎng)景下都需要用到網(wǎng)絡(luò)通信相關(guān)的技術(shù),對(duì)于一些經(jīng)驗(yàn)豐富的程序員來說,Java早期的網(wǎng)絡(luò)通信架構(gòu)存在一些缺陷,這篇文章介紹Java?BOI與NIO
    2022-11-11
  • java實(shí)現(xiàn)對(duì)map的字典序排序操作示例

    java實(shí)現(xiàn)對(duì)map的字典序排序操作示例

    這篇文章主要介紹了java實(shí)現(xiàn)對(duì)map的字典序排序操作,結(jié)合實(shí)例形式分析了java參照微信官網(wǎng)算法實(shí)現(xiàn)的字典序排序操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • SpringBoot+Idea熱部署實(shí)現(xiàn)流程解析

    SpringBoot+Idea熱部署實(shí)現(xiàn)流程解析

    這篇文章主要介紹了SpringBoot+Idea熱部署實(shí)現(xiàn)流程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • java不同線程解讀以及線程池的使用方式

    java不同線程解讀以及線程池的使用方式

    這篇文章主要介紹了java不同線程解讀以及線程池的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 老生常談spring boot 1.5.4 日志管理(必看篇)

    老生常談spring boot 1.5.4 日志管理(必看篇)

    下面小編就為大家?guī)硪黄仙U剆pring boot 1.5.4 日志管理(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06

最新評(píng)論

宁城县| 介休市| 岳西县| 湟源县| 乐东| 惠东县| 肥城市| 新竹市| 马公市| 昌邑市| 天门市| 通海县| 库车县| 宽城| 九寨沟县| 阳泉市| 普陀区| 抚远县| 慈溪市| 白山市| 历史| 晋城| 屏边| 舟曲县| 灵寿县| 贵定县| 明星| 怀集县| 兰溪市| 通化市| 剑河县| 云南省| 德兴市| 镇赉县| 固原市| 湘潭县| 肥西县| 香格里拉县| 开江县| 徐汇区| 垫江县|