MyBatis-plus代码自动生成器


          // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("");
        gc.setOpen(false);
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);
        gc.setFileOverride(false);
        gc.setIdType(IdType.ASSIGN_UUID);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://xxx.xxx.x.xxx:xxxx/库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT%2b8");
        // dsc.setUrl("jdbc:mysql://xxx.xxx.x.xxx:xxxx/库名?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone = GMT%2b8&zeroDateTimeBehavior=convertToNull");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("userName");
        dsc.setPassword("password");
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                //将数据库中datetime转换成LocalDateTime
                if (fieldType.toLowerCase().contains("datetime")) {
                    return DbColumnType.LOCAL_DATE_TIME;
                }
                return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.xxx");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setServiceImpl("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //表名
        strategy.setInclude("v_carrier_count", "v_carrier_detail");

        // 设置要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntityBuilderModel(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        //去掉前缀
        strategy.setTablePrefix("t_");

        //  strategy.entityTableFieldAnnotationEnable(true);


        // 自动lombok;
        //strategy.setLogicDeleteFieldName("deleted");


        // 自动填充配置
 /*       TableFill gmtCreate = new TableFill("TSP_CREATE_DATE", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("TSP_UPDATE_DATE", FieldFill.INSERT_UPDATE);*/

/*        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate); tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);*/

        // 乐观锁
        // strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);

    
        mpg.setStrategy(strategy);
        mpg.execute(); //执行

为了效率而诞生!!

上一篇:$.ajaxSettings.async = false 讲ajax设置为同步


下一篇:火山PC高级数组的辅助操作案例