首页 > 代码库 > 开发积累—泛型工具类

开发积累—泛型工具类

前言:使用SSH2中使用的泛型工具类,以前写泛型比较麻烦。今天收集到一个工具类,好东呀!!分享给大家,绝对有用。JAVA版的web应用程序使用。

 

 

示例代码:

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

/**

 *Hibernate工具类,用于获取Session

 *@author Li Yongqiang

 */

public class HibernateUtils {

         //声明SessionFactory对象

         privatestatic SessionFactory factory = null;

         //实例化ThreadLocal对象

         privatestatic final ThreadLocal<Session> threadLocal = newThreadLocal<Session>();

         //实例化Configuration对象

         privatestatic Configuration cfg = new Configuration();

         //静态块

         static{

                   try{

                            //加载Hibernate配置文件

                            cfg.configure();

                            //实例化SessionFactory

                            factory= cfg.buildSessionFactory();

                   }catch (HibernateException e) {

                            e.printStackTrace();// 打印异常信息

                   }

         }

         /**

          * 获取Session对象

          * @return Session对象

          */

         publicstatic Session getSession() {

                   //从threadLocal中获取Session

                   Sessionsession = (Session) threadLocal.get();

                   //判断session是否为空或未处于开启状态

                   if(session == null || !session.isOpen()) {

                            if(factory == null) {

                                     rebuildSessionFactory();

                            }

                            //从factory开启一个Session

                            session= (factory != null) ? factory.openSession() : null;

                            threadLocal.set(session);// 将session放入threadLocal中

                   }

                   returnsession;

         }

         /**

          * 获取SessionFactory对象

          * @return SessionFactory对象

          */

         publicstatic SessionFactory getSessionFactory() {

                   returnfactory;

         }

         /**

          * 关闭Session

          * @param session对象

          */

         publicstatic void closeSession() {

                   //从threadLocal中获取Session

                   Sessionsession = (Session) threadLocal.get();

                   //移除threadLocal中的对象

                   threadLocal.remove();

                   if(session != null) {

                            if(session.isOpen()) {

                                     session.close();// 关闭Session

                            }

                   }

         }

         /**

          * 创建SessionFactory对象

          */

         publicstatic void rebuildSessionFactory() {

                   try{

                            //加载Hibernate配置文件

                            cfg.configure();

                            //实例化SessionFactory

                            factory= cfg.buildSessionFactory();

                   }catch (Exception e) {

                            e.printStackTrace();// 打印异常信息

                   }

         }

}

 

使用场景代码;

public class DaoSupport<T> implementsBaseDao<T>{

         //泛型的类型

         protectedClass<T> entityClass = GenericsUtils.getGenericType(this.getClass());

         //Hibernate模板

         @Autowired

         protectedHibernateTemplate template;

        

         publicHibernateTemplate getTemplate() {

                   returntemplate;

         }

         @Override

         publicvoid delete(Serializable ... ids) {

                   for(Serializable id : ids) {

                            Tt = (T) getTemplate().load(this.entityClass, id);

                            getTemplate().delete(t);

                   }

         }

         /**

          * 利用get()方法加载对象,获取对象的详细信息

          */

         @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

         publicT get(Serializable entityId) {

                   return(T) getTemplate().get(this.entityClass, entityId);

         }

         /**

          * 利用load()方法加载对象,获取对象的详细信息

          */

         @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

         publicT load(Serializable entityId) {

                   return(T) getTemplate().load(this.entityClass, entityId);

         }

}

 

如有好的建议,可留言或发至笔者邮箱:fzb_xxzy@163.com