mybatis plus generator配置

代码生成器AutoGenerator

AutoGenerator autoGenerator = new AutoGenerator();

GlobalConfig

全局配置,定义文件的输出目录,设置文件的@author信息等公共配置

GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)
gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)
gc.setAuthor("yourName");// 开发人员(默认值:null)
gc.setOpen(false);// 是否打开输出目录(默认值:null)
gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)
autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类

DataSourceConfig

DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://xxx.xxx.x.x:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8");
//dsc.setSchemaName("public");// 数据库方案名
dsc.setDbType(DbType.MYSQL);// 数据库类型
dsc.setDriverName("com.mysql.cj.jdbc.Driver");// 驱动名称
dsc.setUsername("xxx"); // 用户名
dsc.setPassword("xxx"); // 密码autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类

PackageConfig

包配置,就是每个类最上面的package,比如:package com.ck.mybatisplus.foundation.service;这里包配置可以分别配置service、entity、controller等等

PackageConfig pc = new PackageConfig();
pc.setModuleName("foundation");// 父包模块名
pc.setParent("com.ck.mybatisplus");// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setService("service");// Service包名
pc.setEntity("entity");// Entity包名
pc.setServiceImpl("service.impl");// ServiceImpl包名
pc.setMapper("mapper");// Mapper包名
pc.setController("controller");// Contoller包名
// pc.setXml("mapper.xml");// Mapper.xml包名
mpg.setPackageInfo(pc);// 把包配置添加到代码生成器主类

TemplateConfig

方法一

设置自定义模板路径,文件配置信息使用默认的。(如:生成的文件名,生成的文件路径等),一般都是用mybatis plus中自带的模板

//不用带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setEntity("templates/entity.java");
templateConfig.setService("templates/service.java");
templateConfig.setController();

方法二

使用自定义输出配置


// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}
});

StrategyConfig

一些策略的配置

// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(INCLUDE_TABLES.split(","));
strategy.setControllerMappingHyphenStyle(true);
//  strategy.setTablePrefix(pc.getModuleName() + "_");
autoGenerator.setStrategy(strategy);
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());

生成器工具类

自己写的一个生成器工具类,可以参考一下,根据自己的需求进行修改

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.*;/*** @author wenyao*/
public class CodeGenerator {private final static AutoGenerator autoGenerator = new AutoGenerator();//表名映射(','号分割符)private static String INCLUDE_TABLES = "user";//private static String INCLUDE_TABLES = "user,home";//包名配置private static String CONTROLLER = "controller";private static String SERVICE = "service";private static String SERVICEIMPL = "service.Impl";private static String MAPPER = "mapper";private static String ENTITY = "entity";private static String projectPath = System.getProperty("user.dir");// 获取用户程序当前路径(项目根的路径)private static String ParentName = "com.wenyao";private static String ModuleName = "";//公共全局private static String AUTHOR = "wenyao";//数据源配置private static String DB_USERNAME = "root";private static String DB_PASSWORD = "xxx";private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";private static String DB_URL = "jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";//xml模板文件路径private static String TEMPLATEPATH = "/templates/mapper.xml.ftl";//配置private static GlobalConfig gc = new GlobalConfig();private static DataSourceConfig dsc = new DataSourceConfig();private static PackageConfig pc = new PackageConfig();private static InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};private static TemplateConfig templateConfig = new TemplateConfig();private static StrategyConfig strategy = new StrategyConfig();/*** 初始化全局配置*/private static void initGlobalConfig(){// 全局配置gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的输出目录(默认值:D 盘根目录)gc.setAuthor(AUTHOR);// 开发人员(默认值:null)gc.setOpen(false);// 是否打开输出目录(默认值:null)gc.setFileOverride(true);// 是否覆盖已有文件(默认值:false)gc.setSwagger2(true);// 实体属性 Swagger2 注解gc.setBaseResultMap(true); //在xml中生成BaseResultMapgc.setBaseColumnList(true); //autoGenerator.setGlobalConfig(gc);//把全局配置添加到代码生成器主类}/*** 初始化数据源配置*/private static void initDataSourceConfig(){// 数据源配置dsc.setUrl(DB_URL);//dsc.setSchemaName("public");// 数据库方案名dsc.setDbType(DbType.MYSQL);// 数据库类型dsc.setDriverName(DB_DRIVER);// 驱动名称dsc.setUsername(DB_USERNAME); // 用户名dsc.setPassword(DB_PASSWORD); // 密码autoGenerator.setDataSource(dsc);//把数据源配置添加到代码生成器主类}/*** 初始化包配置*/private static void initPackageConfig(){// 包配置pc.setModuleName(ModuleName);// 父包模块名pc.setParent(ParentName);// 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名pc.setService(SERVICE);// Service包名pc.setEntity(ENTITY);// Entity包名pc.setServiceImpl(SERVICEIMPL);// ServiceImpl包名pc.setMapper(MAPPER);// Mapper包名pc.setController(CONTROLLER);// Contoller包名// pc.setXml("mapper.xml");// Mapper.xml包名(自定义配置到resource下,所以这里不使用默认路径)autoGenerator.setPackageInfo(pc);// 把包配置添加到代码生成器主类}/*** 初始化自定义配置* 这里配置生成的mapper.xml文件的自定义输出路径(默认不是输出到resources文件夹下,所以需要自定义)*/private static void initInjectionConfig(){// 自定义配置// 自定义输出配置,自定义配置会被优先输出List<FileOutConfig> focList = new ArrayList<>();focList.add(new FileOutConfig(TEMPLATEPATH) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);autoGenerator.setCfg(cfg);}/*** 初始化模板配置*/private static void initTemplateConfig(){// 配置模板templateConfig.setXml(null);autoGenerator.setTemplate(templateConfig);}/*** 生成策略初始化*/private static void initStrategyConfig(){// 策略配置strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);// 公共父类// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");// 写于父类中的公共字段// strategy.setSuperEntityColumns("id");strategy.setInclude(INCLUDE_TABLES.split(","));strategy.setControllerMappingHyphenStyle(true);//  strategy.setTablePrefix(pc.getModuleName() + "_");autoGenerator.setStrategy(strategy);autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());}public static void init(){initGlobalConfig();initDataSourceConfig();initPackageConfig();initInjectionConfig();initTemplateConfig();initStrategyConfig();}public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {init();autoGenerator.execute();}}

mybatis plus generator配置相关推荐

  1. generator自动生成mybatis的xml配置

    generator自动生成mybatis的xml配置.model.map等信息: 1.下载mybatis-generator-core-1.3.2.jar包.        网址:http://cod ...

  2. MyBatis Plus Generator——MyBatis Plus代码生成器DEMO

    官方文档 https://mp.baomidou.com/guide/generator.html Maven <dependency><groupId>mysql</g ...

  3. Mybatis基于XML配置SQL映射器(一)

    Durid和Mybatis开发环境搭建 SpringBoot搭建基于Spring+SpringMvc+Mybatis的REST服务(http://www.cnblogs.com/nbfujx/p/76 ...

  4. 用mybatis的generator自动生成代码--坑我都走了一遍,后面的同学别踩了

    先说什么是mybatis-generator? mybatis-geneator是一款mybatis自动代码生成工具,可以通过配置,快速生成mapper和xml文件. 步骤一:在pom文件中添加插件配 ...

  5. 如何用MyBatis-Generator自动创建代码(映射生成实体类、DAO接口和Mapping映射文件)

    如何用MyBatis自动生成实体类.DAO接口和Mapping映射文件 引言: 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBa ...

  6. IDEA使用mybatis实现generator自动生成MSSQLSERVER数据库表映射

    IDEA使用mybatis实现generator自动生成MSSQLSERVER数据库表映射,generatorConfig.xml文件中配置如下内容: <?xml version="1 ...

  7. MyBatis简介与配置MyBatis+Spring+MySql

    一.MyBatis简介与配置MyBatis+Spring+MySql 原文出自:http://limingnihao.iteye.com/blog/781671 MyBatis学习 之 一.MyBat ...

  8. MyBatis多数据源配置(读写分离)

    MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用起来可能会很麻烦. 实际应用中可能存在多种结合的情况,你可以理解本文的含义,不要死板的使用. 多数据源的可能情况 ...

  9. MyBatis Plus Generator——基于Velocity的Controller参考模板(集成MyBatis Plus、Swagger2、自封装Response、分页)

    代码生成器 MyBatis Plus Generator--MyBatis Plus代码生成器DEMO 解决方案 包含:MyBatis Plus.Swagger2.自封装Response.MyBati ...

最新文章

  1. 5天玩转PyTorch深度学习,从GAN到词嵌入都有实例丨教程资源
  2. Leetcode199二叉树右视图[C++题解]:BFS+层数
  3. Faster-RCNN训练自己数据集遇到的问题集锦
  4. 父框架与子框架的互操作
  5. 3-15 《元编程》第6章 3-16 hook method
  6. yii urlmanager配置post不生效_一文带你彻底学会 Git Hooks 配置
  7. java操作字符串——CSDN博客
  8. TortoiseGit 冲突和解决方案_入门试炼_07
  9. Cell Research | 单细胞测序技术揭示派杰氏病的致病机制
  10. Overview of HEVC之2 Slices and Tiles
  11. 排名如何得到快速提升?
  12. 【日常吐槽 · 第七期】进击的博客
  13. LeCo-83.删除排序链表中的重复元素
  14. 在vsphere client 给esxi上的虚拟机增加U盘识别
  15. 电脑网易云音乐,网易云音乐的话题区到底有多魔性?
  16. 产业转型季运营商现离职潮
  17. 计算机图像处理实验二 图像直方图及灰度变换
  18. 【Unity2D游戏】实现实时的正确的遮挡关系(引擎自带功能)
  19. 【jzoj4763】【旷野大计算】【莫队】
  20. 深度式睡眠潜入虚拟世界_潜入swiftui的惊人世界

热门文章

  1. ExtJs6 理解 -- Ext.data.proxy.Proxy
  2. 图片轮播,纯js+css
  3. centos6.4 搭建knowlededgeroot-1.0.4知识库平台
  4. 如果再不要求进步,那么你就是下一个陨落的巨头
  5. 经典网页设计:18个示例展示图片在网页中的使用
  6. 【Iphone 游戏开发】游戏引擎剖析
  7. Task和async/await详解
  8. C# 关闭进程的时候总是捕捉到System.Threading.ThreadAbortException: 正在中止线程
  9. webapi同一个Controller多个函数
  10. WCF4.0 –- RESTful WCF Services (1) (入门)