Ibatis批量插入数据

为了减少数据库访问压力,可以在适当的情况下采用Ibatis批量执行。例子如下:

import java.util.ArrayList;
/**
 * 关健字缓存(通过单例来处理)
 *
 * @author
 * @version v 0.1 2012-3-16 下午05:14:06
 */
public class KeywordSingleton extends ArrayList<String>{

    /**  */
    private static final long serialVersionUID = -6177039723625154736L;
    /** 缓存关健字size */
    public static final int            cacheSize        = 10;
   
    private static final KeywordSingleton instance =  new KeywordSingleton();
    /**
     * 私有构造方法
     */
    private KeywordSingleton(){}
    /**
     * 返回唯一实例
     * @return
     */
    public static KeywordSingleton getInstance(){
        return instance;
    }
}

//方法

public void recordKeyword(final String keyword) {
        super.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                    throws SQLException {
                executor.startBatch();// 开始批处理

               //采用单例进行保存数据
                KeywordSingleton.getInstance().add(keyword);
                if (KeywordSingleton.getInstance().size() == KeywordSingleton.cacheSize) {
                    for (int i = 0; i < KeywordSingleton.getInstance().size(); i++) {
                        executor.update("KEYWORDS.recordKeyword", KeywordSingleton
                                .getInstance().get(i));
                    }
                    executor.executeBatch();// 执行批处理
                    KeywordSingleton.getInstance().clear();// 执行完后清空数组
                    log.info("清空关健字缓存数组:"
                            + KeywordSingleton.getInstance().size());
                }
                return null;
            }
        });
    }
   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值