搭建低代码平台plus代码生成器的集成

mybatis-plus提供了代码自动生成器;使用代码生成器可以根据数据库表结构生成我们需要的代码结构,节省了我们建各种类的时间。

以下是集成mybatis-plus代码生成器的具体配置:

pom.xml



   com.baomidou
   mybatis-plus-generator
   ${mybatis-plus.version}
   compile

代码生成器工具类

public class CodeGeneration {
        public static String scanner (String tip){
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + ":");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
                String ipt = scanner.next();
                /*if (StringUtils.isNotEmpty(ipt)) {
                    return ipt;
                }*/

                if (ipt.length()>0) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }

        public static void main (String[]args){
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();

            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = "D:/JAVA/crmvideo/src/main/java";
            gc.setOutputDir(projectPath);
            gc.setAuthor("wlx");
            gc.setOpen(false);
            mpg.setGlobalConfig(gc);

            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://127.0.0.1:3306/crmvideo?characterEncoding=utf8&serverTimezone=UTC");
            dsc.setDriverName("com.mysql.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123456");
            mpg.setDataSource(dsc);

            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName(scanner("模块名"));
            pc.setParent("com.wangkai");
            pc.setController("controller");
            pc.setService("service");
            pc.setServiceImpl("serviceImpl");
            pc.setMapper("mapper");
            pc.setEntity("entity");
            pc.setXml("xml");
            mpg.setPackageInfo(pc);

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

            // 如果模板引擎是 freemarker
            String templatePath = "/templates/mapper.xml.ftl";

            // 自定义输出配置
            List focList = new ArrayList<>();
            // 自定义配置会被优先输出
            focList.add(new FileOutConfig(templatePath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    return projectPath + "/com/wangkai/" + pc.getModuleName()
                            + "/xml/" +tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            });

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

            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();

            // 配置自定义输出模板
            //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
            // templateConfig.setEntity("templates/entity2.java");
            // templateConfig.setService();
            // templateConfig.setController();

            templateConfig.setXml(null);
            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.setSuperEntityColumns("id");
            strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
            strategy.setControllerMappingHyphenStyle(true);
            strategy.setTablePrefix(pc.getModuleName() + "_");
            mpg.setStrategy(strategy);
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
            mpg.execute();

        }
}

代码生成器生成的代码结构


搭建低代码平台-mybatis-plus代码生成器的集成

代码生成器生成的代码结构

展开阅读全文

页面更新:2024-03-30

标签:代码   自动识别   英文   数据源   生成器   逗号   全局   模板   结构   引擎   平台

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top