日常使用中需要访问数据库生成对应文件,现在使用代码实现:
package com.originspace.geo;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.mysql.jdbc.Driver;
import java.util.ArrayList;
import java.util.List;
public class CodeGenerator4Mysql {
public static void main(String[] args) {
CodeGenerator4Mysql test = new CodeGenerator4Mysql();
//包名 表名数组
test.generateByTables("com.space.original", "f_project","f_upload_file","g_color_info" ,"g_layer","r_area","r_monitor");
}
private void generateByTables(String packageName, String... tableNames) {
GlobalConfig config = new GlobalConfig();
String dbUrl = "jdbc:mysql://192.168.120.8:3306/data?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername("root")
.setPassword("Zxcvbnm123.")
.setDriverName(Driver.class.getName());
StrategyConfig strategyConfig = new StrategyConfig();
List<TableFill> tableFillList = new ArrayList<TableFill>() {
private static final long serialVersionUID = 1L;
{
add(new TableFill("CREATED_BY", FieldFill.INSERT));
add(new TableFill("CREATED_AT", FieldFill.INSERT));
add(new TableFill("UPDATED_BY", FieldFill.UPDATE));
add(new TableFill("UPDATED_AT", FieldFill.UPDATE));
}
};
strategyConfig
.setRestControllerStyle(true)
.setEntityBuilderModel(true)
.setEntityLombokModel(true)
.setCapitalMode(true)
.setEntityLombokModel(true)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setEntityTableFieldAnnotationEnable(true)
// .setTablePrefix("t_origin_")
.setLogicDeleteFieldName("del_flag")
// .setVersionFieldName("VERSION")
.setTableFillList(tableFillList)
.setFieldPrefix(new String[] {})//test_id -> id, test_type -> type
.setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组
config.setActiveRecord(false)
.setBaseResultMap(true)
.setBaseColumnList(true)
.setActiveRecord(true)
.setSwagger2(true)
// .setAuthor("guofeng")
.setOutputDir("src\\main\\java")
// .setOutputDir("d:/data/auto")
.setFileOverride(true);
config.setServiceName("%sService");
TemplateConfig templateConfig = new TemplateConfig().setEntity("templates/entity.java");
new AutoGenerator().setGlobalConfig(config)
.setTemplate(templateConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(
new PackageConfig()
.setParent("")
.setController(packageName + ".controller")
.setEntity(packageName + ".model.po")
.setMapper(packageName + ".mapper")
.setService(packageName + ".service")
.setServiceImpl(packageName + ".service.impl")
.setXml("mapper")
).execute();
}
}