最近一直有朋友在问,最新版的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;
}
}

(转)最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1)的更多相关文章

  1. 【转】SSH中 整合spring和proxool 连接池

    [摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...

  2. ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例

    一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...

  3. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  4. Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合

    前言        转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...

  5. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

  6. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  7. Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)

    搭建的环境:eclispe下面的maven web项目 Struts:    2.5.10 Spring:    4.3.8 Hibernate:   5.1.7 .Final MySQL:   5. ...

  8. Struts2,Spring, Hibernate三大框架SSH的整合步骤

    整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...

  9. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

随机推荐

  1. opencv学习笔记(04)——ROI

    ROI的用法:1.直接相加:2.掩码法 #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgpro ...

  2. IOS开发之──应用之间调用(2)

    在上一篇文章中,讲解了如何在自己应用之间调用问题,今天介绍一下如果调用IOS自带的app的方法 一.调用app store界面方法 在实际开发中,往往要推荐自己其他应用和推荐自己的收费软件,那么我们就 ...

  3. C语言基础:指针类型与指针和数组、字符串的关系

    //指针变量就是用来存储地址的,只能存储地址 格式:  int  *p;  这个p为指针变量:指针变量占8个字节 类型是用来说明这个指针指向的类型: 比如上边的int代表这个指针变量会指向int类型的 ...

  4. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  5. 合并2个dll成一个,好处你懂的

    步骤一:先下载微软的工具 ilmerge.exe 地址:http://www.microsoft.com/en-us/download/details.aspx?id=17630 步骤二:安装好之后 ...

  6. netbeans设置字体

    选择 monospaced 字体 摘抄自:http://blog.sina.com.cn/s/blog_4b6047bc01000boz.html 今天看该文档时,突然意识到通过修改JRE的字体配置文 ...

  7. 备份apt目录节省下载时间

    备份与清理      * 备份快速设置已下载的部分软件包,以便重装系统再次使用,免去重新下载的时间   tar cizvf backup.tar.gz /var/cache/apt/archives ...

  8. P1676陶陶吃苹果 - vijos

    描述 curimit知道陶陶很喜欢吃苹果.于是curimit准备在陶陶生日的时候送给他一棵苹果树. curimit准备了一棵这样的苹果树作为生日礼物:这棵苹果树有n个节点,每个节点上有c[i]个苹果, ...

  9. PHP之session相关实例教程与经典代码

    ·php 中cookie和session的用法比较 ·phpmyadmin报错:Cannot start session without errors问题 ·php中cookie与session应用学 ...

  10. 使用函数的递归调用来解决Hanoi(汉诺)塔问题。

    #include<stdio.h> void hanoi(int n, char x, char y, char z); void move(char x, char y); int ti ...