hibernate增删改查

使用hibernate肯定要配置hibernate环境,之前的文章里提过,这里就不过多的赘述,直接进行增删改查的程序

首先是书写单例模式的sessionfactory

[java]  view plain  copy
  1. package Usertest;  
  2.   
  3. import org.hibernate.HibernateException;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7. public class HibernateUtil {  
  8.     private static Configuration cfg;  
  9.     private static SessionFactory sf;  
  10.       
  11.     static  
  12.     {  
  13.         try {  
  14.             cfg = new Configuration().configure();  
  15.             sf = cfg.buildSessionFactory();  
  16.         } catch (HibernateException e) {  
  17.             // TODO Auto-generated catch block  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.       
  22.     public static SessionFactory getSessionFactory(){  
  23.         return sf;  
  24.     }  
  25. }  

测试类:

[java]  view plain  copy
  1. package Usertest;  
  2.   
  3. import org.hibernate.HibernateException;  
  4. import org.hibernate.Session;  
  5. import org.hibernate.SessionFactory;  
  6. import org.hibernate.Transaction;  
  7. import org.hibernate.cfg.Configuration;  
  8. import org.junit.Test;  
  9.   
  10. import text.User;  
  11.   
  12.   
  13.   
  14.   
  15. public class UserDAO {  
  16.       
  17.     @Test  
  18.       
  19.     public void SaveUser(){//保存用户  
  20.         // TODO Auto-generated method stub  
  21.         SessionFactory sf = null;     
  22.         Session session = null;  
  23.         Transaction ts = null;  
  24.           
  25.       
  26.         try {  
  27.             sf = HibernateUtil.getSessionFactory();  
  28.             session = sf.getCurrentSession();  
  29.             ts = session.beginTransaction();  
  30.   
  31.             User user = new User();  
  32.             user.setAge(18);  
  33.             user.setUsername("小王");  
  34.             user.setGender("男");  
  35.             user.setPassword("1245456");  
  36.             session.save(user);  
  37.               
  38.             ts.commit();  
  39.         } catch (HibernateException e) {  
  40.             // TODO Auto-generated catch block  
  41.             if(ts != null)  
  42.             {  
  43.                 ts.rollback();  
  44.             }  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48.     @Test  
  49.     public void getUser(){//查询用户  
  50.         // TODO Auto-generated method stub  
  51.         SessionFactory sf = null;     
  52.         Session session = null;  
  53.         Transaction ts = null;  
  54.       
  55.         try {  
  56.             sf = HibernateUtil.getSessionFactory();  
  57.             session = sf.getCurrentSession();  
  58.             ts = session.beginTransaction();  
  59.             User user =session.get(User.class1);//session的get方法  
  60.             System.out.println(user.getUsername());  
  61.               
  62.             ts.commit();  
  63.         } catch (HibernateException e) {  
  64.             // TODO Auto-generated catch block  
  65.             if(ts != null)  
  66.             {  
  67.                 ts.rollback();  
  68.             }  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.       
  73.     @Test  
  74.     public void updateUser(){//修改用户  
  75.         // TODO Auto-generated method stub  
  76.         SessionFactory sf = null;     
  77.         Session session = null;  
  78.         Transaction ts = null;  
  79.       
  80.         try {  
  81.             sf = HibernateUtil.getSessionFactory();  
  82.             session = sf.getCurrentSession();  
  83.             ts = session.beginTransaction();  
  84.             User user1=session.get(User.class2);  
  85.             user1.setPassword("1514010601");  
  86.             ts.commit();  
  87.         } catch (HibernateException e) {  
  88.             // TODO Auto-generated catch block  
  89.             if(ts != null)  
  90.             {  
  91.                 ts.rollback();  
  92.             }  
  93.             e.printStackTrace();  
  94.         }  
  95.     }  
  96.     @Test  
  97.     public void delUser(){//删除用户  
  98.         // TODO Auto-generated method stub  
  99.         SessionFactory sf = null;     
  100.         Session session = null;  
  101.         Transaction ts = null;  
  102.         User user =new User();  
  103.         user.setUsername("Jenny");  
  104.         user.setAge(11);  
  105.         user.setGender("male");  
  106.         user.setPassword("123456");  
  107.         try {  
  108.             sf = HibernateUtil.getSessionFactory();  
  109.             session = sf.getCurrentSession();  
  110.             ts = session.beginTransaction();  
  111.             session.delete(user);  
  112.             System.out.println("删除成功了!");  
  113.             ts.commit();  
  114.         } catch (HibernateException e) {  
  115.             // TODO Auto-generated catch block  
  116.             if(ts != null)  
  117.             {  
  118.                 ts.rollback();  
  119.             }  
  120.             e.printStackTrace();  
  121.         }  
  122.     }  
  123. }  

增加

删除


修改


查询

扫描二维码关注公众号,回复: 1573829 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_36937005/article/details/80665335