0
点赞
收藏
分享

微信扫一扫

Mybatis-plus代码生成器,子模块下使用templates模板生成代码,在模块下使用单元测试文件直接生成

zmhc 2022-02-14 阅读 113

在这里插入图片描述

public class CodeGenerator {

    @Test
    public void run() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("developerWang");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        //xml开启BaseResultMap
        gc.setBaseResultMap(true);
        //xml开启BaseColumnList
        gc.setBaseColumnList(true);
        //实体属性Swagger2注解
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        /**
         * 数据库资源的配置
         */
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3308/ihrm?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root8");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        /**
         * 包配置
         */
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("comservice"); //模块名
        pc.setParent("com.example");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        /**
         * 自定义配置
         */
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };

        //freemarker模版引擎
        String templatePath = "/templates/mapper.xml.ftl";

        //自定义输出配置
        List<FileOutConfig> listFoc = new ArrayList<FileOutConfig>();

        listFoc.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出的文件名
                return projectPath+"/src/main/resources/mapper/"+tableInfo.getEntityName()+
                        "Mapper"+ StringPool.DOT_XML;
            }
        });

        cfg.setFileOutConfigList(listFoc);
        mpg.setCfg(cfg);

        //配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        /**
         * 策略配置
         */
        StrategyConfig strategy  =new StrategyConfig();
        strategy.setInclude("co_company");
        //数据库表映射到实体的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //lombok模型,实体层加lombok注解
        strategy.setEntityLombokModel(true);
        //控制层直接加 @RestController 控制器
        strategy.setRestControllerStyle(true);
//        strategy.setInclude(scanner("表名,多个英文逗号隔开").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        //表前缀
        strategy.setTablePrefix("co_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
举报

相关推荐

0 条评论