开发积累—泛型工具类

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

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值