本文仅对使用MyBatis-Plus的代码生成器配置做保存,适合使用了该插件的童鞋做参考。
内部有大量默认配置,有性趣的童鞋可以研究下源码。
ps:官方文档更齐全http://mp.baomidou.com/

package com.kichun.ucenter.service;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.io.File;
import java.util.*; /**
* Created by wangqichang on 2018/6/1.
*/
public class MabatisPlusGenerator { //生成文件所在项目路径
private static String baseProjectPath = "D:\\Git\\strandrd_official_website\\kichun\\kichun-ucenter\\kichun-ucenter-entity"; //基本包名
private static String basePackage="com.kichun.ucenter";
//作者
private static String authorName="wangqichang";
//要生成的表名
private static String[] tables= {"t_role","t_resource","t_role_resource","t_user_role"};
//table前缀
private static String prefix="t_"; //数据库配置四要素
private static String driverName = "net.sf.log4jdbc.DriverSpy";
private static String url = "jdbc:log4jdbc:mysql://127.0.0.1:3306/kichun_dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true";
private static String username = "不告诉你";
private static String password = "密码也不告诉你"; public static void main(String[] args) { AutoGenerator gen = new AutoGenerator(); /**
* 数据库配置
*/
gen.setDataSource(new DataSourceConfig()
.setDbType(DbType.MYSQL)
.setDriverName(driverName)
.setUrl(url)
.setUsername(username)
.setPassword(password)
.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
// if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
// return DbColumnType.BOOLEAN;
// }
return super.processTypeConvert(fieldType);
}
})); /**
* 全局配置
*/
gen.setGlobalConfig(new GlobalConfig()
.setOutputDir( baseProjectPath + "/src/main/java")//输出目录
.setFileOverride(true)// 是否覆盖文件
.setActiveRecord(true)// 开启 activeRecord 模式
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(true)// XML ResultMap
.setBaseColumnList(true)// XML columList
.setOpen(false)//生成后打开文件夹
.setAuthor(authorName)
// 自定义文件命名,注意 %s 会自动填充表实体属性!
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("%sService")
.setServiceImplName("%sServiceImpl")
.setControllerName("%sController")
); /**
* 策略配置
*/
gen.setStrategy(new StrategyConfig()
// .setCapitalMode(true)// 全局大写命名
//.setDbColumnUnderline(true)//全局下划线命名
.setTablePrefix(new String[]{prefix})// 此处可以修改为您的表前缀
.setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
.setInclude(tables) // 需要生成的表
.setRestControllerStyle(true)
//.setExclude(new String[]{"test"}) // 排除生成的表
// 自定义实体父类
// .setSuperEntityClass("com.baomidou.demo.TestEntity")
// 自定义实体,公共字段
//.setSuperEntityColumns(new String[]{"test_id"})
//.setTableFillList(tableFillList)
// 自定义 mapper 父类 默认BaseMapper
//.setSuperMapperClass("com.baomidou.mybatisplus.mapper.BaseMapper")
// 自定义 service 父类 默认IService
// .setSuperServiceClass("com.baomidou.demo.TestService")
// 自定义 service 实现类父类 默认ServiceImpl
// .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")
// 自定义 controller 父类
//.setSuperControllerClass("com.kichun."+packageName+".controller.AbstractController")
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// .setEntityColumnConstant(true)
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// .setEntityBuilderModel(true)
// 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
.setEntityLombokModel(true)
// Boolean类型字段是否移除is前缀处理
// .setEntityBooleanColumnRemoveIsPrefix(true)
// .setRestControllerStyle(true)
// .setControllerMappingHyphenStyle(true)
); /**
* 包配置
*/
gen.setPackageInfo(new PackageConfig()
//.setModuleName("User")
.setParent(basePackage)// 自定义包路径
.setController("controller")// 这里是控制器包名,默认 web
.setEntity("entity")
.setMapper("dao")
.setService("service")
.setServiceImpl("service.impl")
.setXml("mapper")
); /**
* 注入自定义配置
*/
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List<FileOutConfig> fileOutList = new ArrayList<>();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
return baseProjectPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + ".xml";
}
});
abc.setFileOutConfigList(fileOutList);
gen.setCfg(abc); /**
* 指定模板引擎 默认是VelocityTemplateEngine ,需要引入相关引擎依赖
*/
gen.setTemplateEngine(new FreemarkerTemplateEngine()); /**
* 模板配置
*/
gen.setTemplate(
// 关闭默认 xml 生成,调整生成 至 根目录
new TemplateConfig().setXml(null)
// 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
// .setController("...");
// .setEntity("...");
// .setMapper("...");
// .setXml("...");
// .setService("...");
// .setServiceImpl("...");
); // 执行生成
gen.execute();
}
}

MyBatis-Plus-Generator配置的更多相关文章

  1. mybatis 三剑客 generator配置 、mybatis plugin

    generator配置 1.配置pom.xml 导入mysql驱动.mybatis.mybatis-generator的依赖 <dependency> <groupId>org ...

  2. Mybatis:Mybatis 逆向工程 generator配置

    一.使用Maven方式引入Mybatis依赖Jar包(版本号自己改或定义)

  3. mybatis generator配置生成代码的问题

    接触第二种orm两天下来,一脸懵逼.mybatis是大多数公司所推崇的,相比于hibernate性能较为好的,操作更为方便的轻量级工具,所以小富就搞起这个orm.好吧,都说mybatis有个配置可以自 ...

  4. MyBatis generator配置 overwrite 文件覆盖失效

    工具:IDEA.jdk1.8.mysql 底部有解决方法! pom.xml配置 <plugins> <!--Mybatis自动代码插入--> <plugin> &l ...

  5. mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

    mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...

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

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

  7. MBG(Mybatis Generator)配置

    配置需注意2点, 1.对于匹配所有表用%,多表配合使用_和%,这个和SQL Like查询模糊匹配方法一致 2.配置报错的话,提示如下:标黄的部分其实是正则表达式 The content of elem ...

  8. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  9. Spring Boot 数据访问集成 MyBatis 与事物配置

    对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...

  10. mybatis使用generator自己主动生成代码时的类型转换

    使用mybatis的generator自己主动生成代码,可是oracle数据库中number(6,2)总是自己主动转成BigDecimal.我想要转成的是float类型 这样就写了一个类型转换器,须要 ...

随机推荐

  1. 博客迁移到github了

    博客迁移到github了,这边基本不更新, 主要是没有找到快捷的同步方法,手动同步太麻烦了,如果你有快速把github博客同步到博客园的方法请一定告诉我

  2. Android接收RabbitMQ消息。

    参考:https://blog.csdn.net/qq_36576738/article/details/83754621 我这android这边就不实现发布消息功能.因为我是在服务端那边推送消息. ...

  3. Gevent模块,协程应用

    Gevent官网文档地址:http://www.gevent.org/contents.html 进程.线程.协程区分 我们通常所说的协程Coroutine其实是corporate routine的缩 ...

  4. 【转】Fuel-Openstack的搭建(一)

    原文链接:https://blog.csdn.net/qq_35180983/article/details/82181496 2.1安装前的准备操作: 首先,我们需要准备以下操作: 2.1.1 下载 ...

  5. python 安装pytorch 及 安装失败解决办法

    python 安装pytorch 及 安装失败解决办法 [转] pytorch安装失败解决办法 [转] 一分钟在win10终端成功安装pytorch pytorch 的安装方法有2种,一种是pip安装 ...

  6. python模块知识四 包和logging日志

    11.包 包:文件夹下具有__init__.py文件就是一个包,包用来管理多个模块 包的结构如下: bake ├── __init__.py ├── api ├── __init__.py ├── p ...

  7. (四)pdf的构成之文件体(树图)

    pdf的文件体类似于一个大树 有个根对象(catalog),该对象中保存着PDF的很多基本信息,并通过间接引用,辐射到所有的间接对象. (下图是大概的树形状)

  8. golang ---网卡信息

    package main import ( "fmt" "log" "net" "strings" ) type Net ...

  9. Hadoop1-认识Hadoop大数据处理架构

    一.简介概述 1.什么是Hadoop Hadoop是Apache软件基金会旗下的一个开源分布式计算平台,为用户提供了系统底层细节透明的分布式基础架构 Hadoop是基于java语言开发,具有很好的跨平 ...

  10. Abp session和Cookie

    问道 面向Abp 在面向服务的时候,Session 干嘛用? 先把Session 的作用说了,但是在微服务环境下给忽略了,相当于忽略了核心. Session 只是个功能.就是根据Cookie 的Ses ...