Search in sources :

Example 1 with IdType

use of com.baomidou.mybatisplus.annotation.IdType in project citrus by Yiuman.

the class MdaServiceImpl method save.

@Override
public String save(Map<String, Object> entity) throws Exception {
    if (!this.beforeSave(entity)) {
        return null;
    }
    Table tableEntity = getTableEntity(getModelId(WebUtils.getRequest()));
    TableMeta tableMeta = entity2Meta(tableEntity);
    PrimaryKeyConstraint primaryKeyConstraint = tableMeta.getPrimaryKey();
    final Map<ColumnMeta, Object> keyMap = new HashMap<>(tableMeta.getColumns().size());
    if (Objects.nonNull(primaryKeyConstraint)) {
        // 如果是组合主键必须主动赋值,否则抛异常
        if (isCombinePrimaryKeys(primaryKeyConstraint)) {
            primaryKeyConstraint.getColumns().forEach(primaryKeyColumn -> {
                Object key = entity.get(primaryKeyColumn.getColumnName());
                if (Objects.isNull(key)) {
                    // 当前模型的主键为组合模式,不能为空
                    throw new MdaException(String.format("The primary key of the current model is in combination mode and cannot be empty." + " The corresponding columns are [%s]", primaryKeyConstraint.getColumns().stream().map(ColumnMeta::getColumnName).collect(Collectors.joining(","))));
                }
                keyMap.put(primaryKeyColumn, key);
            });
        } else {
            primaryKeyConstraint.getColumns().stream().findFirst().ifPresent(primaryKeyColumn -> {
                if (Objects.isNull(entity.get(primaryKeyColumn.getColumnName()))) {
                    final IdentifierGenerator identifierGenerator = GlobalConfigUtils.getGlobalConfig(getDmlProcessor().getSqlSessionFactory(tableEntity.getNamespace()).getConfiguration()).getIdentifierGenerator();
                    IdType idType = tableEntity.getIdType();
                    Object key;
                    if (idType == IdType.ASSIGN_ID) {
                        Number number = identifierGenerator.nextId(entity);
                        key = primaryKeyColumn.getJdbcType().equals(JDBCType.VARCHAR) ? number.toString() : number;
                    } else {
                        key = identifierGenerator.nextUUID(entity);
                    }
                    keyMap.put(primaryKeyColumn, key);
                    entity.put(primaryKeyColumn.getColumnName(), key);
                } else {
                    keyMap.put(primaryKeyColumn, entity.get(primaryKeyColumn.getColumnName()));
                }
            });
        }
    }
    SaveMeta saveMeta = SaveMeta.builder().namespace(tableMeta.getNamespace()).tableName(tableMeta.getTableName()).entity(entity).build();
    SimpleQueryBuilder simpleQueryBuilder = QueryBuilders.create();
    keyMap.forEach((key, value) -> simpleQueryBuilder.eq(key.getColumnName(), value));
    if (Objects.isNull(get(simpleQueryBuilder.toQuery()))) {
        getDmlProcessor().insert(saveMeta);
    } else {
        UpdateWrapper<?> updateWrapper = Wrappers.update();
        keyMap.forEach((key, value) -> {
            updateWrapper.eq(key.getColumnName(), value);
            entity.remove(key.getColumnName());
        });
        getDmlProcessor().update(saveMeta, updateWrapper);
    }
    afterSave(entity);
    return keyMap.values().stream().map(Object::toString).collect(Collectors.joining("-"));
}
Also used : Table(com.github.yiuman.citrus.mda.entity.Table) SimpleQueryBuilder(com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder) IdType(com.baomidou.mybatisplus.annotation.IdType) MdaException(com.github.yiuman.citrus.mda.exception.MdaException) IdentifierGenerator(com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)

Example 2 with IdType

use of com.baomidou.mybatisplus.annotation.IdType in project c4p by xseuen.

the class CodeGenerator method codeGenerator.

/**
 * 代码生成
 *
 * @see OutputFile
 */
@Test
public void codeGenerator() {
    String projectPath = System.getProperty("user.dir");
    String javaPath = projectPackage.replaceAll("\\.", "/");
    // 设置自定义路径
    Map<OutputFile, String> pathInfo = new HashMap<>();
    pathInfo.put(OutputFile.mapperXml, projectPath + "/src/main/resources/mybatis/mapper/");
    pathInfo.put(OutputFile.entity, projectPath + "/src/main/java/" + javaPath + "/entity/");
    pathInfo.put(OutputFile.controller, projectPath + "/src/main/java/" + javaPath + "/controller/");
    pathInfo.put(OutputFile.service, projectPath + "/src/main/java/" + javaPath + "/service/");
    pathInfo.put(OutputFile.serviceImpl, projectPath + "/src/main/java/" + javaPath + "/service/impl/");
    pathInfo.put(OutputFile.mapper, projectPath + "/src/main/java/" + javaPath + "/mapper/");
    pathInfo.put(OutputFile.other, projectPath + "/src/main/java/" + javaPath + "/dto/");
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().entityBuilder().enableLombok().enableChainModel().idType(IdType.ASSIGN_ID).logicDeleteColumnName("is_deleted").addTableFills(// 基于数据库字段填充
    new Column("gmt_created", FieldFill.INSERT)).addTableFills(new Column("gmt_modified", FieldFill.INSERT_UPDATE)).controllerBuilder().enableRestStyle().build());
    generator.packageInfo(packageConfig().pathInfo(pathInfo).build());
    generator.global(globalConfig().outputDir(projectPath + "/src/main/java").enableSwagger().disableOpenDir().build());
    generator.injection(injectionConfig().build());
    generator.execute(new FreemarkerTemplateEngine() {

        @Override
        protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            String otherPath = getPathInfo(OutputFile.other);
            customFile.forEach((key, value) -> {
                String fileName = String.format((otherPath + File.separator + "%s"), entityName + key);
                outputFile(new File(fileName), objectMap, value);
            });
        }
    });
}
Also used : Column(com.baomidou.mybatisplus.generator.fill.Column) Properties(java.util.Properties) IOException(java.io.IOException) HashMap(java.util.HashMap) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) NotNull(javax.validation.constraints.NotNull) File(java.io.File) Test(org.junit.jupiter.api.Test) IdType(com.baomidou.mybatisplus.annotation.IdType) com.baomidou.mybatisplus.generator.config(com.baomidou.mybatisplus.generator.config) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Map(java.util.Map) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Column(com.baomidou.mybatisplus.generator.fill.Column) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) File(java.io.File) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Aggregations

IdType (com.baomidou.mybatisplus.annotation.IdType)2 FieldFill (com.baomidou.mybatisplus.annotation.FieldFill)1 IdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)1 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)1 com.baomidou.mybatisplus.generator.config (com.baomidou.mybatisplus.generator.config)1 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)1 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)1 Column (com.baomidou.mybatisplus.generator.fill.Column)1 Table (com.github.yiuman.citrus.mda.entity.Table)1 MdaException (com.github.yiuman.citrus.mda.exception.MdaException)1 SimpleQueryBuilder (com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 NotNull (javax.validation.constraints.NotNull)1 Test (org.junit.jupiter.api.Test)1