mybatis-plus代码生成配置测试

public static void main(String[] args) {
        // 1、构建代码自动生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2、配置策略
        // 2-1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath+"/src/main/java");
        globalConfig.setAuthor("P");
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(false);// 是否覆盖
        globalConfig.setServiceName("%sService");// 去service的i前缀
        globalConfig.setIdType(IdType.ASSIGN_ID); // 设置id
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setEnableCache(false); // mybaits二级缓存
        globalConfig.setBaseResultMap(true);

//        globalConfig.setSwagger2(true);
        autoGenerator.setGlobalConfig(globalConfig);

        // 2-2、设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/cx-master?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        autoGenerator.setDataSource(dataSourceConfig);

        // 2-3、包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("project");
        packageConfig.setParent("com.cx");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setXml("mapper.mapping");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        autoGenerator.setPackageInfo(packageConfig);

        // 2-4、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("sys_user");// 设置要映射的表名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);// 自动lombok
        strategyConfig.setChainModel(true);// 链式模型
        strategyConfig.setLogicDeleteFieldName("deleted");
        strategyConfig.setTablePrefix("sys_");//去除表前缀

        //2-4-1、自动当填充配置
        TableFill createUserId = new TableFill("create_user_id" , FieldFill.INSERT);
        TableFill updateUserId = new TableFill("update_user_id", FieldFill.INSERT_UPDATE);
        ArrayList tableFills = new ArrayList<>();
        tableFills.add(createUserId);
        tableFills.add(updateUserId);
        strategyConfig.setTableFillList(tableFills);

        // 2-4-2、乐观锁
        strategyConfig.setVersionFieldName("version");

        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
        autoGenerator.setStrategy(strategyConfig);

        autoGenerator.execute();

    }

 

你可能感兴趣的:(mybatis-plus)