Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063182
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2011-03-22 22:54:46

    今天学习了下hibernate,写了个小的手工程序,总结下,
    首先创建数据库表:
    create table increment_testr(id bigint not null, name char(10), primary key(id));
    eclipse下,新建工程。
    新建数据库表的映射,这里使用手工方式完成:
 IncrementTester.java
   
  1. public class IncrementTester {
  2.     private Long id;
  3.     private String name;
  4.     public IncrementTester(){}
  5.     public IncrementTester(String name){
  6.         this.name = name;
  7.     }
  8.     
  9.     public Long getId(){
  10.         return this.id;
  11.     }
  12.     private void setId(Long id){
  13.         this.id = id;
  14.     }
  15.     public String getName(){
  16.         return this.name;
  17.     }
  18.     public void setName(String name){
  19.         this.name = name;
  20.     }
  21. }

  

 对应编写映射xml文件

 IncrementTester.hbm.xml

  1. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  2. "">
  3. private

   实现具体功能的类BussinessService

  1. import java.lang.reflect.*;
  2. import org.hibernate.*;
  3. import org.hibernate.cfg.*;
  4. import java.io.*;
  5. import java.sql.*;
  6. import java.util.*;

  7. public class BussinessService {
  8.     public static SessionFactory sessionFactory;
  9.     static{
  10.         try{
  11.             Configuration config = new Configuration().configure();
  12.             sessionFactory = config.buildSessionFactory();
  13.         }catch(Exception e){
  14.             e.printStackTrace();
  15.         }
  16.     }
  17.     
  18.     public void findAllObjects(String className){
  19.         Session session = sessionFactory.openSession();
  20.         Transaction tx = null;
  21.         try{
  22.             tx = session.beginTransaction();
  23.             List objects = session.createQuery("from "+className).list();
  24.             for(Iterator it = objects.iterator();it.hasNext();){
  25.                 Long id = new Long(0);
  26.                 IncrementTester xx = (IncrementTester)it.next();
  27.                 id=xx.getId();
  28.                 
  29.                 System.out.println("ID of "+className+":"+id+" name: "+xx.getName());
  30.             }
  31.             tx.commit();
  32.         }catch(Exception e){
  33.             e.printStackTrace();
  34.         }finally{
  35.             session.close();
  36.         }
  37.     }
  38.     
  39.     public void saveObject(Object object){
  40.         Session session = sessionFactory.openSession();
  41.         Transaction tx = null;
  42.         try{
  43.             tx = session.beginTransaction();
  44.             session.save(object);
  45.             tx.commit();
  46.         }catch(Exception e){
  47.             e.printStackTrace();
  48.             if(tx != null){
  49.                 tx.rollback();
  50.             }
  51.         }finally{
  52.             session.close();
  53.         }
  54.     }
  55.     
  56.     public void deleteAllObject(String className){
  57.         Session session = sessionFactory.openSession();
  58.         Transaction tx = null;
  59.         
  60.         try{
  61.             tx = session.beginTransaction();
  62.             Query query=session.createQuery("delete from "+className);
  63.             query.executeUpdate();
  64.             tx.commit();
  65.         }catch(Exception e){
  66.             e.printStackTrace();
  67.             if(tx!=null){
  68.                 tx.rollback();
  69.             }
  70.         }finally{
  71.             session.close();
  72.         }
  73.     }
  74.     
  75. }

   实现主函数:

 

  1. public class test {

  2.     /**
  3.      * @param args
  4.      */
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub

  7.         String name="IncrementTester";
  8.         BussinessService aa = new BussinessService();
  9.         aa.deleteAllObject(name);
  10.         Object o1 = null;
  11.         try {
  12.             o1 = Class.forName(name).newInstance();
  13.             ((IncrementTester)o1).setName("caijie");
  14.             aa.saveObject(o1);
  15.             o1 = Class.forName(name).newInstance();
  16.             ((IncrementTester)o1).setName("gufeng");
  17.             aa.saveObject(o1);
  18.             
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.         aa.findAllObjects(name);
  23.     }

  24. }

    hibernate配置文件:hibernate.cfg.xml

 

  1. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  2. "">
  3. org.hibernate.dialect.MySQLDialect
  4. com.mysql.jdbc.Driver
  5. jdbc:mysql://localhost:3306/test
  6. root
  7. root
  8. true

 增加相应的库,运行后成功得到结果:

 

  1. Hibernate: delete from INCREMENT_TESTR
  2. Hibernate: select max(ID) from INCREMENT_TESTR
  3. Hibernate: insert into INCREMENT_TESTR (NAME, ID) values (?, ?)
  4. Hibernate: insert into INCREMENT_TESTR (NAME, ID) values (?, ?)
  5. Hibernate: select incrementt0_.ID as ID0_, incrementt0_.NAME as NAME0_ from INCREMENT_TESTR incrementt0_
  6. ID of IncrementTester:1 name: caijie
  7. ID of IncrementTester:2 name: gufeng

 

 

 

阅读(829) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~