- 浏览: 188485 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
Spring4整合Hibernate4和Struts2
1. S2SH整合所需Jar包
2. Spring4整合Hibernate4
3. Spring4整合Struts2
4. S2SH实例测试
1. S2SH整合所需Jar包
导入jdbc jar包 mysql-connector-java-3.1.12-bin.jar 导入struts2 jar包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar asm-commons-3.3.jar commons-beanutils-1.8.0.jar commons-collections-3.1.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang-2.4.jar commons-lang3-3.1.jar commons-logging-1.1.3.jar ezmorph-1.0.6.jar freemarker-2.3.19.jar ognl-3.0.6.jar struts2-core-2.3.16.3.jar struts2-spring-plugin-2.3.16.3.jar xwork-core-2.3.16.3.jar 导入hibernate4 jar包 dom4j-1.6.1.jar ehcache-core-2.4.3.jar hibernate-c3p0-4.3.5.Final.jar hibernate-commons-annotations-4.0.4.Final.jar hibernate-core-4.3.5.Final.jar hibernate-ehcache-4.3.5.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar mchange-commons-java-0.2.3.4.jar slf4j-api-1.6.1.jar 导入spring4 jar包 aspectjweaver.jar spring-aop-4.0.6.RELEASE.jar spring-aspects-4.0.6.RELEASE.jar spring-beans-4.0.6.RELEASE.jar spring-context-4.0.6.RELEASE.jar spring-context-support-4.0.6.RELEASE.jar spring-core-4.0.6.RELEASE.jar spring-expression-4.0.6.RELEASE.jar spring-jdbc-4.0.6.RELEASE.jar spring-orm-4.0.6.RELEASE.jar spring-tx-4.0.6.RELEASE.jar spring-web-4.0.6.RELEASE.jar spring-webmvc-4.0.6.RELEASE.jar
2. Spring4整合Hibernate4
Spring4接管Hibernate4所有Bean实例,以及SessionFactory,事务管理器; 泛型注入;
3. Spring4整合Struts2
Spring4接管Struts2所有Bean实例
4. S2SH实例测试
创建t_user表 create table t_user( id int(11) primary key auto_increment, userName varchar(20), password varchar(20) ) insert into t_user values(1, 'test', '1234');
UserAction.java package com.andrew.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.springframework.stereotype.Controller; import com.andrew.entity.User; import com.andrew.service.UserService; import com.opensymphony.xwork2.ActionSupport; @Controller public class UserAction extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; @Resource private UserService userService; private User user; private String error; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getError() { return error; } public void setError(String error) { this.error = error; } public String login() throws Exception { HttpSession session = request.getSession(); User currentUser = userService.findUserByNameAndPassword(user); if (currentUser != null) { session.setAttribute("currentUser", currentUser); return SUCCESS; } else { error = "用后名或者密码错误!"; return ERROR; } } public void setServletRequest(HttpServletRequest request) { this.request = request; } }
BaseDao.java package com.andrew.dao; import java.io.Serializable; import java.util.List; public interface BaseDao<T> { public Serializable save(T o); public void delete(T o); public void update(T o); public void saveOrUpdate(T o); public List<T> find(String hql); public List<T> find(String hql, Object[] param); public List<T> find(String hql, List<Object> param); public List<T> find(String hql, Object[] param, Integer page, Integer rows); public List<T> find(String hql, List<Object> param, Integer page, Integer rows); public T get(Class<T> c, Serializable id); public T get(String hql, Object[] param); public T get(String hql, List<Object> param); public Long count(String hql); public Long count(String hql, Object[] param); public Long count(String hql, List<Object> param); public Integer executeHql(String hql); public Integer executeHql(String hql, Object[] param); public Integer executeHql(String hql, List<Object> param); }
BaseDaoImpl.java package com.andrew.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.andrew.dao.BaseDao; @Repository("baseDao") @SuppressWarnings("all") public class BaseDaoImpl<T> implements BaseDao<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getCurrentSession() { return sessionFactory.getCurrentSession(); } public Serializable save(T o) { return this.getCurrentSession().save(o); } public void delete(T o) { this.getCurrentSession().delete(o); } public void update(T o) { this.getCurrentSession().update(o); } public void saveOrUpdate(T o) { this.getCurrentSession().saveOrUpdate(o); } public List<T> find(String hql) { return this.getCurrentSession().createQuery(hql).list(); } public List<T> find(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.list(); } public List<T> find(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.list(); } public List<T> find(String hql, Object[] param, Integer page, Integer rows) { if (page == null || page < 1) { page = 1; } if (rows == null || rows < 1) { rows = 10; } Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } public List<T> find(String hql, List<Object> param, Integer page, Integer rows) { if (page == null || page < 1) { page = 1; } if (rows == null || rows < 1) { rows = 10; } Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); } public T get(Class<T> c, Serializable id) { return (T) this.getCurrentSession().get(c, id); } public T get(String hql, Object[] param) { List<T> l = this.find(hql, param); if (l != null && l.size() > 0) { return l.get(0); } else { return null; } } public T get(String hql, List<Object> param) { List<T> l = this.find(hql, param); if (l != null && l.size() > 0) { return l.get(0); } else { return null; } } public Long count(String hql) { return (Long) this.getCurrentSession().createQuery(hql).uniqueResult(); } public Long count(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return (Long) q.uniqueResult(); } public Long count(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return (Long) q.uniqueResult(); } public Integer executeHql(String hql) { return this.getCurrentSession().createQuery(hql).executeUpdate(); } public Integer executeHql(String hql, Object[] param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { q.setParameter(i, param[i]); } } return q.executeUpdate(); } public Integer executeHql(String hql, List<Object> param) { Query q = this.getCurrentSession().createQuery(hql); if (param != null && param.size() > 0) { for (int i = 0; i < param.size(); i++) { q.setParameter(i, param.get(i)); } } return q.executeUpdate(); } }
User.java package com.andrew.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "t_user") public class User { private Integer id; private String userName; private String password; @Id @GenericGenerator(name = "generator", strategy = "native") @GeneratedValue(generator = "generator") @Column(name = "id", length = 11) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "userName", length = 20) public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name = "password", length = 20) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
UserService.java package com.andrew.service; import java.util.List; import com.andrew.entity.User; public interface UserService { public void saveUser(User user); public void updateUser(User user); public User findUserById(int id); public void deleteUser(User user); public List<User> findAllList(); public User findUserByNameAndPassword(User user); }
UserServiceImpl.java package com.andrew.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.andrew.dao.BaseDao; import com.andrew.entity.User; import com.andrew.service.UserService; @Service("userService") public class UserServiceImpl implements UserService { @Resource private BaseDao<User> baseDao; public void saveUser(User user) { baseDao.save(user); } public void updateUser(User user) { baseDao.update(user); } public User findUserById(int id) { return baseDao.get(User.class, id); } public void deleteUser(User user) { baseDao.delete(user); } public List<User> findAllList() { return baseDao.find("from User"); } public User findUserByNameAndPassword(User user) { return baseDao.get("from User u where u.userName=? and u.password=?", new Object[] { user.getUserName(), user.getPassword() }); } }
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 定义数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring4"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- session工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <!-- 自动扫描注解方式配置的hibernate类文件 --> <property name="packagesToScan"> <list> <value>com.andrew.entity</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务通知属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 定义事务传播属性 --> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="new*" propagation="REQUIRED" /> <tx:method name="set*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="change*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置事务切面 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.andrew.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <!-- 自动加载构建bean --> <context:component-scan base-package="com.andrew" /> </beans>
hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 显示sql语句 --> <property name="show_sql">true</property> <!-- 自动更新 --> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action" /> <package name="s2sh" namespace="/user" extends="struts-default"> <action name="user_*" method="{1}" class="com.andrew.action.UserAction"> <result name="success">/success.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>S2SH</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 添加对spring的支持 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 定义Spring监听器,加载Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 添加对struts2的支持 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Session延迟加载到页面 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath }/user/user_login.action" method="post"> userName:<input type="text" name="user.userName" value="${user.userName }"/><br/> password:<input type="password" name="user.password" value="${user.password }"><br/> <input type="submit" value="login"/><font color="red">${error }</font> </form> </body> </html>
success.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 欢迎:${currentUser.userName } </body> </html>
http://localhost:8080/S2SH/ 1234 1234 submit 运行结果: 用后名或者密码错误! 控制台: Hibernate: select user0_.id as id1_0_, user0_.password as password2_0_, user0_.userName as userName3_0_ from t_user user0_ where user0_.userName=? and user0_.password=? http://localhost:8080/S2SH/ test 1234 submit 运行结果: http://localhost:8080/S2SH/user/user_login.action 欢迎:test 控制台: Hibernate: select user0_.id as id1_0_, user0_.password as password2_0_, user0_.userName as userName3_0_ from t_user user0_ where user0_.userName=? and user0_.password=?
相关推荐
S2SH框架是Java Web开发中的一个经典组合,由Struts2、Spring和Hibernate三个开源框架组成。这个基础框架旨在为初级Java开发人员提供一个快速开发的平台,同时适用于小型到中型项目的应用。下面我们将深入探讨这三个...
总结,S2SH框架结合了Struts2的控制层、Spring的服务层管理和Hibernate的数据访问层,为开发者提供了一个强大而全面的工具集。在实际项目中,这个基础框架能够帮助我们构建稳定、高效的Web应用。通过深入理解和实践...
【S2SH框架源码详解】 S2SH框架,全称为Struts2+Spring+...通过对S2SH框架源码的深入理解和学习,开发者可以更好地掌握企业级Java Web开发的核心技术,提升开发效率,并为构建稳定、高效的应用系统打下坚实的基础。
在IT领域,构建一个新闻发布系统是常见的需求,而使用s2sh框架(Struts2 + Spring + Hibernate)则是实现这一目标的常见技术选择。S2SH框架结合了Struts2的MVC(Model-View-Controller)设计模式、Spring的依赖注入...
**S2SH框架配置详解** S2SH框架是基于Java的Web开发框架,由Struts、Spring和Hibernate三个开源框架组合而成。这个框架为开发者提供了一种高效且灵活的开发方式,尤其对于初学者,理解并掌握S2SH的配置至关重要。...
这些库文件是运行s2sh框架应用程序的基础,因为它们包含了执行特定功能的类和方法。 总结来说,"s2sh框架整合类包"是一个为了简化基于Spring、Struts和Hibernate的Web应用程序开发而提供的工具包。它包含了所有必需...
**S2SH框架整合源代码详解** S2SH框架,全称为Struts2 + Spring + Hibernate,是Java Web开发中的经典组合,常用于构建企业级应用。这个框架整合旨在为初学者提供一个易于理解的增删改查(CRUD)功能实现,帮助他们...
S2SH项目框架是Web应用开发中常用的集成框架,它结合了Struts2、Spring3和Hibernate3这三大开源框架,以提供一个高效且灵活的开发环境。这个框架的组合旨在简化应用程序的架构,提高代码的可维护性和可扩展性。 ...
S2SH框架整合是指Struts2、Spring和Hibernate三个开源框架的集成,它们分别是Web层、控制层和持久层的主流解决方案,共同构建了一个强大的企业级应用开发环境。这三个框架的结合使得开发过程更加高效,代码结构更...
S2SH框架是Java Web开发中的一个经典组合,由Struts、Spring和Hibernate三个开源框架组成。这个框架组合为开发者提供了一个全面的解决方案,用于构建可扩展、模块化且易于维护的Web应用程序。下面将详细介绍S2SH框架...
总之,“最全最新S2SH框架jar包”是一个集成了Struts2、Spring和Hibernate最新版本的开发包,包含Spring注解支持,旨在帮助开发者快速构建稳定、高效的Java Web应用。在使用时,开发者应熟悉每个框架的核心概念和...
#### 一、S2SH框架概述 S2SH框架是Struts2、Spring和Hibernate三个开源框架的整合。这三个框架分别负责Web层、业务逻辑层和服务持久化层,通过整合可以实现MVC模式的应用程序开发。 - **Struts2**:提供了一个强大...
S2SH框架是Java Web开发中的一个经典组合,由Struts 2、Spring和Hibernate三大框架组成,提供了模型-视图-控制器(MVC)架构的支持,实现了业务逻辑、数据访问和用户界面的解耦。本教程将详细介绍如何在MyEclipse ...
在项目中,S2SH框架与Oracle 10g建立连接,通过JDBC驱动进行数据交互。 5. **JSTL(JavaServer Pages Standard Tag Library)**:JSTL是一组标准标签库,用于简化JSP页面中的Java代码,提高可维护性和可读性。在这...
S2SH框架,即Struts2、Hibernate3和Spring3的组合,是Java EE领域中广泛采用的一种集成方案。这个框架组合提供了模型-视图-控制器(MVC)的架构,数据持久化以及依赖注入等功能,极大地提升了开发效率和代码质量。 ...
【标题】"s2sh框架+cxf接口"指的是一个整合了Struts2、Spring和Hibernate三个框架,并且集成了CXF服务接口的项目压缩包。这个压缩文件包含了进行相关开发所需的依赖库,对于想要在Java Web环境中使用这些技术栈的人...
在IT行业中,S2SH框架是指Struts2、Spring和Hibernate三个开源框架的组合,它在Web应用开发中被广泛使用。本项目标题"sysUser S2SH 框架+分页+ajax用户时实验证"揭示了几个关键的技术点,我们将详细探讨这些知识点。...
本文将深入探讨一个使用S2SH框架开发的BBS论坛管理系统,分析其设计原理、核心技术和实现细节。 S2SH框架,全称为Struts2 + Spring + Hibernate的组合,是Java Web开发领域中的一种经典MVC(Model-View-Controller...
在IT行业中,SSH(Struts2、Hibernate、Spring)是一个常见的企业级应用开发框架组合,也被称作S2SH框架。这个框架集合了MVC(Model-View-Controller)架构模式、持久化管理以及依赖注入等功能,使得开发更加高效且...