批量处理SQL

package com.huawei.nis.fttxbaseline.basedata.mapper;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**

  • 批量处理SQL生成抽象类
  • @author lWX501870

*/
public abstract class BatchSQLProvider {

/**
 * 批量新增
 * @param map
 * @return
 */
public String insertBatchSQL(Map> map) {
	List object = (List) map.get("list");  
    StringBuilder sb = new StringBuilder();  
    //此处增加关键字IGNORE,在表中建立唯一索引,新增遇到重复时就自动忽略
    sb.append("INSERT IGNORE INTO ").append(tableName()).append("(");  
    sb.append(StringUtils.join(tableColumns(), ','));
    sb.append(") VALUES ");  
    values(object, sb);
    return sb.toString();  
}

/**
 * 批量删除
 * @param ids
 * @return
 */
public String delBatchSQL(@Param("ids") List ids) {	
    StringBuilder sb = new StringBuilder();  
    sb.append("delete from ").append(tableName()).append(" where id in (");  
    sb.append(StringUtils.join(ids.toArray(), ','));
    sb.append(")");    
    return sb.toString();  
}

/*
 * 批量删除2
 */
public String delArrBatchSQL(@Param("ids") Integer[] ids) {	
    StringBuilder sb = new StringBuilder();  
    sb.append("delete from ").append(tableName()).append(" where id in (");  
    sb.append(StringUtils.join(ids, ','));
    sb.append(")");    
    return sb.toString();  
}


abstract String tableName();

abstract String[] tableColumns();

abstract void values(List object, StringBuilder sb);
 
  

}

你可能感兴趣的:(Java)