美文网首页
自己理解

自己理解

作者: 盗生一 | 来源:发表于2020-10-20 10:06 被阅读0次
    package cn.gxhj.mybatisplus;
    
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.baomidou.mybatisplus.core.toolkit.StringPool;
    import com.baomidou.mybatisplus.core.toolkit.StringUtils;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.InjectionConfig;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.po.TableFill;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * 代码生成器
     */
    public class CodeGenerator {
    
        /**
         * <p>
         * 读取控制台内容
         * </p>
         */
        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.isNotBlank(ipt)) {
                    return ipt;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }
    
        public static void main(String[] args) {
    
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            // 配置项目在在电脑上的路径:配置到具体项目名称
            String projectPath = "C:/Users/Zero/Desktop/logquartz"; // System.getProperty("user.dir");
            //生成文件输出根目录
            gc.setOutputDir(projectPath + "/src/main/java");
            // 生成的文件是否覆盖
            gc.setFileOverride(true);  //文件覆盖
    //        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
    //        gc.setEnableCache(false);// XML 二级缓存
    //        gc.setBaseResultMap(true);// XML ResultMap
    //        gc.setBaseColumnList(false);// XML columList
    
            // 作者
            gc.setAuthor("ZhangShuai");
            //生成完成后不弹出文件框
            gc.setOpen(false);
            // 自定义文件命名,注意 %s 会自动填充表实体属性!
            gc.setEntityName("%sInfo");
            gc.setControllerName("%sController");
            gc.setServiceName("%sService");
            gc.setServiceImplName("%sServiceImpl");
            gc.setMapperName("%sMapper");
            gc.setXmlName("%sInfoMapper");
    
           // gc.setSwagger2(true); // 实体属性 Swagger2 注解
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://127.0.0.1:3306/logquartz?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=false");
            // dsc.setSchemaName("public");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("root");
            mpg.setDataSource(dsc);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            // 设置父级包:XXApplication
            pc.setParent("cn.gxhj.logquartz");
            // 设置模块名
            pc.setModuleName(scanner("模块名"));
            // 设置controller包名
            pc.setController("controller");
            // 设置service包名
            pc.setService("service");
            // 接口实现类默认在service路径下的impl下,设个设置则可以指定其他路径
            //pc.setServiceImpl("service.impl");
            pc.setServiceImpl("service.impl");
            // 设置mapper接口包名
            pc.setMapper("mapper");
            // 接口实现默认在mapper路径下的xml,这个设置则可以指定其他路径
            //pc.setXml("mapper.xml");
            // 设置了 mapper实现xml 不能配置
             pc.setXml("mapper.datamapping");
            // 设置实体包名
            pc.setEntity("entity");
            mpg.setPackageInfo(pc);
    
            // 自定义配置
            // 注入配置, 该配置不在代码中配置出来的话,则该自动生成的文件不会生成对应的文件!!!!
            InjectionConfig cfg = new InjectionConfig() {
                @Override
                public void initMap() {
                    // to do nothing
                }
            };
    
            // 如果模板引擎是 freemarker
    //        String templatePath = "/templates/mapper.xml.ftl";
            // 如果模板引擎是 velocity
            String templatePath = "/templates/mapper.xml.vm";
    
            /************************************ mapper.xml 和 mapper 接口 分开
            // 自定义mapper接口实现配置文件输出的配置(使用了这个则:  templateConfig.setXml(null); 否则
             会多生成一份xml在mapper接口文件夹下的xml文件夹中)
            List<FileOutConfig> focList = new ArrayList<>();
            // 自定义配置会被优先输出
            focList.add(new FileOutConfig(templatePath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                    return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                            + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            });
             ***********************************************/
            /*
            cfg.setFileCreate(new IFileCreate() {
                @Override
                public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                    // 判断自定义文件夹是否需要创建
                    checkDir("调用默认方法创建的目录");
                    return false;
                }
            });
            */
    //        cfg.setFileOutConfigList(focList);
    //        mpg.setCfg(cfg);
    
            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();
    
            // 配置自定义输出模板
            //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
            // templateConfig.setEntity("templates/entity2.java");
            // templateConfig.setService();
            // templateConfig.setController();
    
    
    
            /*
             * 1. 如果.xml文件是生成到Mapper类下面的xml文件夹下,这个配置不需要,如果配置了则不生成.xml文件了
             * 2. 如果.xml文件不是默认生成到Mapper类下面的xml文件夹下,而是指定到某个路径下,
             *     则该配置需要配置tc.setXml(null),如果不配置则Mapper类下面的xml文件夹下还会同时生成一份默认的。
             */
            // 这个标注了则不生成XML文件了
            //templateConfig.setXml(null);
            mpg.setTemplate(templateConfig);
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            // 表名生成策略
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    //        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
            strategy.setEntityLombokModel(true);
            strategy.setRestControllerStyle(true);
            // 表前缀,多个英文逗号分割或者 new String[]{"",""}
            strategy.setTablePrefix("tb_");
            // 加tabale 注解
            strategy.setEntityTableFieldAnnotationEnable(true);
            // 去掉前缀
            strategy.setFieldPrefix("U_");
            strategy.setEnableSqlFilter(false);
            // 公共父类
    //        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
            // 写于父类中的公共字段
            strategy.setSuperEntityColumns("id");
            strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
            strategy.setControllerMappingHyphenStyle(true);
    //        strategy.setTablePrefix(pc.getModuleName() + "_");
    
    
            // 字段自动操作策略
            List<TableFill> tableFillList = new ArrayList<>();
            tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
            tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
            // 表字段与属性映射关系
            strategy.setTableFillList(tableFillList);
            strategy.setEntitySerialVersionUID(true);
            mpg.setStrategy(strategy);
            // 设置模板引擎
            mpg.setTemplateEngine(new VelocityTemplateEngine());
    
            // 执行mybatis plus自动从表生成对应一系列的文件
            mpg.execute();
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:自己理解

          本文链接:https://www.haomeiwen.com/subject/myqcektx.html