首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Ibatis批量安插数据

2012-08-21 
Ibatis批量插入数据为了减少数据库访问压力,可以在适当的情况下采用Ibatis批量执行。例子如下:import java.

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;
??? ??? ??? }
??? ??? });
??? }

热点排行