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

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)文章

  • 淺談選擇、冒泡排序,二分查找法以及一些for循環(huán)的靈活運(yùn)用

    淺談選擇、冒泡排序,二分查找法以及一些for循環(huán)的靈活運(yùn)用

    下面小編就為大家?guī)硪黄獪\談選擇、冒泡排序,二分查找法以及一些for循環(huán)的靈活運(yùn)用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • spring boot線上日志級(jí)別動(dòng)態(tài)調(diào)整的配置步驟

    spring boot線上日志級(jí)別動(dòng)態(tài)調(diào)整的配置步驟

    這篇文章主要為大家介紹了spring boot線上日志級(jí)別動(dòng)態(tài)調(diào)整的配置步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的實(shí)現(xiàn)詳解

    Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的實(shí)現(xiàn)詳解

    平衡二叉樹又被稱為AVL樹(有別于AVL算法),且具有以下性質(zhì):它是一棵空樹或它的左右兩個(gè)子樹的高度差的絕對(duì)值不超過1,并且左右兩個(gè)子樹都是一棵平衡二叉樹。本文將詳解介紹一下平衡二叉樹的原理與實(shí)現(xiàn),需要的可以參考一下
    2022-03-03
  • Java處理表格的實(shí)用工具庫

    Java處理表格的實(shí)用工具庫

    EasyExcel是一個(gè)基于Java的簡單、省內(nèi)存的讀寫Excel的開源項(xiàng)目,在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M(fèi)的Excel,下面這篇文章主要給大家分享介紹了一個(gè)關(guān)于Java處理表格的實(shí)用工具庫,需要的朋友可以參考下
    2021-11-11
  • Java線程池的拒絕策略實(shí)現(xiàn)詳解

    Java線程池的拒絕策略實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java線程池的拒絕策略實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java去除字符串空格的幾種方法簡單舉例

    Java去除字符串空格的幾種方法簡單舉例

    在Java中要去掉字符串中的空格,可以使用多種方法,這篇文章主要給大家介紹了關(guān)于Java去除字符串空格的幾種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • MybatisPlus條件查詢的具體使用

    MybatisPlus條件查詢的具體使用

    MybatisPlus通過條件構(gòu)造器可以組裝復(fù)雜的查詢條件,本文主要介紹了MybatisPlus條件查詢的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式

    Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式

    這篇文章主要介紹了Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring框架應(yīng)用的權(quán)限控制系統(tǒng)詳解

    Spring框架應(yīng)用的權(quán)限控制系統(tǒng)詳解

    在本篇文章里小編給大家整理的是關(guān)于基于Spring框架應(yīng)用的權(quán)限控制系統(tǒng)的研究和實(shí)現(xiàn),需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解

    SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解

    這篇文章主要介紹了SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解,我們常使用@Secured與@PreAuthorize兩個(gè)注解在進(jìn)入方法前進(jìn)行角色、權(quán)限的控制,進(jìn)入方法前數(shù)據(jù)的過濾@PreFilter注解偶爾會(huì)看到,需要的朋友可以參考下
    2023-12-12

最新評(píng)論