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

springboot如何使用MybatisPlus

 更新時(shí)間:2024年09月09日 11:46:04   作者:BinaryBlade  
MyBatisPlus是一個(gè)強(qiáng)大的數(shù)據(jù)庫操作框架,其代碼生成器可以快速生成實(shí)體類、映射文件等,本文介紹了如何導(dǎo)入MyBatisPlus相關(guān)依賴,創(chuàng)建代碼生成器,并配置數(shù)據(jù)庫信息以逆向生成代碼,感興趣的朋友跟隨小編一起看看吧

 MybatisPlus幫助文檔地址:代碼生成器 | MyBatis-Plus

 導(dǎo)入相關(guān)依賴

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>

第一個(gè)為MybatisPlus的起步依賴,后面兩個(gè)依賴是為逆向生成服務(wù)的。

 代碼生成器

package cn.xzit.springbootmybatisplus;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*該工具類用于生成實(shí)體類,mapper,service,controller
 * 用法:右鍵run該類,控制臺(tái)輸入表名,回車即可
 * */
/**
 * @author noBody
 */
public class CodeGenerator {
    /**
     * <p>
     * 讀取控制臺(tái)內(nèi)容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請(qǐng)輸入" + tip + ":");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!");
    }
    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");
        gc.setAuthor("jyz");
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        // gc.setSwagger2(true); 實(shí)體屬性 Swagger2 注解
        mpg.setGlobalConfig(gc);
        // 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        //  pc.setModuleName(scanner("模塊名"));
        pc.setParent("cn");/*生成的所有entity mapper等會(huì)放進(jìn)主包里面*/
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";
        // 自定義輸出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定義配置會(huì)被優(yōu)先輸出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化??!
                return projectPath + "/src/main/resources/mapping/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
/*        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判斷自定義文件夾是否需要?jiǎng)?chuàng)建
                checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄,自定義目錄用");
                if (fileType == FileType.MAPPER) {
                    // 已經(jīng)生成 mapper 文件判斷存在,不想重新生成返回 false
                    return !new File(filePath).exists();
                }
                // 允許生成模板文件
                return true;
            }
        });*/
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定義輸出模板
        //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會(huì)根據(jù)使用的模板引擎自動(dòng)識(shí)別
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("你自己的父類實(shí)體,沒有就不用設(shè)置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父類
        //strategy.setSuperControllerClass("你自己的父類控制器,沒有就不用設(shè)置!");
        // 寫于父類中的公共字段
        // strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

 創(chuàng)建一個(gè)generator包,包下面創(chuàng)建一個(gè)CodeGenerator類,類中放以上的代碼,修改其中的數(shù)據(jù)庫名和用戶名以及密碼,右鍵運(yùn)行。輸入需要逆向生成的表名,多個(gè)表用逗號(hào)隔開,然后回車。

注意要修改主包,我的主包為cn,讀者可以自行修改代碼中的setParent中的內(nèi)容。

配置文件.yml文件

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: 123456
    hikari:
      connection-timeout: 6000
      maximum-pool-size: 5
#  jackson:
#    date-format: yyyy-MM-dd HH:mm:ss
#    time-zone: GMT+8
#    serialization:
#      write-dates-as-timestamps: false
mybatis-plus:
  configuration:
    #開啟駝峰功能,數(shù)據(jù)庫字段hello_world 實(shí)體類helloWorld 也能對(duì)應(yīng)匹配
    map-underscore-to-camel-case: true
    #結(jié)果集自動(dòng)映射(resultMap)
    auto-mapping-behavior: full
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:mapping/*Mapper.xml
  global-config:
    # 邏輯刪除配置
    db-config:
      # 刪除前
      logic-not-delete-value: 1
      # 刪除后
      logic-delete-value: 0

到此這篇關(guān)于springboot使用MybatisPlus的文章就介紹到這了,更多相關(guān)springboot使用MybatisPlus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring Data Jpa 模糊查詢的正確用法

    詳解Spring Data Jpa 模糊查詢的正確用法

    本篇文章主要介紹了詳解Spring Data Jpa 模糊查詢的正確用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • 解決FontConfiguration.getVersion報(bào)空指針異常的問題

    解決FontConfiguration.getVersion報(bào)空指針異常的問題

    這篇文章主要介紹了解決FontConfiguration.getVersion報(bào)空指針異常的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 使用SpringBoot整合Activiti6工作流的操作方法

    使用SpringBoot整合Activiti6工作流的操作方法

    這篇文章主要介紹了使用SpringBoot整合Activiti6工作流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Spring MVC登錄注冊(cè)以及轉(zhuǎn)換json數(shù)據(jù)

    Spring MVC登錄注冊(cè)以及轉(zhuǎn)換json數(shù)據(jù)

    本文主要介紹了Spring MVC登錄注冊(cè)以及轉(zhuǎn)換json數(shù)據(jù)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • MultipartFile中transferTo(File file)的路徑問題及解決

    MultipartFile中transferTo(File file)的路徑問題及解決

    這篇文章主要介紹了MultipartFile中transferTo(File file)的路徑問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)分頁接口的詳細(xì)教程

    SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)分頁接口的詳細(xì)教程

    MyBatis-Plus是一個(gè)MyBatis的增強(qiáng)工具,在MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,在SpringBoot項(xiàng)目中使用MyBatis-Plus可以大大簡(jiǎn)化分頁邏輯的編寫,本文將介紹如何在 SpringBoot項(xiàng)目中使用MyBatis-Plus實(shí)現(xiàn)分頁接口
    2024-03-03
  • Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng)

    Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng)

    instanceof 是 Java 的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類的實(shí)例,返回 boolean 的數(shù)據(jù)類型。本文重點(diǎn)給大家介紹Java instanceof關(guān)鍵字用法詳解及注意事項(xiàng),需要的朋友參考下吧
    2021-09-09
  • hibernate中的對(duì)象關(guān)系映射

    hibernate中的對(duì)象關(guān)系映射

    hibernate中的ORM映射文件通常以.hbm.xml作為后綴。使用這個(gè)映射文件不僅易讀,而且可以手工修改,也可以通過一些工具來生成映射文檔,下文給大家詳細(xì)的介紹hibernate中的對(duì)象關(guān)系映射,需要的朋友參考下吧
    2017-09-09
  • Spring框架基于xml實(shí)現(xiàn)自動(dòng)裝配流程詳解

    Spring框架基于xml實(shí)現(xiàn)自動(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?中
    2022-11-11
  • Java網(wǎng)絡(luò)編程三要素及通信程序詳解

    Java網(wǎng)絡(luò)編程三要素及通信程序詳解

    這篇文章主要介紹了Java網(wǎng)絡(luò)編程三要素及通信程序詳解,Java網(wǎng)絡(luò)編程是在網(wǎng)絡(luò)通信協(xié)議下,實(shí)現(xiàn)網(wǎng)絡(luò)互連的不同計(jì)算機(jī)上運(yùn)行的程序間可以進(jìn)行數(shù)據(jù)交換,需要的朋友可以參考下
    2023-07-07

最新評(píng)論

成武县| 泾川县| 南丰县| 德兴市| 昌图县| 临江市| 门源| 西昌市| 曲沃县| 温州市| 武宣县| 温州市| 杭锦后旗| 平塘县| 中阳县| 出国| 尉氏县| 报价| 山阴县| 井陉县| 昌宁县| 石狮市| 嘉义县| 蛟河市| 汾阳市| 大化| 宝山区| 平陆县| 紫阳县| 长寿区| 金坛市| 青铜峡市| 云龙县| 比如县| 陵川县| 望城县| 福贡县| 奇台县| 株洲县| 孝昌县| 晋中市|