美文网首页
mybatis-plus 3.2.0代码生成器

mybatis-plus 3.2.0代码生成器

作者: 幻影翔 | 来源:发表于2019-11-24 16:09 被阅读0次

    官方教程

    引入依赖

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
            <scope>runtime</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-core</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!-- freemarker模版引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    

    编写代码

      /**
     * 代码生成器
     *
     * @Author jack
     * @Since 1.0 2019/11/24 15:08
     */
    public class CodeGenerator {
    
    private static final String OUTPUTDIR = "/src/main/java";
    // 公共实体
    private static final String SUPPER_ENTITY_CLASS = "com.jack006.common.BaseEntity";
    // 公共父类
    private static final String SUPPER_CONTROLLER_CLASS = "com.jack006.common.BaseController";
    
    private static final String AUTHOR = "jack";
    private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DBURL = "jdbc:mysql://localhost:3306/learn?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
    private static final String DBUSERNAME = "jack";
    private static final String DBPASSWORD = "jack";
    
    
    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+OUTPUTDIR);
        gc.setAuthor(AUTHOR);
        gc.setOpen(false);
        // 是否覆盖文件
        gc.setFileOverride(false);
        // 自定义文件名
        gc.setMapperName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        mpg.setGlobalConfig(gc);
    
        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName(DBDRIVER);
        dsc.setUrl(DBURL);
        dsc.setUsername(DBUSERNAME);
        dsc.setPassword(DBPASSWORD);
        mpg.setDataSource(dsc);
    
        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.jack006");
        pc.setEntity("entity");
        pc.setXml("mapper");
        pc.setService("service");
        mpg.setPackageInfo(pc);
    
        // 定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // do something
            }
        };
    
        // 设置模板引擎
        String templatePath = "/templates/mapper.xml.ftl";
    
        // 自定义输出配置
        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(SUPPER_ENTITY_CLASS);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        strategy.setSuperControllerClass(SUPPER_CONTROLLER_CLASS);
        // 写于父类中的公共字段
        strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
      }
    }
    

    使用

    image.png

    生成效果

    image.png

    相关文章

      网友评论

          本文标题:mybatis-plus 3.2.0代码生成器

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