依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
package com.fht.passing.generator.mybatis;
import com.fht.passing.generator.mybatis.mybatis.GeneratorConfig;
import com.fht.passing.generator.mybatis.mybatis.GeneratorFactory;
/**
* MyBatis CodeGenerator代码生成器
*
* @author Weiping.wang
* @since 2021-04-29
* @version
*/
public class MyBatisGenerator {
/**
* MyBatis代码生成
*/
public static void main(String[] args) {
GeneratorConfig generatorConfig = new GeneratorConfig();
// Mysql数据库连接URL
generatorConfig.setDatabaseUrl("jdbc:mysql://192.168.1.10:3306/fht-passing?SSL=false");
// Mysql数据库用户名
generatorConfig.setDatabaseUsername("fht");
// Mysql数据库密码
generatorConfig.setDatabasePassword("fht123");
// 开发者姓名
generatorConfig.setAuthor("Boxing");
// 包名
generatorConfig.setPackagePath("com.fht.passing");
// 模块名称
generatorConfig.setModel("visitor");
// 表名,多个用","隔开
generatorConfig.setTables("visit_record");
// Mapper xml文件存储路径
generatorConfig.setMapperXmlPathPrefix("/src/test/resources/mybatis/mapper/visitor/");
//执行
new GeneratorFactory().generate(generatorConfig);
}
}
package com.fht.passing.generator.mybatis.mybatis;
/**
* MyBatis Generator代码生成器配置文件
*
* @author Weiping.wang
* @since 2021-04-29
* @version
*/
public class GeneratorConfig {
/**
* 数据库驱动
*/
private final String databaseDriver = "com.mysql.jdbc.Driver";
/**
* 数据库连接URL
*/
private String databaseUrl;
/**
* Mysql用户名
*/
private String databaseUsername;
/**
* Mysql密码
*/
private String databasePassword;
/**
* 开发者
*/
private String author;
/**
* 模块包名
*/
private String packagePath;
/**
* 模块名称
*/
private String model;
/**
* 表名
*/
private String tables;
/**
* mapper xml存储路径
*/
private String mapperXmlPathPrefix;
public String getDatabaseDriver() {
return databaseDriver;
}
public String getDatabaseUrl() {
return databaseUrl;
}
public void setDatabaseUrl(String databaseUrl) {
this.databaseUrl = databaseUrl;
}
public String getDatabaseUsername() {
return databaseUsername;
}
public void setDatabaseUsername(String databaseUsername) {
this.databaseUsername = databaseUsername;
}
public String getDatabasePassword() {
return databasePassword;
}
public void setDatabasePassword(String databasePassword) {
this.databasePassword = databasePassword;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getPackagePath() {
return packagePath;
}
public void setPackagePath(String packagePath) {
this.packagePath = packagePath;
}
public String getTables() {
return tables;
}
public void setTables(String tables) {
this.tables = tables;
}
public String getMapperXmlPathPrefix() {
return mapperXmlPathPrefix;
}
public void setMapperXmlPathPrefix(String mapperXmlPathPrefix) {
this.mapperXmlPathPrefix = mapperXmlPathPrefix;
}
}
package com.fht.passing.generator.mybatis.mybatis;
import com.baomidou.mybatisplus.enums.FieldFill;
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.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 com.fht.passing.generator.mybatis.MyBatisGenerator;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* MyBatis Generator代码生成器
*
* @author Weiping.wang
* @since 2021-04-29
*/
@Component
public class GeneratorFactory {
public void generate(GeneratorConfig generatorConfig) {
String filePath = MyBatisGenerator.class.getClassLoader().getResource("").getPath();
filePath = filePath.substring(1, filePath.indexOf("/build"));
filePath = "/" + filePath + "/";
final String xmlPath = filePath;
int result = 1;
// 自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE));
// 代码生成器
AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
// 全局配置
new GlobalConfig()
.setOutputDir(filePath + "/src/test/java")
.setFileOverride(false)
.setActiveRecord(true)
.setEnableCache(false)
.setBaseResultMap(true)
.setBaseColumnList(true)
.setAuthor(generatorConfig.getAuthor())
// 自定义文件命名,注意 %s 会自动填充表实体属性!
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("%sRepository")
.setServiceImplName("%sRepositoryImpl")
.setControllerName("%sController")
).setDataSource(
// 数据源配置
new DataSourceConfig()
.setDbType(DbType.MYSQL)
.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
return super.processTypeConvert(fieldType);
}
})
.setDriverName(generatorConfig.getDatabaseDriver())
.setUsername(generatorConfig.getDatabaseUsername())
.setPassword(generatorConfig.getDatabasePassword())
.setUrl(generatorConfig.getDatabaseUrl())
).setStrategy(
new StrategyConfig()
.setNaming(NamingStrategy.underline_to_camel)
.setInclude(generatorConfig.getTables().split(","))
.setTableFillList(tableFillList)
.setRestControllerStyle(true)
.setEntityLombokModel(true)
.setRestControllerStyle(true)
).setPackageInfo(
new PackageConfig()
.setParent(generatorConfig.getPackagePath())
.setController(generatorConfig.getModel() + ".web")
.setMapper(generatorConfig.getModel() + ".infrastructure.persistence.mapper")
.setEntity(generatorConfig.getModel() + ".infrastructure.persistence.entity")
.setService(generatorConfig.getModel() + ".domain.model")
.setServiceImpl(generatorConfig.getModel() + ".infrastructure.persistence.repository")
).setCfg(
new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
}.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
"/templates/mapper.xml" + ((1 == result) ? ".ftl" : ".vm")) {
@Override
public String outputFile(TableInfo tableInfo) {
return xmlPath + generatorConfig.getMapperXmlPathPrefix() + "/" + tableInfo.getEntityName() + "Mapper.xml";
}
}))
).setTemplate(
new TemplateConfig().setXml(null)
);
// 执行生成
if (result == 1) {
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();
}
}