需要引用的jar
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
/**
-
@Author zbf
-
@DATA 2019/10/8 16:48
-
@ClassIntroduction
*/
public class CodeGenerator {public static void main(String[] args) {
String[] tables = {"system_quartz","system_quartz_info"}; String parentMapper="/src/main/java/com/qdwy/qudaowuyouservice/mapper/feedback"; String item_model="/qudaowuyou-service";//model String mapper_url="/src/main/resources/mapper/feedback"; String bean_moedel="/qudaowuyou-entity"; //实体 String bean_url="/src/main/java/com/qdwy/qudaowuyouentity/entity/feedback"; String db_url="jdbc:mysql://47.106.192.29:3306/qdwy_feedback?autoReconnect=true"; String db_driver="com.mysql.cj.jdbc.Driver"; String db_user="root"; String db_password="qudaowuyou201951@#$&*"; GeneratorUtils.genertatorUilt(tables, item_model,mapper_url, db_url, db_driver, db_user, db_password,bean_moedel,bean_url,parentMapper);
}
}
/**
-
@Author zbf
-
@DATA 2019/10/14 21:01
-
@ClassIntroduction
*/
public class GeneratorUtils {/**
*-
@param tables 表名
-
@param item_model 需要放在哪一个model下
-
@param mapperUrl mapper文件存放地址
-
@param db_url 数据库的链接url
-
@param db_driver 数据库的链接
-
@param db_user 数据库的用户名
-
@param db_password 数据库的密码
*/
public static void genertatorUilt(String[] tables, String item_model, String mapperUrl, String db_url, String db_driver, String db_user,
String db_password,String beanPath_url,String bean_url,String mapper_url) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
final String projectPath = System.getProperty(“user.dir”)+item_model;
final String beanProjectPath=System.getProperty(“user.dir”)+beanPath_url;gc.setOutputDir(projectPath + “/src/main/java”);
gc.setAuthor(“zbf”);
gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setEnableCache(false);
mpg.setGlobalConfig(gc);// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(db_url);
dsc.setDriverName(db_driver);
dsc.setUsername(db_user);
dsc.setPassword(db_password);
mpg.setDataSource(dsc);// 包配置
// PackageConfig pc = new PackageConfig();
// pc.setModuleName(null);
// pc.setParent(parentPackage);
// mpg.setPackageInfo(pc);// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {}
};
// 如果模板引擎是 freemarker
String templatePath = “/templates/mapper.xml.ftl”;// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
/projectPath + “/src/main/resources/mapper/” + pc.getModuleName()
+ “/” + tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML;/
return projectPath + mapperUrl
+ “/” + tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML;
}
});String templatePathBean="/templates/entity.java.ftl";
focList.add(new FileOutConfig(templatePathBean) {
@Override
public String outputFile(TableInfo tableInfo) {
return beanProjectPath+bean_url +"/"+tableInfo.getEntityName()+StringPool.DOT_JAVA;
}
});String templateMapper="/templates/mapper.java.ftl";
focList.add(new FileOutConfig(templateMapper) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath+mapper_url+"/"+tableInfo.getEntityName()+“Mapper”+StringPool.DOT_JAVA;
}
});cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
//是否需要产生对应的默认文件
templateConfig.setXml(null);
templateConfig.setEntity(null);
templateConfig.setController(null);
templateConfig.setServiceImpl(null);
templateConfig.setService(null);
templateConfig.setMapper(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();//实体对象公共父类
//strategy.setSuperEntityClass(BaseEntity.class);//表名生成策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//表字段生成策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setInclude(tables);
strategy.setEntityLombokModel(true);
// strategy.setTablePrefix(pc.getModuleName() + “_”);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
-
}