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

詳解使用Mybatis-plus + velocity模板生成自定義的代碼

 更新時(shí)間:2021年03月05日 14:08:18   作者:君君君sss  
這篇文章主要介紹了詳解使用Mybatis-plus + velocity模板生成自定義的代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

pom.xml文件的配置

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.3.0</version>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.1.0</version>
</dependency>

<!-- 代碼生成器模板 -->
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>
  <version>1.7</version>
</dependency>

CodeGenerator配置文件

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
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.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import lombok.extern.java.Log;

/**
 * 代碼生成器
 */
@Log
public class CodeGenerator {

 //項(xiàng)目存儲(chǔ)位置
 public static String PROJECT_GENERATE_DISK = "E:\\";
 //包名
 public static String PARENT_PACKAGE_NAME = "com";
 //包名
 public static String PACKAGE_NAME = "rent.security";
 //數(shù)據(jù)庫(kù)地址
 public static String DB_URL = "jdbc:mysql://localhost:3306/mp?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
 //數(shù)據(jù)庫(kù)實(shí)例名
 public static String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
 //數(shù)據(jù)庫(kù)用戶
 public static String USER = "root";
 //數(shù)據(jù)庫(kù)密碼
 public static String PASSWORD = "root";
 //數(shù)據(jù)庫(kù)schema
 public static String SCHEMA = "mp";
 //要查詢的表名
 public static String TABLE_NAMES = "sys_role_menu";
 //創(chuàng)建人
 public static String AUTHOR = "jmm";
 //是否強(qiáng)制帶上注解
 public static boolean ENABLE_TABLE_FIELD_ANNOTATION = false;
 //生成的注解帶上IdType類型
 public static IdType TABLE_IDTYPE = null;
 //生成的Service 接口類名是否以I開頭 默認(rèn)是以I開頭 user表 -> IUserService, UserServiceImpl
 public static boolean SERVICE_CLASS_NAME_START_WITHI = false;

 /**
 * 全局配置
 */
 private static GlobalConfig GlobalGenerate() {
 GlobalConfig config = new GlobalConfig();
 config.setActiveRecord(false)// 不需要ActiveRecord特性的請(qǐng)改為false
  .setIdType(TABLE_IDTYPE)
  .setEnableCache(false)// XML 二級(jí)緩存
  .setAuthor(AUTHOR)
  .setBaseResultMap(true)// XML ResultMap
  .setBaseColumnList(false)// XML columList
  .setOutputDir(PROJECT_GENERATE_DISK + "\\java" )
  .setFileOverride(true)
  .setControllerName("%sController" );//自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性!

 if (!SERVICE_CLASS_NAME_START_WITHI) {
  config.setServiceName("%sService" );
 }
 return config;
 }

 /**
 * 數(shù)據(jù)源配置
 */
 private static DataSourceConfig DaoSourceGenerate() {
 DataSourceConfig dataSourceConfig = new DataSourceConfig();
 DbType type = DbType.MYSQL;
 dataSourceConfig.setDbType(type)//數(shù)據(jù)庫(kù)類型
  .setUrl(DB_URL)//數(shù)據(jù)庫(kù)地址
  .setUsername(USER)//數(shù)據(jù)庫(kù)用戶名
  .setPassword(PASSWORD)//數(shù)據(jù)庫(kù)密碼
  .setDriverName(DRIVER_CLASS_NAME)//實(shí)例名
  .setSchemaName(SCHEMA);
 return dataSourceConfig;
 }

 /**
 * 策略配置
 */
 private static StrategyConfig StrategyGenerate() {
 StrategyConfig strategyConfig = new StrategyConfig();
 strategyConfig.setVersionFieldName("version" )
  .setCapitalMode(true)// 全局大寫命名 ORACLE 注意
  .setEntityLombokModel(false)
  .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
  .entityTableFieldAnnotationEnable(ENABLE_TABLE_FIELD_ANNOTATION)
  .setInclude(TABLE_NAMES)//修改替換成你需要的表名,多個(gè)表名傳數(shù)組
  .setEntityColumnConstant(true)// 【實(shí)體】是否生成字段常量(默認(rèn) false)public static final String ID = "test_id";
  .setEntityBuilderModel(true);// 【實(shí)體】是否為構(gòu)建者模型(默認(rèn) false)public User setName(String name) {this.name = name; return this;}
 return strategyConfig;
 }

 /**
 * 自定義模板配置
 */
 private static TemplateConfig TemplateGenerate() {
 TemplateConfig templateConfig = new TemplateConfig()
  .setController("templates/java/controller.java" )
  .setService("templates/java/service.java" )
  .setServiceImpl("templates/java/serviceImpl.java" )
  .setMapper("templates/java/mapper.java" );
 return templateConfig;
 }

 private static final String MODULE_NAME = "sysRoleMenu";

 private static final String CLASS_NAME = "SysRoleMenu";

 private static final String class_name = "sysRoleMenu";

 /**
 * 自定義文件及key
 */
 private static InjectionConfig FileGenerate() {
 InjectionConfig injectionConfig = new InjectionConfig() {
  @Override
  public void initMap() {//自定義參數(shù)
  Map<String, Object> map = new HashMap<>();
  //這些自定義的值在vm模板的語(yǔ)法是通過${cfg.xxx}來(lái)調(diào)用的。
  map.put("moduleName", MODULE_NAME);
  map.put("ClassName", CLASS_NAME);
  map.put("className", class_name);
  map.put("packageName", PARENT_PACKAGE_NAME + "." + PACKAGE_NAME);
  map.put("author", AUTHOR);
  map.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  this.setMap(map);
  }
 };
 return injectionConfig;
 }

 /**
 * 包配置
 */
 public static PackageConfig PackageGenerate() {
 PackageConfig pc = new PackageConfig().setParent("com" )
  .setModuleName(PACKAGE_NAME)
  .setController("controller")
  .setEntity("domain")
  .setMapper("dao")
  .setXml("mapper");
 return pc;
 }

 public static void main(String[] args) {
 //全局配置
 GlobalConfig config = GlobalGenerate();
 //配置數(shù)據(jù)源
 DataSourceConfig dataSourceConfig = DaoSourceGenerate();
 //配置策略
 StrategyConfig strategyConfig = StrategyGenerate();
 //配置模板
 TemplateConfig templateConfig = TemplateGenerate();
 //自定義值
 InjectionConfig injectionConfig = FileGenerate();
 //配置包
 PackageConfig packageConfig = PackageGenerate();
 //生成代碼
 new AutoGenerator().setGlobalConfig(config)
  .setTemplate(templateConfig)//自定義模板路徑
  .setCfg(injectionConfig)
  .setDataSource(dataSourceConfig)
  .setStrategy(strategyConfig)
  .setPackageInfo(packageConfig)
  .execute();
 }
}

一定要注意!?。njectionConfig方法中自定義的Map返回對(duì)象可以返回給模板調(diào)用,調(diào)用方法是${cfg.xxx}

vm模板代碼實(shí)例

package ${package.Controller}; //這樣寫是依據(jù)步驟2的PackageConfig方法配置

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ${package.Entity}.${cfg.ClassName};
import ${package.Service}.${cfg.ClassName}Service;
import com.rent.model.AjaxResult;

/**
 * ${cfg.moduleName}Controller
 *
 * @author ${author}
 * @date ${cfg.datetime}
 */
@Controller
@RequestMapping("/${cfg.moduleName}/${cfg.className}")
public class ${cfg.ClassName}Controller extends BaseController

  @Autowired
  private ${cfg.ClassName}Service ${cfg.className}Service;

  /**
   * 查詢${cfg.className}列表
   */
  @GetMapping("/list")
  public TableDataInfo list(@RequestParam int pageNumber, @RequestParam int pageSize) {

 Page<${cfg.ClassName}> page = ${cfg.className}Service.select${cfg.ClassName}Page(pageNumber, pageSize);
 return page;
  }

  /**
   * 新增保存${cfg.ClassName}
   */
  @PostMapping("/add${cfg.ClassName}")
  public AjaxResult addSave(${cfg.ClassName} ${cfg.ClassName}) {

 boolean ret = ${cfg.ClassName}Service.save(${cfg.ClassName});
 return new AjaxResult(ret, ret ? "成功" : "失敗");
  }

  /**
   * 修改保存${cfg.ClassName}
   */
  @PostMapping("/edit")
  public AjaxResult editSave(${cfg.ClassName} ${cfg.ClassName}) {

 boolean ret = ${cfg.ClassName}Service.updateById(${cfg.ClassName});
 return new AjaxResult(ret, ret ? "成功" : "失敗");
  }

 @PostMapping("deleteById")
 @ApiOperation("根據(jù)ID刪除${cfg.ClassName}")
 public AjaxResult<${cfg.ClassName}> deleteById(Long id) {
 boolean ret = ${cfg.ClassName}Service.removeById(id);
 return new AjaxResult(true, ret ? "成功" : "失敗");
 }

 @PostMapping("findById")
 @ApiOperation("根據(jù)ID查詢${cfg.ClassName}")
 public AjaxResult<${cfg.ClassName}> findById(Long id) {
 ${cfg.ClassName} user = ${cfg.ClassName}Service.getById(id);
 return new AjaxResult(true, "成功", user);
 }
}

最后如果需要更詳細(xì)的vm調(diào)用請(qǐng)參閱官方文檔:https://baomidou.com/config/generator-config.html#datasource

到此這篇關(guān)于詳解使用Mybatis-plus + velocity模板生成自定義的代碼的文章就介紹到這了,更多相關(guān)Mybatis-plus velocity生成自定義代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java多線程之Future設(shè)計(jì)模式

    Java多線程之Future設(shè)計(jì)模式

    這篇文章主要介紹了Java多線程之Future設(shè)計(jì)模式,F(xiàn)uture 代表的是未來(lái)的一個(gè)憑據(jù),文章主要附上Future具體實(shí)現(xiàn)類、橋接Future和FutureTask的代碼,需要的朋友可以參考一下
    2021-10-10
  • Java編程之多線程死鎖與線程間通信簡(jiǎn)單實(shí)現(xiàn)代碼

    Java編程之多線程死鎖與線程間通信簡(jiǎn)單實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Java源碼解析之Gateway請(qǐng)求轉(zhuǎn)發(fā)

    Java源碼解析之Gateway請(qǐng)求轉(zhuǎn)發(fā)

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Gateway請(qǐng)求轉(zhuǎn)發(fā)展開,文中有非常詳細(xì)介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信

    Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信

    本文主要介紹了Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信,包含實(shí)現(xiàn)實(shí)時(shí)消息傳遞和群發(fā)消息等功能,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • 淺談springfox-swagger原理解析與使用過程中遇到的坑

    淺談springfox-swagger原理解析與使用過程中遇到的坑

    本篇文章主要介紹了淺談springfox-swagger原理解析與使用過程中遇到的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-02-02
  • Spring Boot 表單驗(yàn)證篇

    Spring Boot 表單驗(yàn)證篇

    在表單提交的時(shí)候,我們需要進(jìn)行驗(yàn)證。下面通過本篇文章給大家介紹Spring Boot 表單驗(yàn)證篇,需要的朋友可以參考下
    2017-08-08
  • Java生成動(dòng)態(tài)版驗(yàn)證碼的方法實(shí)例

    Java生成動(dòng)態(tài)版驗(yàn)證碼的方法實(shí)例

    這篇文章主要給大家介紹了利用Java生成動(dòng)態(tài)版驗(yàn)證碼的方法實(shí)例,本文生成的是GIF格式 + 干擾元素,讓驗(yàn)證碼破解難度又上了一個(gè)層次,文中給出了詳細(xì)的示例代碼,并在文末給出了完整的實(shí)例代碼供大家下載學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。
    2017-04-04
  • 關(guān)于Springboot的日志配置

    關(guān)于Springboot的日志配置

    Spring Boot默認(rèn)使用LogBack日志系統(tǒng),如果不需要更改為其他日志系統(tǒng)如Log4j2等,則無(wú)需多余的配置,LogBack默認(rèn)將日志打印到控制臺(tái)上,需要的朋友可以參考下
    2023-05-05
  • 深入淺析Mybatis的缺陷問題

    深入淺析Mybatis的缺陷問題

    Mybatis是業(yè)界非常流行的持久層框架,輕量級(jí)、易用,在金融IT領(lǐng)域完全是領(lǐng)軍地位,比Hibernate更受歡迎,優(yōu)勢(shì)非常多,也是非常值得我們學(xué)習(xí)的。這篇文章主要介紹了Mybatis的缺陷問題的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • java中實(shí)現(xiàn)分頁(yè)的幾種常見方式總結(jié)

    java中實(shí)現(xiàn)分頁(yè)的幾種常見方式總結(jié)

    在項(xiàng)目中經(jīng)常會(huì)查詢大量數(shù)據(jù),這就要用到分頁(yè)展示,下面這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)分頁(yè)的幾種常見方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12

最新評(píng)論