Eclipse中Hibernate配置

path路径下导入 hibernate /lib/required目录下所有的jar包。以及连接 数据库 的Jar包
Eclipse中Hibernate配置_第1张图片
层次结构
Eclipse中Hibernate配置_第2张图片
hibernate.cfg.xml
[html]   view plain   copy
  1.  version="1.0" encoding="UTF-8"?>  
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  4.           
  5.   
  6.       
  7.          name="show_sql">true  
  8.          name="hibernate.dialect">org.hibernate.dialect.MySQLDialect  
  9.          name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver  
  10.          name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate  
  11.          name="hibernate.connection.username">root  
  12.          name="hibernate.connection.password">mysqladmin  
  13.          resource="org/zbq/bean/User.hbm.xml"/>  
  14.       
  15.   
User. Java
[java]   view plain   copy
  1. package org.zbq.util;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Query;  
  6. import org.hibernate.Session;  
  7. import org.hibernate.SessionFactory;  
  8. import org.hibernate.Transaction;  
  9. import org.hibernate.cfg.Configuration;  
  10. import org.hibernate.service.ServiceRegistry;  
  11. import org.hibernate.service.ServiceRegistryBuilder;  
  12. import org.zbq.bean.User;  
  13.   
  14. public class HibernateUtil {  
  15.     private static SessionFactory sessionFactory;  
  16.       
  17.     static{  
  18.         try{  
  19.             Configuration conf = new Configuration();  
  20.             conf.configure();  
  21.             ServiceRegistry sr = new ServiceRegistryBuilder()  
  22.                                     .applySettings(conf.getProperties())  
  23.                                     .buildServiceRegistry();  
  24.               
  25.             sessionFactory = conf.buildSessionFactory(sr);  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.       
  31.       
  32.     public static void save(User user) throws Exception{  
  33.         Transaction tr = null;  
  34.         Session session = sessionFactory.openSession();  
  35.           
  36.         try{  
  37.             tr = session.beginTransaction();  
  38. //          tr.begin();  
  39.             session.save(user);  
  40.             tr.commit();  
  41.         } catch (Exception e) {  
  42.             if(null != tr){  
  43.                 tr.rollback();  
  44.             }  
  45.             throw e;  
  46.         } finally {  
  47.             session.close();  
  48.         }  
  49.     }  
  50.       
  51.     @SuppressWarnings("unchecked")  
  52.     public static List listUsers() throws Exception{  
  53.         List list = null;  
  54.         Transaction tr = null;  
  55.         Session session = sessionFactory.openSession();  
  56.           
  57.         try{  
  58.             tr = session.beginTransaction();  
  59. //          tr.begin();  
  60.             String sql = "from User";  
  61.             Query query = session.createQuery(sql);  
  62.               
  63.             list = (List)query.list();  
  64.             tr.commit();  
  65.         } catch (Exception e) {  
  66.             e.printStackTrace();  
  67.         } finally {  
  68.             session.close();  
  69.         }  
  70.           
  71.         return list;  
  72.     }  
  73. }  

User.hbm.xml
[html]   view plain   copy
  1.  version="1.0" encoding="UTF-8"?>  
  2.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
  3.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4.   
  5.   
  6.      name="org.zbq.bean.User" table="user">  
  7.          name="id" column="id" type="int">  
  8.              class="increment"/>  
  9.           
  10.           
  11.          name="name" column="name" type="string"/>  
  12.          name="pass" column="pass" type="string"/>         
  13.       
  14.   

HibernateUtil.java
[java]   view plain   copy
  1. package org.zbq.util;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Query;  
  6. import org.hibernate.Session;  
  7. import org.hibernate.SessionFactory;  
  8. import org.hibernate.Transaction;  
  9. import org.hibernate.cfg.Configuration;  
  10. import org.hibernate.service.ServiceRegistry;  
  11. import org.hibernate.service.ServiceRegistryBuilder;  
  12. import org.zbq.bean.User;  
  13.   
  14. public class HibernateUtil {  
  15.     private static SessionFactory sessionFactory;  
  16.       
  17.     static{  
  18.         try{  
  19.             Configuration conf = new Configuration();  
  20.             conf.configure();  
  21.             ServiceRegistry sr = new ServiceRegistryBuilder()  
  22.                                     .applySettings(conf.getProperties())  
  23.                                     .buildServiceRegistry();  
  24.               
  25.             sessionFactory = conf.buildSessionFactory(sr);  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.       
  31.       
  32.     public static void save(User user) throws Exception{  
  33.         Transaction tr = null;  
  34.         Session session = sessionFactory.openSession();  
  35.           
  36.         try{  
  37.             tr = session.beginTransaction();  
  38. //          tr.begin();  
  39.             session.save(user);  
  40.             tr.commit();  
  41.         } catch (Exception e) {  
  42.             if(null != tr){  
  43.                 tr.rollback();  
  44.             }  
  45.             throw e;  
  46.         } finally {  
  47.             session.close();  
  48.         }  
  49.     }  
  50.       
  51.     @SuppressWarnings("unchecked")  
  52.     public static List listUsers() throws Exception{  
  53.         List list = null;  
  54.         Transaction tr = null;  
  55.         Session session = sessionFactory.openSession();  
  56.           
  57.         try{  
  58.             tr = session.beginTransaction();  
  59. //          tr.begin();  
  60.             String sql = "from User";  
  61.             Query query = session.createQuery(sql);  
  62.               
  63.             list = (List)query.list();  
  64.             tr.commit();  
  65.         } catch (Exception e) {  
  66.             e.printStackTrace();  
  67.         } finally {  
  68.             session.close();  
  69.         }  
  70.           
  71.         return list;  
  72.     }  
  73. }  
测试类
[java]   view plain   copy
  1. package org.zbq.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.zbq.bean.User;  
  6. import org.zbq.util.HibernateUtil;  
  7.   
  8. public class HibernatUtilTest {  
  9.   
  10.     public static void main(String[] args) throws Exception {  
  11.         User user = new User();  
  12.         user.setId(2);  
  13.         user.setName("cat");  
  14.         user.setPass("intel123");  
  15.         HibernateUtil.save(user);  
  16.           
  17.         List list = HibernateUtil.listUsers();  
  18.         for(User u : list){  
  19.             System.out.println(u.getName() + ":" + u.getPass());  
  20.         }  
  21.     }  
  22.   
  23. }  

你可能感兴趣的:(Hibernate)