package com.company.project.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
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.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MyBatisPlusGeneratorAnt { public static void main(String[] args) { String projectPath = System.getProperty("user.dir"); // 自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<TableFill>();
//如 每张表都有一个创建时间、修改时间
//而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
//修改时,修改时间会修改,
//虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
//使用公共字段填充功能,就可以实现,自动按场景更新了。
//如下是配置
TableFill sysCreateTime = new TableFill("create_time", FieldFill.INSERT);
TableFill sysUpdateTime = new TableFill("update_time", FieldFill.UPDATE);
TableFill sysCreateBy = new TableFill("create_by", FieldFill.INSERT);
TableFill sysUpdateBy = new TableFill("update_by", FieldFill.UPDATE);
tableFillList.add(sysCreateTime);
tableFillList.add(sysUpdateTime);
tableFillList.add(sysCreateBy);
tableFillList.add(sysUpdateBy); // 1. 全局配置
GlobalConfig config = new GlobalConfig();
// 是否支持AR模式
config.setActiveRecord(true)
// 作者
.setAuthor("test@company.com")
// 生成路径
.setOutputDir(projectPath + "/src/main/java/")
// 文件覆盖
.setFileOverride(true)
// 主键策略
.setIdType(IdType.AUTO)
// 设置生成的service接口的名字的首字母是否为I,例如IEmployeeService
.setServiceName("%sService")
//生成基本的resultMap
.setBaseResultMap(true)
//生成基本的SQL片段
.setBaseColumnList(true)
//生成后打开文件夹
.setOpen(false).setDateType(DateType.ONLY_DATE); // 2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
// 设置数据库类型
dsConfig.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/test")
.setUsername("root")
.setPassword("root")
.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
System.out.println("转换类型:" + fieldType);
if (fieldType.toLowerCase().contains("tinyint")) {
return DbColumnType.INTEGER;
}
return super.processTypeConvert(globalConfig, fieldType);
}
}); // 3. 策略配置globalConfiguration中
StrategyConfig stConfig = new StrategyConfig();
// 全局大写命名
stConfig.setCapitalMode(true)
// 指定表名 字段名是否使用下划线
//.setDbColumnUnderline(true)
// 数据库表映射到实体的命名策略
.setNaming(NamingStrategy.underline_to_camel)
//.setTablePrefix("tbl_")
// 生成的表
.setInclude(new String[] {
"table_teacher","table_student"
})
.setEntityBooleanColumnRemoveIsPrefix(false)
// 自定义实体,公共字段
.setTableFillList(tableFillList); // 4. 包名策略配置
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.company.project.api")
//dao
.setMapper("repository.mysql.mapper")
//servcie
.setService("service")
//controller
.setController("web")
.setEntity("repository.mysql.domain")
//mapper.xml
.setXml("repository.mysql.mybatis"); // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
InjectionConfig cfg = 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> focList = new ArrayList<>();
// 调整xml生成目录演示
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/META-INF/mybatis/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList); // 关闭默认生成,如果设置空 OR Null 将不生成该模块。
TemplateConfig tc = new TemplateConfig();
tc.setController(null);
tc.setXml(null); // 5. 整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig)
.setCfg(cfg)
.setTemplate(tc); // 6. 执行
ag.execute();
} }

最新文章

  1. SQL Server 服务器磁盘测试之SQLIO篇(一)
  2. JQuery mobile中按钮自定义属性的改变
  3. [redis] Node is not empty. Either the node already knows other nodes
  4. centos 7.2 网卡配置文件 及 linux bridge的静态配置
  5. (转)HBase工程师线上工作经验总结----HBase常见问题及分析
  6. C++静态成员和静态成员函数
  7. 如何做一个avalon组件
  8. shell脚本学习--shell中的变量$
  9. Codevs 2837 考前复习
  10. nullptr和NULL
  11. git常用命令行
  12. CKplayer 新手入门超简单使用教程
  13. [转] git config命令使用第一篇——介绍,基本操作,增删改查
  14. Tkinter 导入安装包
  15. PHP Session的优化使用
  16. Android实时取景:用SurfaceView实现
  17. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]
  18. Day2 数据类型和运算符
  19. Python完成RF测试用例
  20. Selenium2+python自动化65-js定位几种方法总结【转载】

热门文章

  1. Web Service简介与开发实例
  2. 蓝鲸-监控 排错思路 - 原理push - bkdata报错 - saas的日志
  3. nginx - 反向代理 - 配置文件 header - 日志log格式
  4. 登录进入Mysql数据库的几种方式
  5. 算法 - k-means++
  6. node.js中的url.parse方法使用说明
  7. 【扩展GCD】荒岛野人
  8. 基于rabbitmq的Spring-amqp基本使用
  9. python并发编程-进程理论-进程方法-守护进程-互斥锁-01
  10. 附录1:arrayanalysis的本地使用(质量控制)