com.baomidou.mybatisplus.annotation.IdType Java Examples

The following examples show how to use com.baomidou.mybatisplus.annotation.IdType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomMybatisPlusParameterHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo, MappedStatement ms, Object parameterObject, boolean isInsert) {
    if (null == tableInfo) {
        return parameterObject;
    } else {
        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
        if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty()) && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }

        if (metaObjectHandler != null) {
            if (isInsert && metaObjectHandler.openInsertFill()) {
                metaObjectHandler.insertFill(metaObject);
            } else if (!isInsert) {
                metaObjectHandler.updateFill(metaObject);
            }
        }

        return metaObject.getOriginalObject();
    }
}
 
Example #2
Source File: SpringBootPlusGenerator.java    From spring-boot-plus with Apache License 2.0 4 votes vote down vote up
/**
 * 生成代码
 * @param args
 */
public static void main(String[] args) {
    GeneratorProperties generatorProperties = new GeneratorProperties();

    // 设置基本信息
    generatorProperties
            .setMavenModuleName("example")
            .setParentPackage("com.example")
            .setModuleName("foobar")
            .setAuthor("geekidea")
            .setFileOverride(true);

    // 设置表信息
    generatorProperties.addTable("foo_bar","id");
    // 设置表前缀
    // generatorProperties.setTablePrefix(Arrays.asList("tb_"));

    // 数据源配置
    generatorProperties.getDataSourceConfig()
            .setDbType(DbType.MYSQL)
            .setUsername("root")
            .setPassword("root")
            .setDriverName("com.mysql.jdbc.Driver")
            .setUrl("jdbc:mysql://localhost:3306/spring_boot_plus?useUnicode=true&characterEncoding=UTF-8&useSSL=false");

    // 生成配置
    generatorProperties.getGeneratorConfig()
            .setGeneratorStrategy(GeneratorStrategy.SINGLE)
            .setGeneratorEntity(true)
            .setGeneratorController(true)
            .setGeneratorService(true)
            .setGeneratorServiceImpl(true)
            .setGeneratorMapper(true)
            .setGeneratorMapperXml(true)
            .setGeneratorPageParam(true)
            .setGeneratorQueryVo(true)
            .setRequiresPermissions(false)
            .setPageListOrder(true)
            .setParamValidation(true)
            .setSwaggerTags(true)
            .setOperationLog(true);

    // 全局配置
    generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
            .setOpen(true)
            .setSwagger2(true)
            .setIdType(IdType.AUTO)
            .setDateType(DateType.ONLY_DATE);

    // 策略配置
    generatorProperties.getMybatisPlusGeneratorConfig().getStrategyConfig()
            .setNaming(NamingStrategy.underline_to_camel)
            .setColumnNaming(NamingStrategy.underline_to_camel)
            .setEntityLombokModel(true)
            .setRestControllerStyle(true)
            .setControllerMappingHyphenStyle(true)
            .setVersionFieldName(GeneratorConstant.VERSION)
            .setLogicDeleteFieldName(GeneratorConstant.DELETED);

    // 生成代码
    CodeGenerator codeGenerator = new CodeGenerator();
    codeGenerator.generator(generatorProperties);

}
 
Example #3
Source File: CodeGenerator2.java    From kvf-admin with MIT License 4 votes vote down vote up
public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

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

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/activiti_k?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.kalvin");
        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.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
            }
        });
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        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.kalvin.kvf.common.entity.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass("com.kalvin.kvf.common.controller.BaseController");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }