最近一直有朋友在问,最新版的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. C# 使用隐式或显示实现接口的区别

    通俗的来讲,"显示接口实现"就是使用接口名称作为方法名的前缀;而传统的实现方式称之为:"隐式接口实现".费话不说,例子如下:      interface IA ...

  2. Python脚本控制的WebDriver 常用操作 <十四> 处理button dropdown 的定位

    测试用例场景 模拟选择下拉菜单中数据的操作 Python脚本 测试用HTML代码: <html> <body> <form> <select name=&qu ...

  3. JDBC ----常用数据库的驱动程序及JDBC URL:

    常用数据库的驱动程序及JDBC URL: Oracle数据库: 驱动程序包名:ojdbc14.jar  驱动类的名字:oracle.jdbc.driver.OracleDriver  JDBC URL ...

  4. 二分查找or折半查找

    package com.gxf.search; /** * 测试折半查找or二分查找 * @author xiangfei * */ public class BiSearch { /** * 非递归 ...

  5. 屏蔽ios7中某个页面的默认手势滑回返回

    - (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...

  6. Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld

    前言(Prologue) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android ...

  7. java集合类(四)About Set

    接上篇:java集合类(三)About Iterator & Vector(Stack) 之前,在比较java常见集合类的时候,就了解到一点有关Set的特性.实现类及其要求等,读者可以去温习下 ...

  8. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  9. oracle 删除用户,表空间;循环删除表

    select * from dba_tablespaces 说明:查看所有表空间 ----------------------------------------------------------- ...

  10. 【HDOJ】【4089】Activation

    概率DP kuangbin总结中的第5题 题解copy: HDU 4098 题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列 ...