zoukankan      html  css  js  c++  java
  • Hibernate系列4-----之删除

    1.和它的增改查兄弟不同,多了个until包定义了HibernateUntil类,让我们来一起看看吧

     1 public class HibernateUntil {
     2     private static Configuration cfg=new Configuration().configure();
     3    private static SessionFactory factory=cfg.buildSessionFactory();
     4 
     5     //1.方法返回session  静态成员变量不能直接使用非静态成员
     6     //Session依赖于Session工厂,工厂依赖于Configure
     7     public static Session getSession(){
     8       return factory.openSession();
     9 
    10     }
    11     //2.关闭session
    12     public static void closeSession(){
    13         getSession().close();
    14     }
    View Code

    这里也考察了一个知识点静态变量与非静态变量    要想学好就得把基础打好!

    2.接下来我们来写一下测试类

    1 @Test
    2     public void testhibernate() {
    3         deleteStudent();//删除学生
    4     }
    View Code
     1  private void deleteStudent() {
     2       Session session= HibernateUntil.getSession();
     3       Student student=new Student();
     4       student.setSid(2);
     5       //开启事务
     6       Transaction tx=session.beginTransaction();
     7       session.delete(student);
     8       tx.commit();
     9       HibernateUntil.closeSession();
    10         System.out.println("success  ok");
    11     }
    View Code

    恩,Hibernate系列增删改查就此结束,感谢同行们的阅览哈!

  • 相关阅读:
    Service、chkconfig命令
    mongoDB 入门
    HTTP 缓存
    MIME类型记录
    CSS3 动画 思维导图
    部署Seafile服务
    AngularJS 学习笔记
    Bootstrap3 学习笔记
    CSS 弹性盒
    传送门(portal)
  • 原文地址:https://www.cnblogs.com/ruyan886621/p/7422243.html
Copyright © 2011-2022 走看看