mybatis plus 自定义生成模板

作者: 码农梦醒 | 来源:发表于2019-12-09 07:30 被阅读0次
    import com.baomidou.mybatisplus.enums.IdType;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
    import com.baomidou.mybatisplus.generator.config.GlobalConfig;
    import com.baomidou.mybatisplus.generator.config.PackageConfig;
    import com.baomidou.mybatisplus.generator.config.StrategyConfig;
    import com.baomidou.mybatisplus.generator.config.rules.DbType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import org.junit.Test;
    
    /**
     * @author pan
     * @date 2019-12-08
     */
    public class GeneratorServiceEntity {
    
        @Test
        public void generateCode() {
            String packageName = "itmc";
            boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
            generateByTables(serviceNameStartWithI, packageName, "b_requirement_report");
        }
    
        private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
            String codeFileOutputDir = "/Users/pan/workspace/webservice-dir/test";
            GlobalConfig config = new GlobalConfig();
            String dbUrl = "jdbc:mysql://localhost:3306/itmc_dev";
            DataSourceConfig dataSourceConfig = new DataSourceConfig();
            dataSourceConfig.setDbType(DbType.MYSQL)
                    .setUrl(dbUrl)
                    .setUsername("root")
                    .setPassword("root")
                    .setDriverName("com.mysql.jdbc.Driver");
            StrategyConfig strategyConfig = new StrategyConfig();
            strategyConfig
                    .setControllerMappingHyphenStyle(true)
                    .setEntityColumnConstant(true)
                    .setRestControllerStyle(true)
                    .setTablePrefix("b_")
                    .setCapitalMode(true)
                    .setEntityLombokModel(true)
                    .setDbColumnUnderline(true)
                    .entityTableFieldAnnotationEnable(true)
                    .setNaming(NamingStrategy.underline_to_camel)
                    .setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组
             // 去除entity父类的公共字段
            // strategy.setSuperEntityColumns(superEntityColumns);
            config.setActiveRecord(true)//po类是否继承mybatis plus的Model类
                    .setAuthor("潘志勇")
                    .setBaseResultMap(true)
                    .setBaseColumnList(true)
                    .setIdType(IdType.INPUT)
                    .setMapperName("%sMapper")
                    .setXmlName("%sMapper")
                    .setOutputDir(codeFileOutputDir)
                    .setFileOverride(true);
            if (!serviceNameStartWithI) {
                config.setServiceName("%sService");
            }
    
            new AutoGenerator().setGlobalConfig(config)
                    .setDataSource(dataSourceConfig)
                    .setStrategy(strategyConfig)
                    .setPackageInfo(
                            new PackageConfig()
                                    .setParent(packageName)
                                    .setController("controller.requirement")
                                    .setEntity("model.requirement")
                                    .setMapper("mapper.requirement")
                                    .setXml("mapper.requirement")
                    ).execute();
        }
    
        private void generateByTables(String packageName, String... tableNames) {
            generateByTables(true, packageName, tableNames);
        }
    }
    
    

    https://gitee.com/flying-cattle/mybatis-dsc-generator

    相关文章

      网友评论

        本文标题:mybatis plus 自定义生成模板

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