|
@@ -0,0 +1,97 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.annotation.DbType;
|
|
|
+import com.baomidou.mybatisplus.annotation.FieldFill;
|
|
|
+import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
|
+import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.PackageConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.po.TableFill;
|
|
|
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author admin
|
|
|
+ */
|
|
|
+public class CodeGeneration {
|
|
|
+
|
|
|
+ //main函数
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ AutoGenerator autoGenerator = new AutoGenerator();
|
|
|
+
|
|
|
+ //全局配置
|
|
|
+ GlobalConfig gc = new GlobalConfig();
|
|
|
+ String oPath = System.getProperty("user.dir");//得到当前项目的路径
|
|
|
+ gc.setOutputDir("C:\\Users\\admin\\Desktop"); //生成文件输出根目录
|
|
|
+ gc.setOpen(false);//生成完成后不弹出文件框
|
|
|
+ gc.setFileOverride(true); //文件覆盖
|
|
|
+ gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
|
|
|
+ gc.setEnableCache(false);// XML 二级缓存
|
|
|
+ gc.setBaseResultMap(true);// XML ResultMap
|
|
|
+ gc.setBaseColumnList(false);// XML columList
|
|
|
+ gc.setAuthor("fpy");// 作者
|
|
|
+
|
|
|
+ // 自定义文件命名,注意 %s 会自动填充表实体属性!
|
|
|
+ gc.setControllerName("%sController");
|
|
|
+ gc.setServiceName("%sService");
|
|
|
+ gc.setServiceImplName("%sServiceImpl");
|
|
|
+ gc.setMapperName("%sMapper");
|
|
|
+ gc.setXmlName("%sMapper");
|
|
|
+ autoGenerator.setGlobalConfig(gc);
|
|
|
+
|
|
|
+ // 数据源配置
|
|
|
+ DataSourceConfig dsc = new DataSourceConfig();
|
|
|
+ dsc.setDbType(DbType.MYSQL); //设置数据库类型,我是postgresql
|
|
|
+ dsc.setDriverName("com.mysql.cj.jdbc.Driver");
|
|
|
+ dsc.setUsername("root");
|
|
|
+ dsc.setPassword("Incontrol@Rgs!2021");
|
|
|
+ dsc.setUrl("jdbc:mysql://49.73.84.232:3306/rgs002268?characterEncoding=utf8&failOverReadOnly=false&autoReconnect=true&roundRobinLoadBalance=true&serverTimezone=GMT%2B8&useSSL=false"); //指定数据库
|
|
|
+ autoGenerator.setDataSource(dsc);
|
|
|
+
|
|
|
+ // 3、包的配置
|
|
|
+ PackageConfig pc = new PackageConfig();
|
|
|
+ // pc.setModuleName("blog"); // 生成到指定模块中
|
|
|
+ pc.setParent("cn.com");
|
|
|
+ pc.setEntity("entity");
|
|
|
+ pc.setMapper("mapper");
|
|
|
+ pc.setService("service");
|
|
|
+ pc.setController("controller");
|
|
|
+ autoGenerator.setPackageInfo(pc);
|
|
|
+
|
|
|
+ // 4、策略配置
|
|
|
+ StrategyConfig strategy = new StrategyConfig();
|
|
|
+ // 设置要映射的表名(重要,需要修改的地方)
|
|
|
+ strategy.setInclude("qc_models_material_type");
|
|
|
+ strategy.setNaming(NamingStrategy.underline_to_camel); // 自动转换表名的驼峰命名法
|
|
|
+ strategy.setColumnNaming(NamingStrategy.no_change); // 自动转换列名的驼峰命名法
|
|
|
+ strategy.setEntityLombokModel(true); // 是否使用lombox
|
|
|
+ strategy.setLogicDeleteFieldName("deleted"); // 配置逻辑删除字段
|
|
|
+ // 自动填充策略
|
|
|
+ TableFill gmtCreate = new TableFill("create", FieldFill.INSERT);
|
|
|
+ TableFill gmtModified = new TableFill("updated", FieldFill.INSERT);
|
|
|
+ ArrayList<TableFill> tableFills = new ArrayList<>();
|
|
|
+ tableFills.add(gmtCreate);
|
|
|
+ tableFills.add(gmtModified);
|
|
|
+// strategy.setTableFillList(tableFills);
|
|
|
+ // 乐观锁
|
|
|
+ strategy.setVersionFieldName("version");
|
|
|
+
|
|
|
+ autoGenerator.setStrategy(strategy);
|
|
|
+
|
|
|
+ // 自定义配置
|
|
|
+ InjectionConfig cfg = new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ // to do nothing
|
|
|
+ }
|
|
|
+ };
|
|
|
+ autoGenerator.setCfg(cfg);
|
|
|
+
|
|
|
+ autoGenerator.execute(); // 执行
|
|
|
+ }
|
|
|
+
|
|
|
+}
|