Example usage for org.apache.ibatis.mapping SqlCommandType INSERT

List of usage examples for org.apache.ibatis.mapping SqlCommandType INSERT

Introduction

In this page you can find the example usage for org.apache.ibatis.mapping SqlCommandType INSERT.

Prototype

SqlCommandType INSERT

To view the source code for org.apache.ibatis.mapping SqlCommandType INSERT.

Click Source Link

Usage

From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java

License:Apache License

public MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
        SqlSource sqlSource, KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
    return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.INSERT, modelClass, null,
            Integer.class, keyGenerator, keyProperty, keyColumn);
}

From source file:com.baomidou.mybatisplus.mapper.AutoSqlInjector.java

License:Apache License

/**
 * initInsert//from   w  w  w.  ja v a  2  s .  c om
 */
private void initInsert() {
    if (hasMappedStatement(SqlRunner.INSERT)) {
        logger.warn("MappedStatement 'SqlRunner.Insert' Already Exists");
        return;
    }
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, SqlRunner.SQLScript, Map.class);
    createUpdateMappedStatement(SqlRunner.INSERT, sqlSource, SqlCommandType.INSERT);
}

From source file:com.baomidou.mybatisplus.MybatisDefaultParameterHandler.java

License:Apache License

/**
 * <p>/* ww  w . jav a2 s  . c om*/
 * ? ID
 * </p>
 *
 * @param ms
 * @param parameterObject ??
 * @return
 */
protected static Object processBatch(MappedStatement ms, Object parameterObject) {
    boolean isFill = false;
    // ???
    MetaObjectHandler metaObjectHandler = GlobalConfiguration.getMetaObjectHandler(ms.getConfiguration());
    /* ???? */
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        isFill = true;
    } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
        isFill = true;
    }
    if (isFill) {
        Collection<Object> parameters = getParameters(parameterObject);
        if (null != parameters) {
            List<Object> objList = new ArrayList<>();
            for (Object parameter : parameters) {
                TableInfo tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
                if (null != tableInfo) {
                    objList.add(populateKeys(metaObjectHandler, tableInfo, ms, parameter));
                } else {
                    /*
                     * ???
                    */
                    objList.add(parameter);
                }
            }
            return objList;
        } else {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(parameterObject.getClass());
            return populateKeys(metaObjectHandler, tableInfo, ms, parameterObject);
        }
    }
    return parameterObject;
}

From source file:com.baomidou.mybatisplus.MybatisDefaultParameterHandler.java

License:Apache License

/**
 * <p>/*from w w  w .  java2  s.  co  m*/
 * 
 * </p>
 *
 * @param metaObjectHandler ??
 * @param tableInfo         ????
 * @param ms                MappedStatement
 * @param parameterObject   ??
 * @return Object
 */
protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
        MappedStatement ms, Object parameterObject) {
    if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
        /* ?? */
        return parameterObject;
    }
    /*  */
    MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        if (tableInfo.getIdType().getKey() >= 2) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            /*  ID */
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }
        // ?
        if (metaObjectHandler.openInsertFill()) {
            metaObjectHandler.insertFill(metaObject);
        }
    } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
        // 
        metaObjectHandler.updateFill(metaObject);
    }
    return metaObject.getOriginalObject();
}

From source file:com.baomidou.mybatisplus.MybatisMapperAnnotationBuilder.java

License:Apache License

void parseStatement(Method method) {
    Class<?> parameterTypeClass = getParameterType(method);
    LanguageDriver languageDriver = getLanguageDriver(method);
    SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
    if (sqlSource != null) {
        Options options = method.getAnnotation(Options.class);
        final String mappedStatementId = type.getName() + "." + method.getName();
        Integer fetchSize = null;
        Integer timeout = null;/* w  w  w. j  a v a2s  .com*/
        StatementType statementType = StatementType.PREPARED;
        ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
        SqlCommandType sqlCommandType = getSqlCommandType(method);
        boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
        boolean flushCache = !isSelect;
        boolean useCache = isSelect;

        KeyGenerator keyGenerator;
        String keyProperty = "id";
        String keyColumn = null;
        if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
            // first check for SelectKey annotation - that overrides everything else
            SelectKey selectKey = method.getAnnotation(SelectKey.class);
            if (selectKey != null) {
                keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method),
                        languageDriver);
                keyProperty = selectKey.keyProperty();
            } else if (options == null) {
                keyGenerator = configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE
                        : NoKeyGenerator.INSTANCE;
            } else {
                keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE
                        : NoKeyGenerator.INSTANCE;
                keyProperty = options.keyProperty();
                keyColumn = options.keyColumn();
            }
        } else {
            keyGenerator = NoKeyGenerator.INSTANCE;
        }

        if (options != null) {
            if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
                flushCache = true;
            } else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
                flushCache = false;
            }
            useCache = options.useCache();
            fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE
                    ? options.fetchSize()
                    : null; //issue #348
            timeout = options.timeout() > -1 ? options.timeout() : null;
            statementType = options.statementType();
            resultSetType = options.resultSetType();
        }

        String resultMapId = null;
        ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
        if (resultMapAnnotation != null) {
            String[] resultMaps = resultMapAnnotation.value();
            StringBuilder sb = new StringBuilder();
            for (String resultMap : resultMaps) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(resultMap);
            }
            resultMapId = sb.toString();
        } else if (isSelect) {
            resultMapId = parseResultMap(method);
        }

        assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize,
                timeout,
                // ParameterMapID
                null, parameterTypeClass, resultMapId, getReturnType(method), resultSetType, flushCache,
                useCache,
                // TODO gcode issue #577
                false, keyGenerator, keyProperty, keyColumn,
                // DatabaseID
                null, languageDriver,
                // ResultSets
                options != null ? nullOrEmpty(options.resultSets()) : null);
    }
}

From source file:com.mybatisX.core.MybatisDefaultParameterHandler.java

License:Apache License

/**
 * <p>/*from  w ww. j  a  va  2 s.  c  o  m*/
 * ? ID
 * </p>
 * 
 * @param ms
 * @param parameterObject
 *            ??
 * @return
 */
protected static Object processBatch(MappedStatement ms, Object parameterObject) {
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        /**
         * ????
         */
        Collection<Object> parameters = getParameters(parameterObject);
        if (null != parameters) {
            List<Object> objList = new ArrayList<Object>();
            for (Object parameter : parameters) {
                TableInfo tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
                if (null != tableInfo) {
                    objList.add(populateKeys(tableInfo, ms, parameter));
                } else {
                    /*
                     * ???
                     */
                    objList.add(parameter);
                }
            }
            return objList;
        } else {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(parameterObject.getClass());
            return populateKeys(tableInfo, ms, parameterObject);
        }
    }
    return parameterObject;
}

From source file:com.mybatisX.core.MybatisMapperAnnotationBuilder.java

License:Apache License

void parseStatement(Method method) {
    Class<?> parameterTypeClass = getParameterType(method);
    LanguageDriver languageDriver = getLanguageDriver(method);
    SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
    if (sqlSource != null) {
        Options options = method.getAnnotation(Options.class);
        final String mappedStatementId = type.getName() + "." + method.getName();
        Integer fetchSize = null;
        Integer timeout = null;/*from  w w w . j  a v a2s. c o m*/
        StatementType statementType = StatementType.PREPARED;
        ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
        SqlCommandType sqlCommandType = getSqlCommandType(method);
        boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
        boolean flushCache = !isSelect;
        boolean useCache = isSelect;
        KeyGenerator keyGenerator;
        String keyProperty = "id";
        String keyColumn = null;
        if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
            // first check for SelectKey annotation - that overrides
            // everything else
            SelectKey selectKey = method.getAnnotation(SelectKey.class);
            if (selectKey != null) {
                keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method),
                        languageDriver);
                keyProperty = selectKey.keyProperty();
            } else if (options == null) {
                keyGenerator = configuration.isUseGeneratedKeys() ? new Jdbc3KeyGenerator()
                        : new NoKeyGenerator();
            } else {
                keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
                keyProperty = options.keyProperty();
                keyColumn = options.keyColumn();
            }
        } else {
            keyGenerator = new NoKeyGenerator();
        }
        if (options != null) {
            if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
                flushCache = true;
            } else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
                flushCache = false;
            }
            useCache = options.useCache();
            fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE
                    ? options.fetchSize()
                    : null; // issue
            // #348
            timeout = options.timeout() > -1 ? options.timeout() : null;
            statementType = options.statementType();
            resultSetType = options.resultSetType();
        }
        String resultMapId = null;
        ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
        if (resultMapAnnotation != null) {
            String[] resultMaps = resultMapAnnotation.value();
            StringBuilder sb = new StringBuilder();
            for (String resultMap : resultMaps) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(resultMap);
            }
            resultMapId = sb.toString();
        } else if (isSelect) {
            resultMapId = parseResultMap(method);
        }
        assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize,
                timeout,
                // ParameterMapID
                null, parameterTypeClass, resultMapId, getReturnType(method), resultSetType, flushCache,
                useCache,
                // TODO gcode issue #577
                false, keyGenerator, keyProperty, keyColumn,
                // DatabaseID
                null, languageDriver,
                // ResultSets
                options != null ? nullOrEmpty(options.resultSets()) : null);
    }
}

From source file:com.mybatisX.mapper.AutoSqlInjector.java

License:Apache License

/**
 * <p>/*w ww. j  a  va  2  s . c o  m*/
 * IF ??
 * </p>
 *
 * @param sqlCommandType
 *            SQL ?
 * @param fieldInfo
 *            ?
 * @param prefix
 *            ??
 * @param colse
 *            ??
 * @return
 */
protected String convertIfTag(SqlCommandType sqlCommandType, TableFieldInfo fieldInfo, String prefix,
        boolean colse) {
    /* ?? */
    String property = fieldInfo.getProperty();
    if (null != prefix) {
        property = prefix + property;
    }

    /*  */
    if (sqlCommandType == SqlCommandType.INSERT && fieldInfo.getFieldStrategy() == FieldStrategy.FILL) {
        return "";
    }
    if (fieldInfo.getFieldStrategy() == FieldStrategy.IGNORED) {
        return "";
    } else if (fieldInfo.getFieldStrategy() == FieldStrategy.NOT_EMPTY) {
        if (colse) {
            return "</if>";
        } else {
            return String.format("\n\t<if test=\"%s!=null and %s!=''\">", property, property);
        }
    } else {
        // FieldStrategy.NOT_NULL
        if (colse) {
            return "</if>";
        } else {
            return String.format("\n\t<if test=\"%s!=null\">", property);
        }
    }
}

From source file:com.mybatisX.mapper.AutoSqlInjector.java

License:Apache License

protected String convertIfTagInsert(TableFieldInfo fieldInfo, boolean colse) {
    return convertIfTag(SqlCommandType.INSERT, fieldInfo, null, colse);
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

License:Open Source License

private void buildBatchInsert(String statementId, String collection) {
    Integer fetchSize = null;/*ww w.j av  a 2  s.c  o m*/
    Integer timeout = null;
    Class<?> parameterType = entityClass;

    ///~~~~~~~~~~
    boolean flushCache = true;
    boolean useCache = false;
    boolean resultOrdered = false;
    KeyGenerator keyGenerator = new NoKeyGenerator();
    String keyProperty = null;
    String keyColumn = null;

    Id id = AnnotationUtils.findDeclaredAnnotation(Id.class, entityClass);
    GeneratedValue generatedValue = AnnotationUtils.findDeclaredAnnotation(GeneratedValue.class, entityClass);
    if (id != null) {
        String keyStatementId = entityClass.getName() + ".insert" + SelectKeyGenerator.SELECT_KEY_SUFFIX;
        if (!sharded) {
            if (containSn)
                snGenerators.put(statementId, new SnGenerator());

            if (configuration.hasKeyGenerator(keyStatementId)) {
                keyGenerator = configuration.getKeyGenerator(keyStatementId);
            } else if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    keyGenerator = new UuidKeyGenerator(generatedValue.length());

                }
            } else {
                keyGenerator = id.generatedKeys() ? new Jdbc4KeyGenerator() : new NoKeyGenerator();
            }
        } else {
            if (containSn)
                shardSnGenerators.put(statementId, new ShardSnGenerator());
            if (generatedValue != null) {
                if (generatedValue.strategy() == GenerationType.UUID) {
                    shardedKeyGenerators.put(statementId, new ShardUuidKeyGenerator(generatedValue.length()));
                } else if (generatedValue.strategy() == GenerationType.TABLE
                        || generatedValue.strategy() == GenerationType.AUTO) {
                    shardedKeyGenerators.put(statementId, new ShardJdbc4KeyGenerator());
                }
            }
        }
        keyProperty = idField.getName();
        keyColumn = StringUtils.isBlank(id.column()) ? CaseFormatUtils.camelToUnderScore(idField.getName())
                : id.column();
    }

    List<SqlNode> contents = new ArrayList<SqlNode>();
    contents.add(this.getBatchInsertSql(collection));
    SqlSource sqlSource = new DynamicSqlSource(configuration, new MixedSqlNode(contents));

    assistant.addMappedStatement(statementId, sqlSource, StatementType.PREPARED, SqlCommandType.INSERT,
            fetchSize, timeout, null, parameterType, null, null, null, flushCache, useCache, resultOrdered,
            keyGenerator, keyProperty, keyColumn, databaseId, lang);
}