(转)最新版的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;
}
}
(转)最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1)的更多相关文章
- 【转】SSH中 整合spring和proxool 连接池
[摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...
- ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例
一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...
- 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 ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- Maven环境下搭建SSH框架之Spring整合Hibernate
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...
- Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)
搭建的环境:eclispe下面的maven web项目 Struts: 2.5.10 Spring: 4.3.8 Hibernate: 5.1.7 .Final MySQL: 5. ...
- Struts2,Spring, Hibernate三大框架SSH的整合步骤
整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...
- SSH框架之Spring+Struts2+Hibernate整合篇
回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...
随机推荐
- thinkpad t440p 解决无线网卡驱动
$ wget https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1239578/+attachment/4057550/+files/rtl_9 ...
- SQL随机数的生成
下面是一个随机函数问题,获取两位数的随机数,且不重复. 但是说明一下,这个函数有点bug,例如:两位数的函数最后能生成89个,如果将数量改成90,那么就无法生成,陷入死循环了. IF object_i ...
- SSHDroid(SSH Server for Android)通过PC或命令连接android
1.下载berserker.android.apps.sshdroid.apk .(如果你懒的下载,给我留言,我会发给你) 2.安装到手机,显示如图: 简单解释一下:一般android系统没有root ...
- Ubuntu下codeblocks汉化
code::blocks是一个十分好用编辑环境,一个在手,无所不能,为了更好的支持中文,我列出了汉化的方法: 1下载中文汉化包:http://pan.baidu.com/s/1hqvNZbI 2.解压 ...
- 【C#】线程池
将方法排入队列以便执行,并指定包含该方法所用数据的对象.此方法在有线程池线程变得可用时执行. class Program { static void Main(string[] args) { str ...
- PIGCMS提示“你的程序为盗版,非法授权,请联系QQ7530782或者8441010”的修复方法
最近群里又有人发出来微信平台盗版源码这个问题求解决,其实我本人是一直支持正版的,大家有条件的还是购买正好为好,既然有人问我就顺便解决了下,其实很简单,再换个接口就好了,查看了一下是在\PigCms\L ...
- 常用的机器学习&数据挖掘知识点【转】
转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...
- C#如何判断两个数组相等
/// <summary> /// 数组比较是否相等 /// </summary> /// <param name="bt1">数组1</ ...
- 关于MDK中:RO-data、RW-data、ZI-data
最近在LPC2109上调试ENC28J60,协议栈使用的是UIP,刚开始用的telnet服务,能够正常编译运行.然后换成webserver提示: enc28j60.axf: Error: L6406E ...
- linux下MySQL 5.6源码安装
linux下MySQL 5.6源码安装 1.下载:当前mysql版本到了5.6.20 http://dev.mysql.com/downloads/mysql 选择Source Code 2.必要软件 ...