码迷,mamicode.com
首页 > 其他好文 > 详细

开发积累—泛型工具类

时间:2014-05-22 09:37:48      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:java   ssh2   web程序   

前言:使用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

 

开发积累—泛型工具类,布布扣,bubuko.com

开发积累—泛型工具类

标签:java   ssh2   web程序   

原文地址:http://blog.csdn.net/u011331844/article/details/26288343

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!