tkmybatis -- 自定义批量插入的实现

  tkmybatis 实现自定义的批量新增方法

tk.mybatis.mapper.common.special 包中批量插入数据的接口:
package tk.mybatis.mapper.common.special;

import java.util.List;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.provider.SpecialProvider;

@RegisterMapper
public interface InsertListMapper {
    @Options(
        useGeneratedKeys = true,
        keyProperty = "id"
    )
    @InsertProvider(
        type = SpecialProvider.class,
        method = "dynamicSQL"
    )
    int insertList(List var1);
}

实现类:

package tk.mybatis.mapper.provider;

import java.util.Iterator;
import java.util.Set;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

public class SpecialProvider extends MapperTemplate {
    public SpecialProvider(Class mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String insertList(MappedStatement ms) {
        Class entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
        sql.append(" VALUES ");
        sql.append("");
        sql.append("");
        Set columnList = EntityHelper.getColumns(entityClass);
        Iterator var5 = columnList.iterator();

        while(var5.hasNext()) {
            EntityColumn column = (EntityColumn)var5.next();
            if (!column.isId() && column.isInsertable()) {
                sql.append(column.getColumnHolder("record") + ",");
            }
        }

        sql.append("");
        sql.append("");
        return sql.toString();
    }

    public String insertUseGeneratedKeys(MappedStatement ms) {
        Class entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
        sql.append(SqlHelper.insertValuesColumns(entityClass, true, false, false));
        return sql.toString();
    }
}

 

可以仿照这个来自定义实现一个插入方法:

批量插入接口:

package com.cfcc.tsms.auth.utils;

import com.cfcc.tsms.base.config.provider.SpecialBatchProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;

import java.util.List;

/**
 * 自定义mapper方法,主要是解决批量插入/更新问题(主键字段也插入)
 * 
 * @author gzy
 * @version : 1.0
 * @since : 2017年1月5日 下午8:59:34
 */
@RegisterMapper 
public interface SpecialBatchMapper {
	
	/**
	 * 批量插入数据库,所有字段都插入,包括主键
	 *
	 * @return
	 */
	@Options(useGeneratedKeys = true, keyProperty = "id")
	@InsertProvider(type = SpecialBatchProvider.class, method = "insertListUseAllCols")
	int insertListUseAllCols(List recordList);
}

批量插入实现类: 

package com.cfcc.tsms.base.config.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Set;

/**
 * 自定义mapper方法,主要是解决批量插入/更新问题(主键字段也插入)
 * 
 * @author gzy
 * @version : 1.0
 * @since : 2017年1月5日 下午9:06:35
 */

public class SpecialBatchProvider  extends MapperTemplate {

	public SpecialBatchProvider(Class mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}

	public String insertListUseAllCols(MappedStatement ms) {
        Class entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, false, false, false));
        sql.append(" VALUES ");
        sql.append("");
        sql.append("");
        //获取全部列
        Set columnList = EntityHelper.getColumns(entityClass);
        //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
        for (EntityColumn column : columnList) {
            if (column.isInsertable()) {
                sql.append(column.getColumnHolder("record") + ",");
            }
        }
        sql.append("");
        sql.append("");
        return sql.toString();
	}

}

 

你可能感兴趣的:(java,基础知识,DAO--框架)