zoukankan      html  css  js  c++  java
  • HibernateSessionFactoryUtil类的增,删,改,查

    HibernateSessionFactoryUtil类提供了一个getSessionFactory()静态方法。

    通过调用此方法可以返回一个SessionFactory对象。

    在其他地方创建Session对象的时候:

    只需要这样一句代码:

    Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

    这样既可获得Session对象。

    在hibernate.cfg.xml  

    <session-factory></session-factory>中加入下列代码:

    <property name="current_session_context_class">thread</property>

    注意<property></property>必须要放在<mapping></mapping>上面

    HibernateTest类封装了对数据操作更删改查的基本方法。

    根据不同的业务逻辑环境代码作出相应的变更即可使用。


    HibernateSessionFactoryUtil.java

    1. package com.xiami.util;  
    2.   
    3. import org.hibernate.SessionFactory;   
    4. import org.hibernate.cfg.Configuration;  
    5.   
    6. public class HibernateSessionFactoryUtil {  
    7.     private static final SessionFactory sessionFactory;  
    8.     static {  
    9.         try {  
    10.             sessionFactory = new Configuration().configure().buildSessionFactory();  
    11.         } catch (Throwable ex) {  
    12.               /*
                   * 需要 捕获Throwable对象, 否则捕获不到 Error及其子类,以及NoClassDefFoundError类型的错误
                   */
    13.             throw new ExceptionInInitializerError(ex);  
    14.         }  
    15.     }  
    16.   
    17.     private HibernateSessionFactoryUtil() {  
    18.           
    19.     }  
    20.   
    21.     public static SessionFactory getSessionFactory() {  
    22.         return sessionFactory;  
    23.     }  
    24. }  



    HibernateTest.java

      1. package com.xiami.examples;  
      2.   
      3. import org.hibernate.Session;  
      4. import org.hibernate.Transaction;  
      5.   
      6. import com.xiami.util.HibernateSessionFactoryUtil;  
      7.   
      8. public class HibernateTest {  
      9.     public static void main(String args[]){  
      10.         HibernateTest test = new HibernateTest();  
      11.           
      12.         //增加一条记录]  
      13. //      Guestbook gb = new Guestbook();  
      14. //      gb.setContent("我是内容");  
      15. //      gb.setCreatedTime("2012-03-17");  
      16. //      gb.setEmail("kalision@foxmail.com");  
      17. //      gb.setName("mr.zhou");  
      18. //      gb.setPhone("12345678912");  
      19. //      gb.setTitle("我的信息");  
      20. //      test.addGuestbook(gb);  
      21.           
      22.         //删除一条记录  
      23. //      test.deleteGuestbook(7);  
      24.           
      25.         //修改更新一条记录  
      26. //      Guestbook gb = test.getGuestbook(1);  
      27. //      gb.setName("admin");  
      28. //      System.out.println(test.updateGuest(gb));  
      29.   
      30.         //得到一条记录  
      31.         Guestbook gb = test.getGuestbook(1);  
      32.         System.out.println(gb.getName());  
      33.     }  
      34.       
      35.     /* 
      36.      * 增加一条记录的方法 
      37.      */  
      38.     public void addGuestbook(Guestbook gb){  
      39.         Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
      40.         Transaction tx = session.beginTransaction();  
      41.         session.save(gb);  
      42.         tx.commit();  
      43.     }  
      44.       
      45.     /* 
      46.      * 删除一条记录的方法 
      47.      */  
      48.     public void deleteGuestbook(Integer id){  
      49.         Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
      50.         Transaction tx = session.beginTransaction();  
      51.           
      52.         //第一种方法.先得到对应id的记录的对象,然后在删除此对象对应的记录。  
      53. //      Guestbook gb = (Guestbook) session.get(Guestbook.class, new Integer(id));  
      54. //      tx.commit();  
      55. //      session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
      56. //      tx = session.beginTransaction();  
      57. //      session.delete(gb);  
      58. //      tx.commit();  
      59.           
      60.         //第二种方法。直接调用本类中的getGuestbook()方法来得到要对应id的对象。直接就删除了。  
      61.         Guestbook gb = getGuestbook(id);  
      62.         session.delete(gb);  
      63.         tx.commit();  
      64.     }  
      65.       
      66.     /* 
      67.      * 修改一条记录的方法(更新) 
      68.      */  
      69.     public boolean updateGuest(Guestbook gb){  
      70.        
      71.         Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
      72.         Transaction tx = session.beginTransaction();  
      73.         try {
      74.              session.update(gb);
      75.              tx.commit();
      76.              return true;
      77.           }catch(Exception e){
      78.               return false;
      79.            }
      80.     }  
      81.       
      82.     /* 
      83.      * 查询一条记录的方法 
      84.      */  
      85.     public Guestbook getGuestbook(Integer id){  
      86.         Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();  
      87.         Transaction tx = session.beginTransaction();  
      88.         Guestbook gb = new Guestbook();  
      89.         gb = (Guestbook) session.get(Guestbook.classnew Integer(id));  
      90.         return gb;  
      91.     }  

  • 相关阅读:
    我的WCF之旅(1):创建一个简单的WCF程序
    网页设计中颜色的搭配
    CSS HACK:全面兼容IE6/IE7/IE8/FF的CSS HACK
    UVa 1326 Jurassic Remains
    UVa 10340 All in All
    UVa 673 Parentheses Balance
    UVa 442 Matrix Chain Multiplication
    UVa 10970 Big Chocolate
    UVa 679 Dropping Balls
    UVa 133 The Dole Queue
  • 原文地址:https://www.cnblogs.com/ZHANYU/p/3618803.html
Copyright © 2011-2022 走看看