您的位置:首页 > 服装鞋帽 > 女装 > Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境

Hibernate Shards 数据的水平、垂直切割(一)- Hibernate测试环境

luyued 发布于 2011-06-11 05:22   浏览 N 次  

阅读: 58 评论: 0 作者: riccc 发表于 2010-04-09 22:44 原文链接

准备
需要用到的东西如下,下载这几个项目最新的release版本:
1. Hibernate Core,这里用的版本为3.5.0-final
2. Hibernate Shards,这里用的版本为3.0.0-Beta2
3. MySql java connector,这里用的版本为5.1.12
4. slf4j,这里用的版本为1.5.11
5. 安装并启动mysql服务,在mysql中建立数据库hbshards,在里面如下建立测试用的contact表:
 CREATE TABLE contact ( ID varchar(8) NOT NULL, LOGIN_ID varchar(18) NOT NULL, PASSWORD varchar(12) NOT NULL, NAME varchar(40) NOT NULL, EMAIL varchar(40) NOT NULL, PRIMARY KEY (ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
6. 设置好java环境变量
这里用于这个测试的CLASSPATH设置如下
.;
D:\Work\research\Java\Hibernate-Test\lib\antlr-2.7.6.jar;
D:\Work\research\Java\Hibernate-Test\lib\dom4j-1.6.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\jta-1.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\javassist-3.9.0.GA.jar;
D:\Work\research\Java\Hibernate-Test\lib\commons-collections-3.1.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-nop-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\slf4j-api-1.5.11.jar;
D:\Work\research\Java\Hibernate-Test\lib\mysql-connector-java-5.1.12-bin.jar;
D:\Work\research\Java\Hibernate-Test\lib\hibernate3.jar;

Hibernate测试项目
用Eclipse建一个java项目Hibernate-Test,在项目文件夹下面建立lib目录
将hibernate3.jar放入lib目录中,把Hibernate发布包中lib\required目录下除了slf4j-api-1.5.8.jar之外的其他jar文件放入lib目录中
将mysql java connector发布包中的mysql-connector-java-5.1.12-bin.jar放入lib目录
将slf4j发布包中的slf4j-api-1.5.11.jar、slf4j-nop-1.5.11.jar放入lib目录
项目从lib目录中引用hibernate3.jar

在src目录下添加测试用的实体类ContactEntity:
 public class ContactEntity { private String _id; private String _name; private String _email; private String _loginId; private String _password; 
public ContactEntity(){} public ContactEntity(String id, String loginId, String password ,String name, String email){ this._id = id; this._loginId = loginId; this._password = password; this._name = name; this._email = email; } public String getId(){ return this._id; } public void setId(String id){ this._id=id; } public String getEMail(){ return this._email; } public void setEMail(String email){ this._email=email; } public String getName(){ return this._name; } public void setName(String name){ this._name=name; } public String getLoginId(){ return this._loginId; } public void setLoginId(String loginId){ this._loginId=loginId; } public String getPassword(){ return this._password; } public void setPassword(String password){ this._password=password; } public String toString(){ return "{ Id=\"" + this._id + "\"" + ", LoginId=\"" + this._loginId + "\"" + ", Name=\"" + this._name + "\"" + ", EMail=\"" + this._email + "\" }"; } }
测试实体的映射文件ContactEntity.hbm.xml:
             
测试用的类:
 import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; 
public class Main { public static void main(String[] args) { String loginId = "RicCC@cnblogs.com"; String password = "123"; if(args!=null && args.length==2){ loginId = args[0]; password = args[1]; } HibernateTest(loginId, password); } private static void HibernateTest(String loginId, String password){ SessionFactory factory = null; Session session = null; Transaction transaction = null; List contacts = null; Iterator it = null; try{ factory = new Configuration().configure().buildSessionFactory(); session = factory.openSession(); transaction = session.beginTransaction();
System.out.println("===Create Contacts==="); ContactEntity c = new ContactEntity("01111111","RicCC@cnblogs.com" , "123", "Richie", "RicCC@cnblogs.com"); session.save(c); c = new ContactEntity("91111111","a@cnblogs.com" , "123", "AAA", "a@cnblogs.com"); session.save(c); c = new ContactEntity("81111111","b@cnblogs.com" , "123", "BBB", "b@cnblogs.com"); session.save(c); c = new ContactEntity("31111111","c@cnblogs.com" , "123", "CCC", "c@cnblogs.com"); session.save(c); session.flush();
System.out.println("\n===Login Test==="); contacts = session.createQuery("from ContactEntity where LoginId=:loginId") .setString("loginId", loginId) .list(); if(contacts.isEmpty()) System.out.println("contact " + loginId + " not found!"); else{ c = (ContactEntity)contacts.get(0); if(c.getPassword()==password) System.out.println("user " + loginId + " login successful"); else System.out.println("password is incorrect, login failed!"); }
System.out.println("\n===Delete Contacts==="); contacts = session.createQuery("from ContactEntity").list(); it = contacts.iterator(); while(it.hasNext()){ session.delete(it.next()); } transaction.commit(); }catch(Exception e){ if(transaction!=null) transaction.rollback(); System.out.println(e.getMessage()); }finally{ if(session!=null) session.close(); if(factory!=null) factory.close(); } } }
Hibernate配置文件hibernate.cfg.xml:
     com.mysql.jdbc.Driver jdbc:mysql://localhost/hbshards root dev 10 true org.hibernate.dialect.MySQLDialect validate   
项目结构如下图:


运行测试
用Eclipse编译class文件,将Main.class、ContactEntity.class以及hibernate.cfg.xml、ContactEntity.hbm.xml 拷贝到lib目录中,运行Main.class结果如下:

评论: 0 查看评论发表评论

找优秀程序员,就在博客园

最新新闻:
· Intel制程大腕Borh:摩尔定律的新动向(2010-04-12 23:41)
· 惠普完成收购3Com公司 H3C正式并入惠普(2010-04-12 23:19)
· 全方位革新:Adobe CS5家族正式发布(2010-04-12 23:18)
· Google收购英国视觉搜索公司PLink(2010-04-12 23:03)
· 互联网客户端崛起:流量之外的价值(2010-04-12 22:26)

编辑推荐:[图文直播]Visual Studio 2010 全球发布会

网站导航:博客园首页 个人主页 新闻 闪存 小组 博问 社区 知识库

图文资讯
广告赞助商