zoukankan      html  css  js  c++  java
  • 开发积累—泛型工具类

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

  • 相关阅读:
    Response生成注册验证码实现例子02
    Mysql 自增字段起始值auto_increment的修改方法
    elite核心库的加载方式及自动加载类库
    elite核心类库之事件类
    wamp速度缓慢的解决办法
    Dwzdialog中批量提交的问题处理
    PHP中缀表达式与逆波兰式的计算(用于工资项目等四则计算)
    PHP工资计算之逆波兰式
    elite核心类库之模板类
    PHP soap访问接口出错汇总及解决办法
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6991261.html
Copyright © 2011-2022 走看看