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. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值