纯Hibernate使用-不用spring事务管理

来源:互联网 发布:隐藏文件的软件 编辑:程序博客网 时间:2024/06/05 07:42

最近一直在做系统测试,检验功能,测试bug。发现一直都是用spring来管理配置hibernate依赖注入,来管理Hibernate事务。但是有的程序需要手动Main函数启动,而采用spring加载的话,就会触发很多类,影响性能,还有当spring里面写了触发器后,那么随着你的main函数运行,你的触发器也得到了启动,那么这个会造成多个进程开启了触发器。

    当离开了spring管理时,发现Hibernate居然不知道怎么调用了,参考了网上一些大牛的文章,自己动手实现了,无需spring管理来进行Hibernate调用。

 

    直接上代码:1.工具类,2.xml配置文件,3.实体类,注释方式  4.测试功能类

 

Hibernateutil .java代码  收藏代码
  1. package com.t.common.hibernate;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.cfg.Configuration;  
  9.   
  10. public final class HibernateUtil {  
  11.       
  12.     private static SessionFactory sessionFactory;  
  13.     private static ThreadLocal session = new ThreadLocal();  
  14.       
  15.     public HibernateUtil(){  
  16.           
  17.     }  
  18.     static {  
  19.         Configuration cfg = new Configuration();  
  20.         cfg.configure("hibernate.common.xml");  
  21.         sessionFactory = cfg.buildSessionFactory();  
  22.     }  
  23.       
  24.     public static Session getThreadLocalSession(){  
  25.         Session s = (Session) session.get();  
  26.         if(s==null){  
  27.             s=getSession();  
  28.             session.set(s);  
  29.         }  
  30.         return s;  
  31.     }  
  32.     public static void closeSession(){  
  33.         Session s = (Session)session.get();  
  34.         if(s!=null){  
  35.             s.close();  
  36.             session.set(null);  
  37.         }  
  38.     }  
  39.     public static SessionFactory getSessionFactory(){  
  40.         return sessionFactory;  
  41.     }  
  42.     public static Session getSession(){  
  43.         return sessionFactory.openSession();  
  44.     }  
  45.     //增删改查  
  46.     //当需要事务一致性的时候,需要回滚,比如商品系统  
  47.     /*  
  48.      * public static void add(Object entity){  
  49.         Session s =null;  
  50.         Transaction tx = null;  
  51.         try {  
  52.             s = HibernateUtil.getSession();  
  53.             tx = s.beginTransaction();  
  54.             s.save(entity);  
  55.             tx.commit();  
  56.         } catch (HibernateException e) {    
  57.             if(tx != null){    
  58.                 tx.rollback();    
  59.             }    
  60.             throw e;    
  61.         }finally {  
  62.             if(s!=null){  
  63.                 s.close();  
  64.             }  
  65.         }  
  66.     }  
  67.      */  
  68.     public static void add(Object entity){  
  69.         Session s =null;  
  70.         Transaction tx = null;  
  71.         try {  
  72.             s = HibernateUtil.getSession();  
  73.             tx = s.beginTransaction();  
  74.             s.save(entity);  
  75.             tx.commit();  
  76.         } finally {  
  77.             if(s!=null){  
  78.                 s.close();  
  79.             }  
  80.         }  
  81.     }  
  82.     public static void delete(Object entity){  
  83.         Session s =null;  
  84.         Transaction tx = null;  
  85.         try {  
  86.             s = HibernateUtil.getSession();  
  87.             tx = s.beginTransaction();  
  88.             s.delete(entity);  
  89.             tx.commit();  
  90.         } finally {  
  91.             if(s!=null){  
  92.                 s.close();  
  93.             }  
  94.         }  
  95.     }  
  96.     public static Object get(Class clazz, Serializable id){  
  97.         Session s =null;  
  98.         try {  
  99.             s = HibernateUtil.getSession();  
  100.             Object obj = s.get(clazz, id);  
  101.             return obj;  
  102.         } finally {  
  103.             if(s!=null){  
  104.                 s.close();  
  105.             }  
  106.         }  
  107.     }  
  108. }  

 

Hibernate.common.xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6. <session-factory>  
  7.     <property name="hibernate.bytecode.use_reflection_optimizer">  
  8.         false  
  9.     </property>  
  10.     <property name="hibernate.connection.driver_class">  
  11.         oracle.jdbc.driver.OracleDriver  
  12.     </property>  
  13.     <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>  
  14.     <property name="connection.username">baike</property>  
  15.     <property name="connection.password">baike</property>  
  16.     <property name="hibernate.dialect">  
  17.         org.hibernate.dialect.Oracle10gDialect  
  18.     </property>  
  19.     <property name="hibernate.search.autoregister_listeners">  
  20.         false  
  21.     </property>  
  22.     <property name="hibernate.show_sql">true</property>  
  23.       
  24.     <!-- 映射类 -->  
  25.     <mapping class="com.t.common.hibernate.BaikeUser"/>  
  26.       
  27. </session-factory>  
  28. </hibernate-configuration>  

 

Baikeuser .java代码  收藏代码
  1. package com.t.common.hibernate;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.GenerationType;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.SequenceGenerator;  
  9. import javax.persistence.Table;  
  10.   
  11.   
  12. @Entity  
  13. @Table(name="T_USER")  
  14. public class BaikeUser {  
  15.       
  16.     private Long id ;   
  17.     private String username;  
  18.     private String password;  
  19.       
  20.     @Id  
  21.     @SequenceGenerator(name="generator",allocationSize=1,initialValue=1,sequenceName="SEQ_USER_ID")  
  22.     @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="generator")  
  23.     @Column(name="ID")  
  24.     public Long getId() {  
  25.         return id;  
  26.     }  
  27.     @Column(name="username")  
  28.     public String getUsername() {  
  29.         return username;  
  30.     }  
  31.     @Column(name="password")  
  32.     public String getPassword() {  
  33.         return password;  
  34.     }  
  35.     public void setId(Long id) {  
  36.         this.id = id;  
  37.     }  
  38.     public void setUsername(String username) {  
  39.         this.username = username;  
  40.     }  
  41.     public void setPassword(String password) {  
  42.         this.password = password;  
  43.     }  
  44. }  

 

Testuser.java代码  收藏代码
  1. package com.t.common.hibernate;  
  2.   
  3. public class TestUSer {  
  4.     public static void main(String[] args) {  
  5.           
  6.         BaikeUser user = new BaikeUser();  
  7.         user.setId(1L);  
  8.         user.setUsername("cjp1989");  
  9.         user.setPassword("12345");  
  10.         HibernateUtil.add(user);  
  11.     }  
  12. }  

 

   1.注意事项:xml配置文件可以自定义的,但是<session-factory> 单独使用时,没有name属性

原创粉丝点击