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

[java] view plain copy
  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

[java] view plain copy
    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.     }  

转载于:https://www.cnblogs.com/ZHANYU/p/3618803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值