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

  • 相关阅读:
    Windows JScript 在 游览器 中运行 调试 Shell 文件系统
    autohotkey 符号链接 软连接 symbolink
    软链接 硬链接 测试
    SolidWorks 修改 基准面 标准坐标系
    手机 路径 WebDAV 映射 驱动器
    Win10上手机路径
    explorer 命令行
    单位公司 网络 封锁 屏蔽 深信 AC
    cobbler自动化部署原理篇
    Docker四种网络模式
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6991261.html
Copyright © 2011-2022 走看看