mybatis-plus 自动生成代码到项目的指定模块路径下

1,904 阅读1分钟

现在项目都是模块化,目前在用mybatis-plus,想自动生成代码到指定模块下。需求就这么个需求

直接上代码:

public class Test2 {
    public static void main(String[] args) {
            /**
             * mybaits-plus 生成代码到指定模块路径下
             * @param args
             */
                // 代码生成器
                AutoGenerator mpg = new AutoGenerator();
                // set freemarker 模板引擎
                mpg.setTemplateEngine(new FreemarkerTemplateEngine());

                // 全局配置
                GlobalConfig gc = new GlobalConfig();
                String projectPath = System.getProperty("user.dir");
                // gc.setOutputDir(projectPath + "/src/main/java");
                gc.setAuthor("junchen");
                gc.setOpen(false);
                gc.setBaseResultMap(true);
                // gc.setSwagger2(true); 实体属性 Swagger2 注解
                mpg.setGlobalConfig(gc);

                // 数据源配置
                DataSourceConfig dsc = new DataSourceConfig();
                dsc.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8");
                // dsc.setSchemaName("public");
                dsc.setDriverName("com.mysql.jdbc.Driver");
                dsc.setUsername("root");
                dsc.setPassword("123456");
                mpg.setDataSource(dsc);

                /**
                 * 包配置
                 * 简单来说 就是写绝对路径
                 */
                PackageConfig pc = new PackageConfig();
                //  pc.setModuleName("code");
                pc.setParent("com.xxx");
                //指定生成文件导入的包
                pc.setEntity("domain.entity");
                pc.setService("service.sync");
                pc.setServiceImpl("sync.impl");
                pc.setController("controller");
                pc.setMapper("dao.mapper.storeMapper");
                pc.setXml(null);
                //指定生成文件的绝对路径
                Map<String, String> pathInfo  = new HashMap<>();
                String parentPath = "\\src\\main\\java\\com\\yzt\\erp\\middleware\\";
                String conStr ="\\xx-";

                String entityPath = projectPath.concat(conStr).concat("domain").concat(parentPath).concat("\\domain\\entity");
                String mapper_path = projectPath.concat(conStr).concat("dao").concat(parentPath).concat("dao\\mapper\\storeMapper");
                String xml_path = projectPath.concat(conStr).concat("dao").concat("\\src\\main\\resources\\sqlmap\\store");
                String service_path = projectPath.concat(conStr).concat("service").concat(parentPath).concat("service\\sync");
                String service_impl_path = projectPath.concat(conStr).concat("service").concat(parentPath).concat("service\\sync\\impl");
                String controller_path = projectPath.concat(conStr).concat("web").concat(parentPath).concat("controller");

                pathInfo.put("entity_path",entityPath);
                pathInfo.put("mapper_path",mapper_path);
                pathInfo.put("xml_path",xml_path);
                pathInfo.put("service_path",service_path);
                pathInfo.put("service_impl_path",service_impl_path);
                pathInfo.put("controller_path",controller_path);
                pc.setPathInfo(pathInfo);
                mpg.setPackageInfo(pc);

                // 自定义配置
                // 自定义配置
                InjectionConfig cfg = new InjectionConfig() {
                    @Override
                    public void initMap() {
                        // to do nothing
                    }
                };
                mpg.setCfg(cfg);
                // 配置模板
                TemplateConfig templateConfig = new TemplateConfig();
                mpg.setTemplate(templateConfig);

                // 策略配置
                StrategyConfig strategy = new StrategyConfig();
                strategy.setNaming(NamingStrategy.underline_to_camel);
                strategy.setColumnNaming(NamingStrategy.underline_to_camel);
                strategy.setEntityLombokModel(true);
                strategy.setRestControllerStyle(true);
                strategy.setControllerMappingHyphenStyle(true);//驼峰转
                strategy.setInclude("student");//表名
                strategy.setControllerMappingHyphenStyle(true);
                mpg.setStrategy(strategy);
                mpg.setTemplateEngine(new FreemarkerTemplateEngine());
                mpg.execute();


            }

}