Eclipse中Hibernate配置

path路徑下導入hibernate/lib/required目錄下所有的jar包。以及連接數據庫的Jar包

層次結構

hibernate.cfg.xml
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.           
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.         <property name="show_sql">true</property>  
  9.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  10.         <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>  
  11.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>  
  12.         <property name="hibernate.connection.username">root</property>  
  13.         <property name="hibernate.connection.password">mysqladmin</property>  
  14.         <mapping resource="org/zbq/bean/User.hbm.xml"/>  
  15.     </session-factory>  
  16. </hibernate-configuration>  
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<User> listUsers() throws Exception{  
  53.         List<User> 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<User>)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. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  5.   
  6. <hibernate-mapping>  
  7.     <class name="org.zbq.bean.User" table="user">  
  8.         <id name="id" column="id" type="int">  
  9.             <generator class="increment"/>  
  10.         </id>  
  11.           
  12.         <property name="name" column="name" type="string"/>  
  13.         <property name="pass" column="pass" type="string"/>         
  14.     </class>  
  15. </hibernate-mapping>  

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<User> listUsers() throws Exception{  
  53.         List<User> 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<User>)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<User> list = HibernateUtil.listUsers();  
  18.         for(User u : list){  
  19.             System.out.println(u.getName() + ":" + u.getPass());  
  20.         }  
  21.     }  
  22.   
  23. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章