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

简介: 最近一直有朋友在问,最新版的Spring、Struts、Hibernate整合老是有问题,昨晚大概看了一下。从Hibernate 4 开始,本身已经很好的实现了数据库事务模块,而Spring也把Hibernate4之后的HibernateDaoSupport去掉了,Spring建议使用官方的HibernateAPI进行操作。
最近一直有朋友在问,最新版的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;  
    }  
}  

 

相关文章
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——updateEmp.jsp
ssh(Spring+Spring mvc+hibernate)——updateEmp.jsp
9 0
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——updateDept.jsp
ssh(Spring+Spring mvc+hibernate)——updateDept.jsp
11 0
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——showEmp.jsp
ssh(Spring+Spring mvc+hibernate)——showEmp.jsp
10 0
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
9 0
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——saveEmp.jsp
ssh(Spring+Spring mvc+hibernate)——saveEmp.jsp
8 0
|
2月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
8 0
|
1月前
|
安全 Linux Shell
Linux SSH(Secure Shell)服务
Linux SSH提供安全网络协议,使用公钥加密技术确保远程服务传输安全。OpenSSH是实现SSH服务的免费开源工具,允许用户加密连接远程登录Linux服务器执行任务。SSH比Telnet更安全,防止数据被截获。SSH还支持端口转发和隧道,广泛应用于系统管理和网络维护,是安全远程访问服务器的重要工具。
26 1
|
2月前
|
安全 Shell Linux
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
45 0
|
19天前
|
Linux 网络安全 数据安全/隐私保护
SSH工具连接远程服务器或者本地Linux系统
SSH工具连接远程服务器或者本地Linux系统
19 0
|
7天前
|
存储 安全 Linux