Search in sources :

Example 1 with BatchInsertTypeHandler

use of com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler in project qiuyj-code by qiuyuanjun.

the class OracleSqlBuilder method batchInsert.

@Override
public Object batchInsert(SqlInfo sqlInfo, Object args) {
    List list = (List) ParameterResolver.resolveParameter(args).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert()'s parameter can not be an empty list");
    } else {
        StringBuilder sql = new StringBuilder(256);
        sql.append("INSERT ALL");
        String join = " INTO " + sqlInfo.getTableName();
        StringBuilder eachBuilder = new StringBuilder();
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> instanceParameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
        BatchInsertTypeHandler typeHandler = new BatchInsertTypeHandler(sqlInfo);
        ParameterMapping canonic = new ParameterMapping.Builder(sqlInfo.getConfiguration(), "list", typeHandler).build();
        for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
            joiner.add(column);
            instanceParameterMappings.add(canonic);
        }
        eachBuilder.append(joiner.toString());
        eachBuilder.append(" VALUES ");
        joiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        for (int idx = 0; idx < sqlInfo.getFieldCount(); idx++) {
            joiner.add(AbstractCommonSqlBuilder.PREPARE_FLAG);
        }
        eachBuilder.append(joiner.toString());
        joiner = new StringJoiner(join);
        for (int idx = 0; idx < list.size(); idx++) {
            joiner.add(eachBuilder.toString());
            parameterMappings.addAll(instanceParameterMappings);
        }
        sql.append(join).append(joiner).append(" SELECT 1 FROM DUAL");
        return new StaticSqlSource(sqlInfo.getConfiguration(), sql.toString(), parameterMappings);
    }
}
Also used : BatchInsertTypeHandler(com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) StringJoiner(java.util.StringJoiner)

Example 2 with BatchInsertTypeHandler

use of com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler in project qiuyj-code by qiuyuanjun.

the class MysqlSqlBuilder method batchInsert.

@Override
public Object batchInsert(SqlInfo sqlInfo, Object args) {
    List list = (List) ParameterResolver.resolveParameter(args).getParameterValues()[0];
    if (list.isEmpty()) {
        throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
    } else {
        // 由于批量增加的sql可能会很长,所以这里给一个较大的初始化值
        StringBuilder sqlBuilder = new StringBuilder(256);
        sqlBuilder.append("INSERT INTO ");
        sqlBuilder.append(sqlInfo.getTableName());
        StringJoiner joiner = new StringJoiner(",", "(", ")");
        for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
            joiner.add(column);
        }
        sqlBuilder.append(joiner.toString());
        sqlBuilder.append(" VALUES ");
        StringJoiner grammaJoiner = new StringJoiner(",");
        StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
        List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
        BatchInsertTypeHandler typeHandler = new BatchInsertTypeHandler(sqlInfo);
        ParameterMapping canonic = new ParameterMapping.Builder(sqlInfo.getConfiguration(), "list", typeHandler).build();
        for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
            valueJoiner.add(AbstractCommonSqlBuilder.PREPARE_FLAG);
            singleInstanceParameterMapping.add(canonic);
        }
        List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
        String value = valueJoiner.toString();
        for (int i = 0; i < list.size(); i++) {
            grammaJoiner.add(value);
            allParameterMappings.addAll(singleInstanceParameterMapping);
        }
        sqlBuilder.append(grammaJoiner.toString());
        return new StaticSqlSource(sqlInfo.getConfiguration(), sqlBuilder.toString(), allParameterMappings);
    }
}
Also used : BatchInsertTypeHandler(com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler) ArrayList(java.util.ArrayList) PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) List(java.util.List) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) StringJoiner(java.util.StringJoiner)

Aggregations

BatchInsertTypeHandler (com.qiuyj.mybatis.sqlbuild.typehandler.BatchInsertTypeHandler)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)2 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)2 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)1