zoukankan      html  css  js  c++  java
  • mybatis-plus代码生成

    1.pom.xml引入mybatis-plus协议

     <!-- mybatisPlus 核心库 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.1.0</version>
            </dependency>
    
            <!-- 引入阿里数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.6</version>
            </dependency>
    
            <!--mysql依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <!-- mybatis plus 代码生成器依赖 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.1.0</version>
            </dependency>
    <!-- 代码生成器模板 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> <!--@Date注解支持--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>

    2.代码生成类配置

    public class CodeGenerator {
    
        //读取控制台内容
        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;
                }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "!");
        }
    
        public static void main(String[] args) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");//设置生成的文件输出位置
            gc.setAuthor("jobob");
            gc.setOpen(false);
            gc.setSwagger2(true); //实体属性 是否生成Swagger2注解,不用可以注释掉
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("数据库连接地址");
            // dsc.setSchemaName("public");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");//数据库驱动
            dsc.setUsername("账号");
            dsc.setPassword("密码");
            mpg.setDataSource(dsc);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName(scanner("模块名"));
            pc.setParent("com");//设置包名
            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";
    
            // 自定义输出配置
            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.setFileOutConfigList(focList);
            mpg.setCfg(cfg);
    
            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();
    
            templateConfig.setXml(null);
            mpg.setTemplate(templateConfig);
    
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setSuperEntityClass("com.deloitte.freport.dbase.IEntity");//实体类的父类,没有就不用设置!
            strategy.setEntityLombokModel(true);
            strategy.setRestControllerStyle(true);
    
            // 公共父类
            //strategy.setSuperControllerClass("controller层父类,没有就不用设置!");
    
            // 写于父类中的公共字段
            strategy.setSuperEntityColumns("id");
            strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));//如果把这段注释掉就会生成数据库中所有的表
            strategy.setControllerMappingHyphenStyle(true);
            strategy.setTablePrefix(pc.getModuleName() + "_");
    
            mpg.setStrategy(strategy);
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
            mpg.execute();
        }

    3.在application.yml 配置mybatis-plus

    mybatis-plus:
      #根据模板生成
      global-config:
        db-config:
          id-type: uuid
          field-strategy: not_empty
          table-underline: true #标的下划线支持
          db-type: mysql #数据库类型
          logic-delete-value: 1 #逻辑删除默认值为1
          logic-not-delete-value: 0 #逻辑未删除默认值为0
      mapper-locations: classpath:/mapper/*.xml #设置扫描包

    4.根据自定义模板生成

    参考:https://mp.baomidou.com/guide/generator.html

  • 相关阅读:
    【自然框架】 之 资源角色——列表过滤方案(思路篇)
    【自然框架】 之 主从表的添加、修改
    今天你进步了吗?
    【自然框架】通用权限的视频演示(一):添加角色,权限到功能节点和按钮
    【杂谈】您是以什么姿态来参与讨论、回帖的?
    【自然框架】之通用权限:用PowerDesigner重新设计了一下数据库,有ER图和表关系图
    【自然框架】 页面里的父类—— 改进和想法、解释
    页面里的父类——BaseUI源代码下载(2009.10.15更新)
    【自然框架】 页面里的父类——把共用的东东都交给父类,让子类专注于其他。
    【自然框架】之通用权限:数据库设计的几种使用方式
  • 原文地址:https://www.cnblogs.com/HQ0422/p/13208137.html
Copyright © 2011-2022 走看看