原文:
https://www.jianshu.com/p/8bf62ad92f54
https://mp.baomidou.com/config/generator-config.html#datetype
pom.xml的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
核心代码
package com.example.test5.support;
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.io.File;
import java.util.ArrayList;
import java.util.List;
public class class2 {
private static String projectPath = null;
public static void main(String[] args) {
projectPath = System.getProperty("user.dir");
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(configGlobalConfig());
autoGenerator.setDataSource(configDataSource());
autoGenerator.setStrategy(configStrategyConfig());
autoGenerator.setPackageInfo(configPackageConfig());
autoGenerator.setCfg(customerConfig());
autoGenerator.execute();
}
/**
* 自定义生成文件配置
*
* @return
*/
private static InjectionConfig customerConfig() {
InjectionConfig config = new InjectionConfig() {
@Override
public void initMap() {
}
};
List<FileOutConfig> files = new ArrayList<FileOutConfig>();
files.add(new FileOutConfig("/templates/template.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
String expand = "c:/pms/expand";
String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());
return entityFile;
}
});
config.setFileOutConfigList(files);
return config;
}
/**
* 全局配置
*
* @return
*/
private static GlobalConfig configGlobalConfig() {
GlobalConfig config = new GlobalConfig();
// 是否支持AR模式
config.setActiveRecord(true)
// 作者
.setAuthor("test@company.com")
// 生成路径
// .setOutputDir(projectPath + "/src/main/java/")
.setOutputDir("c:/pms/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);
return config;
}
/**
* 数据源配置
*
* @return
*/
private static DataSourceConfig configDataSource() {
DataSourceConfig dsConfig = new DataSourceConfig();
// 设置数据库类型
dsConfig.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8")
.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);
}
});
return dsConfig;
}
/**
* 策略配置
*
* @return
*/
private static StrategyConfig configStrategyConfig() {
StrategyConfig stConfig = new StrategyConfig();
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);
// 全局大写命名
stConfig.setCapitalMode(true)
// 指定表名 字段名是否使用下划线
//.setDbColumnUnderline(true)
// 数据库表映射到实体的命名策略
.setNaming(NamingStrategy.underline_to_camel)
//.setTablePrefix("tbl_")
// 生成的表
.setInclude(new String[]{
"pms_brand"
})
.setEntityBooleanColumnRemoveIsPrefix(false)
// 自定义实体,公共字段
.setTableFillList(tableFillList);
return stConfig;
}
/**
* 包名策略配置
* @return
*/
private static PackageConfig configPackageConfig() {
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("com.example.test5.pms")
//dao
.setMapper("dao")
//service
.setService("service")
//controller
.setController("controller")
.setEntity("entity")
//mapper.xml
.setXml("mapper");
return pkConfig;
}
}
模板
#foreach($field in $!{table.fields})
#if($!{field.name}!=$!{cfg.tenantColumn})
{
label: "$!{field.comment}",
prop: "$!{field.propertyName}",
rules: [{
required: true,
message: "请输入$!{field.comment}",
trigger: "blur"
}]
},
#end
#end
.