guzz的批量增加的功能

针对guzz的表分切与批量增加的应用

guzz的表切分功能,数据库表是需要提前建好的。

guzz对于单个记录的插入操作可以在插入之前设置 tableConditon.

例如:
WriteTranSession  session=trm.openRWTran(true);		
		int userNo=1;
		int courseNo=2;
		
		try{
		
			for(int i=0,j=100;i<j;i++)
			{
				userNo=i%4+1;
				courseNo=courseNo%8+1;
				
				Study study=new Study( userNo,  courseNo);
				
				Guzz.setTableCondition(study.getUserNo()) ;
				
				session.insert(study) ;

                                System.out.println(study.getNo());//可以得到记录号
			}
		
		}finally{
			session.close();
		}

对于批量插入操作时不可以在每次insert的前一步设置  tableConditon.,

例如:
WriteTranSession  session=trm.openRWTran(false);
		
		int userNo=1;
		int courseNo=2;
		
		try{			
			ObjectBatcher batcher =session.createObjectBatcher();			
			Study study=new Study( userNo,  courseNo);
			for(int i=0,j=100;i<j;i++)
			{
				userNo=i%4+1;
				courseNo=courseNo%8+1;				
				study=new Study( userNo,  courseNo);				
				batcher.setTableCondition(study.getUserNo());//这里会报错的
				batcher.insert(study) ;
				System.out.println(study.getNo());
			}
			
			batcher.executeBatch();
			session.commit();		
		}finally{
			session.close();
		}


我们可以改变成分批的批量处理的结果是:

		
		WriteTranSession  session=trm.openRWTran(false);		
		int userNo=1;
		int courseNo=2;		
		try{
			
			ObjectBatcher batcher1 =session.createObjectBatcher();
			ObjectBatcher batcher2 =session.createObjectBatcher();
			ObjectBatcher batcher3 =session.createObjectBatcher();
			ObjectBatcher batcher4 =session.createObjectBatcher();
							
			batcher1.setTableCondition(1);
			batcher2.setTableCondition(2);
			batcher3.setTableCondition(3);
			batcher4.setTableCondition(4);
			
			Study study=null;
			
			for(int i=0,j=100;i<j;i++)
			{
				userNo=i%4+1;
				courseNo=courseNo%8+1;				
				study=new Study( userNo,  courseNo);				
				//batcher.setTableCondition(study.getUserNo());
				switch(userNo)
				{
				case 1:
					batcher1.insert(study);
					break;
				case 2:
					batcher2.insert(study);
					break;
				case 3:
					batcher3.insert(study);
					break;
				case 4 :
					batcher4.insert(study);
					break;
				}
				 
			}
			
			int s1[]=batcher1.executeBatch();
			int s2[]=batcher2.executeBatch();
			int s3[]=batcher3.executeBatch();
			int s4[]=batcher4.executeBatch(); 
			//s1、s2、s3、s4的元素值都是1
			session.commit();		
		}finally{
			session.close();
		}




附录:
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE guzz-mapping PUBLIC "-//GUZZ//GUZZ MAPPING DTD//EN" "http://www.guzz.org/dtd/guzz-mapping.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.uidd.messageBoard.pojo.Study" table="t_study_" shadow="com.sjdd.messageBoard.pojo.StudyShadouView">
        <id name="no" type="int">
            <generator class="identity" />
        </id>
        <property name="userNo" type="int" column="userNo" />
        <property name="courseNo" type="int" column="courseNo" />
    </class>
</hibernate-mapping>

分表的规则

package com.uidd.messageBoard.pojo;

import org.guzz.exception.GuzzException;
import org.guzz.orm.AbstractShadowTableView;

public class StudyShadouView extends AbstractShadowTableView {

	public String toTableName(Object tableCondition) {
		// TODO Auto-generated method stub
		 if(tableCondition == null){ //强制要求必须设置表分切条件,避免编程时疏忽。
	             throw new GuzzException("null table conditon is not allowed.") ;
	     }
	     
	     int userNo = (Integer) tableCondition ;
	     
	     //如果用户ID为偶数,记入TB_COMMENT1, 否则写入TB_COMMENT2
	     int i = userNo % 3 + 1 ;
	     
	     return super.getConfiguredTableName() + i;
	     
	}

}


创建表的SQL

create table t_study_1
(
  no INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  userNo INT NOT NULL,
  courseNo int NOT NULL
);
create table t_study_2
(
  no INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  userNo INT NOT NULL,
  courseNo int NOT NULL
);
create table t_study_3
(
  no INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  userNo INT NOT NULL,
  courseNo int NOT NULL
);




package com.uidd.messageBoard.pojo;

public class Study implements java.io.Serializable {
	
	private int no;
	private int userNo;
	private int courseNo;
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public int getUserNo() {
		return userNo;
	}
	public void setUserNo(int userNo) {
		this.userNo = userNo;
	}
	public int getCourseNo() {
		return courseNo;
	}
	public void setCourseNo(int courseNo) {
		this.courseNo = courseNo;
	}
	public Study() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Study(int no, int userNo, int courseNo) {
		super();
		this.no = no;
		this.userNo = userNo;
		this.courseNo = courseNo;
	}
	
	public Study(int userNo, int courseNo) {
		super();
		this.userNo = userNo;
		this.courseNo = courseNo;
	}
	
}

你可能感兴趣的:(批量处理,guzz,表的水平切分)