• (转)最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1)


    最近一直有朋友在问,最新版的Spring、Struts、Hibernate整合老是有问题,昨晚大概看了一下。从Hibernate 4 开始,本身已经很好的实现了数据库事务模块,而Spring也把Hibernate4之后的HibernateDaoSupport去掉了,Spring建议使用官方的HibernateAPI进行操作。这样一来,以前习惯使用HibernateDaoSupport来操作的人来说刚刚开始可能有些不习惯。我根据官方的说明,大概的整合一下,高手可以路过,给刚上路的朋友们。
    现在把主要的代码和配置贴出来,供大家参考,其它配置文件和代码和以前没有什么大变化,直接就能用,主要就是Dao。
    
    Web.xml
    
    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
        id="WebApp_ID" version="3.0">  
        <display-name>Eriloan_com</display-name>  
        <session-config>  
            <session-timeout>30</session-timeout>  
        </session-config>  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:/spring-config/applicationContext-*.xml</param-value>  
        </context-param>  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  
        <listener>  
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>  
        </listener>  
        <listener>  
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
        </listener>  
        <listener>  
            <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>  
        </listener>  
        <filter>  
            <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
            <init-param>  
                <param-name>forceEncoding</param-name>  
                <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>encodingFilter</filter-name>  
            <url-pattern>  
    public class DaoImpl implements IDao{  
        protected Logger log = Logger.getLogger(DaoImpl.class);  
      
         
        private SessionFactory sessionFactory;  
      
         
        public void addObject(Object obj) throws DaoException{  
            log.debug("BaseDao addObject object " + obj);  
            try{  
                sessionFactory.getCurrentSession().save(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象添加记录异常,请联系管理员!",e);  
            }  
        }  
      
         
        public void dbFlush() throws DaoException{  
            log.debug("BaseDao addObject dbFlush");  
            try{  
                sessionFactory.getCurrentSession().flush();  
            }catch(Exception e){  
                throw new DaoException("刷新缓存失败,请联系管理员!",e);  
            }  
        }  
      
         
        public String addObjectPK(Object obj) throws DaoException{  
            log.debug("BaseDao addObjectPK object " + obj);  
            String id = null;  
            try{  
                id = (String) sessionFactory.getCurrentSession().save(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象添加记录异常,请联系管理员!",e);  
            }  
            return id;  
        }  
      
         
        public void deleteObject(Object obj) throws DaoException{  
            log.debug("BaseDao deleteObject object " + obj);  
            try{  
                sessionFactory.getCurrentSession().delete(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象删除记录异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings({"rawtypes"})  
        public void deleteObject(Class cls,Serializable id) throws DaoException{  
            log.debug("BaseDao deleteObject Class " + cls.getName() + " id " + id.toString());  
            try{  
                this.deleteObject(sessionFactory.getCurrentSession().get(cls,id));  
            }catch(Exception e){  
                throw new DaoException("根据传入对象与ID删除记录异常,请联系管理员!",e);  
            }  
        }  
      
         
        public void updateObject(Object obj) throws DaoException{  
            log.debug("BaseDao updateObject object " + obj);  
            try{  
                sessionFactory.getCurrentSession().update(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象更新记录异常,请联系管理员!");  
            }  
        }  
      
         
        public String updateObjectPK(Object obj) throws DaoException{  
            log.debug("BaseDao updateObjectPK object " + obj);  
            String id = null;  
            try{  
                id = this.addObjectPK(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象更新记录异常,请联系管理员!",e);  
            }  
            return id;  
        }  
      
         
        public void addOrUpdate(Object obj) throws DaoException{  
            log.debug("BaseDao updateObjectPK object " + obj);  
            try{  
                sessionFactory.getCurrentSession().saveOrUpdate(obj);  
            }catch(Exception e){  
                throw new DaoException("根据传入对象保存数据异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings({"rawtypes"})  
        public Object findObjectById(Class cls,Serializable id) throws DaoException{  
            log.debug("BaseDao findObjectById Class " + cls.getName() + " id " + id.toString());  
            Object obj = null;  
            try{  
                obj = sessionFactory.getCurrentSession().get(cls,id);  
            }catch(Exception e){  
                throw new DaoException("根据对象及ID查询记录异常,请联系管理员!",e);  
            }  
            return obj;  
        }  
      
         
        @SuppressWarnings({"rawtypes"})  
        public List findAllData(Class cls) throws DaoException{  
            log.debug("BaseDao findAllData Class " + cls.getName());  
            List list = null;  
            try{  
                list = sessionFactory.getCurrentSession().createQuery(" from " + cls.getName() + "").list();  
            }catch(Exception e){  
                throw new DaoException("根据对象查询记录异常,请联系管理员!",e);  
            }  
            return list;  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public List findHQLObject(String hql) throws DaoException{  
            try{  
                return sessionFactory.getCurrentSession().createQuery(hql).list();  
            }catch(Exception e){  
                throw new DaoException("根据传入条件语句查询记录异常,请联系管理员!");  
            }  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public List findListByHqlName(IHqlProviderSet hqlProviderSet,String queryName,Map paramMap) throws DaoException{  
            String hql;  
            try{  
                hql = hqlProviderSet.getHqlByQryName(queryName);  
                Query query = sessionFactory.getCurrentSession().createQuery(hql);  
                if(paramMap != null){  
                    hqlArgs(paramMap,query);  
                }  
      
                return query.list();  
            }catch(Exception e){  
                throw new DaoException("按HQL提供者别名与条件查询集合异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public List findListByHqlName(IHqlProviderSet hqlProviderSet,String queryName,Map paramMap,PageEntity page) throws DaoException{  
            String hql;  
            try{  
                hql = hqlProviderSet.getHqlByQryName(queryName);  
      
                Query query = sessionFactory.getCurrentSession().createQuery(hql);  
      
                if(paramMap != null){  
                    hqlArgs(paramMap,query);  
                }  
      
                query.setFirstResult((page.getPageNo() - 1) * page.getPageSize());  
                query.setMaxResults(page.getPageSize());  
      
                return query.list();  
            }catch(Exception e){  
                throw new DaoException("按HQL提供者别名、条件、分页信息查询集合异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public int findIntRowCountByHqlName(Class cls) throws DaoException{  
            try{  
                Query query = sessionFactory.getCurrentSession().createQuery(" select count(c.id) from " + cls.getName() + " c ");  
                List list = query.list();  
                int rowCount = ((Number) list.get(0)).intValue();  
                return rowCount;  
            }catch(Exception e){  
                throw new DaoException("查询记录总数异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public int findIntRowCountByHqlName(IHqlProviderSet hqlProviderSet,String queryName,Map paramMap) throws DaoException{  
            String hql;  
            try{  
                hql = hqlProviderSet.getHqlByQryName(queryName);  
                Query query = sessionFactory.getCurrentSession().createQuery(hql);  
                if(paramMap != null){  
                    hqlArgs(paramMap,query);  
                }  
                List list = query.list();  
                int rowCount = ((Number) list.get(0)).intValue();  
                return rowCount;  
            }catch(Exception e){  
                throw new DaoException("执行带参数的查询记录总数异常,请联系管理员!",e);  
            }  
        }  
      
         
        @SuppressWarnings("rawtypes")  
        public void hqlArgs(Map argsMap,Query query){  
            Iterator itKey = argsMap.keySet().iterator();  
            while(itKey.hasNext()){  
                String key = (String) itKey.next();  
                @SuppressWarnings("unused")  
                Object obj = argsMap.get(key);  
                if(argsMap.get(key) instanceof List){  
                    query.setParameterList(key,(List) argsMap.get(key));  
                }else{  
                    query.setParameter(key,argsMap.get(key));  
                }  
            }  
        }  
      
        public SessionFactory getSessionFactory(){  
            return sessionFactory;  
        }  
      
        public void setSessionFactory(SessionFactory sessionFactory){  
            this.sessionFactory = sessionFactory;  
        }  
    }  
  • 相关阅读:
    FCKEditor配置
    在线文档编辑器原理
    DVD格式(vob)文件转换avi,转换后可嵌入HTML中播放
    Javascript小技巧
    sql server 查询当前记录的前一条和后一条记录
    [翻译]开发一个自己的HTML在线编辑器(二)
    在线编辑器(4)TAB键缩进功能
    IIS不能浏览ASP页面
    C#枚举类型的使用《转》
    C#中泛型使用《转》
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/4006008.html
Copyright © 2020-2023  润新知