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

MyBatisPlus代碼生成器的使用示例

 更新時間:2021年12月30日 08:37:32   作者:萬里顧—程  
本文主要介紹了MyBatisPlus代碼生成器的使用示例,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,感興趣的可以了解一下

AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,極大的提升了開發(fā)效率。

導(dǎo)入依賴

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.19</version>
        </dependency>

更詳細(xì)的代碼生成器配置請查看官方文檔:https://baomidou.com/pages/061573/#superentityclass

表結(jié)構(gòu)

在這里插入圖片描述

當(dāng)前項目結(jié)構(gòu)

在這里插入圖片描述

配置代碼生成器

1、globalConfig 全局策略配置

outputDir

  • 生成文件的輸出目錄
  • 默認(rèn)值:D 盤根目錄

fileOverride

  • 是否覆蓋已有文件
  • 默認(rèn)值:false

open

  • 是否打開輸出目錄
  • 默認(rèn)值:true

enableCache

  • 是否在 xml 中添加二級緩存配置
  • 默認(rèn)值:false

開發(fā)人員

  • 默認(rèn)值:null

kotlin

  • 開啟 Kotlin 模式
  • 默認(rèn)值:false

swagger2

  • 開啟 swagger2 模式
  • 默認(rèn)值:false

activeRecord

  • 開啟 ActiveRecord 模式
  • 默認(rèn)值:false

baseResultMap

  • 開啟 BaseResultMap
  • 默認(rèn)值:false

baseColumnList

  • 開啟 baseColumnList
  • 默認(rèn)值:false

dateType

  • 時間類型對應(yīng)策略
  • 默認(rèn)值:TIME_PACK

entityName

  • 實體命名方式
  • 默認(rèn)值:null 例如:%sEntity 生成 UserEntity

mapperName

  • mapper 命名方式
  • 默認(rèn)值:null 例如:%sDao 生成 UserDao

xmlName

  • Mapper xml 命名方式
  • 默認(rèn)值:null 例如:%sDao 生成 UserDao.xml

serviceName

  • service 命名方式
  • 默認(rèn)值:null 例如:%sBusiness 生成 UserBusiness

serviceImplName

  • service impl 命名方式
  • 默認(rèn)值:null 例如:%sBusinessImpl 生成 UserBusinessImpl

controllerName

  • controller 命名方式
  • 默認(rèn)值:null 例如:%sAction 生成 UserAction

idType

  • 指定生成的主鍵的 ID 類型
  • 默認(rèn)值:null

2、dataSourceConfig 數(shù)據(jù)源配置

dbQuery

  • 數(shù)據(jù)庫信息查詢類
  • 默認(rèn)由 dbType 類型決定選擇對應(yīng)數(shù)據(jù)庫內(nèi)置實現(xiàn)

? 實現(xiàn) IDbQuery 接口自定義數(shù)據(jù)庫查詢 SQL 語句 定制化返回自己需要的內(nèi)容

dbType

  • 數(shù)據(jù)庫類型
  • 該類內(nèi)置了常用的數(shù)據(jù)庫類型【必須】

schemaName

  • 數(shù)據(jù)庫 schema name
  • 例如 PostgreSQL 可指定為 public

typeConvert

  • 類型轉(zhuǎn)換
  • 默認(rèn)由 dbType 類型決定選擇對應(yīng)數(shù)據(jù)庫內(nèi)置實現(xiàn)

? 實現(xiàn) ITypeConvert 接口自定義數(shù)據(jù)庫 字段類型 轉(zhuǎn)換為自己需要的 java 類型,內(nèi)置轉(zhuǎn)換類型無法滿足可實現(xiàn) IColumnType 接口自定義

url

  • 驅(qū)動連接的 URL

driverName

  • 驅(qū)動名稱

username

  • 數(shù)據(jù)庫連接用戶名

password

  • 數(shù)據(jù)庫連接密碼
package com.haoming;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;


public class ChengCode {
    public static void main(String[] args) {
        //構(gòu)建代碼生成器對象
        AutoGenerator mpg = new AutoGenerator();
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");//生成文件的輸出目錄
        gc.setAuthor("鼠皓明");//作者
        gc.setOpen(false);//是否打開輸出目錄
        gc.setFileOverride(false);//是否覆蓋已有的文件
        gc.setServiceName("%sService");//去除Service的I前綴
        gc.setIdType(IdType.ID_WORKER);//主鍵生成策略
        //ONLY_DATE 只使用 java.util.date 代替,SQL_PACK 使用 java.sql 包下的,TIME_PACK 使用 java.time 包下的 java8 新的時間類型
        gc.setDateType(DateType.TIME_PACK);//數(shù)據(jù)庫時間類型 到 實體類時間類型 對應(yīng)策略
        gc.setSwagger2(true);//開啟swagger2模式
        mpg.setGlobalConfig(gc);

        //2、數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSl=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);//數(shù)據(jù)庫類型
        mpg.setDataSource(dsc);

        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");//父包模塊名
        pc.setParent("com.cheng");//父包名,如果為空,將下面子包名必須寫全部, 否則就只需寫子包名
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig sy = new StrategyConfig();
        sy.setInclude("user");//設(shè)置要映射的表,可以設(shè)置多張
        sy.setNaming(NamingStrategy.underline_to_camel);//從數(shù)據(jù)庫表到文件的命名策略,下劃線轉(zhuǎn)駝峰命名
        sy.setColumnNaming(NamingStrategy.underline_to_camel);//列的命名策略
        sy.setEntityLombokModel(true);//開啟lombok支持
        sy.setLogicDeleteFieldName("deleted");//設(shè)置邏輯刪除字段
        sy.setVersionFieldName("version");//設(shè)置樂觀鎖
        sy.setRestControllerStyle(true);//開啟controller的restful命名
        sy.setControllerMappingHyphenStyle(true);//開啟controller中請求映射的連字符樣式,如:localhost:8080/hello_id_1
        //設(shè)置自動填充
        TableFill create_time = new TableFill("create_time", FieldFill.INSERT);
        TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(create_time);
        tableFills.add(update_time);
        mpg.setStrategy(sy);

        //執(zhí)行代碼生成器
        mpg.execute();
    }
}

執(zhí)行代碼生成器,查看項目結(jié)構(gòu)的變化

在這里插入圖片描述

代碼生成器執(zhí)行成功,自動生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼。

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

相關(guān)文章

最新評論