- 浏览: 126086 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
本节主要通过一个小例子来讲解spring,struts,hibernate的结合:
例:增删改查用户:
操作列表:保存用户或显示所有用户,如下图
新增用户界面
显示所有用户列表
修改用户:
以下是代码实现,此工程所需的JAR包及配置与前几节一样
User对应的bean,user有id,age,firstname,lastname四个属性:
package com.test.bean; public class User { private int id; private int age; private String firstname; private String lastname; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } }
User对应的hibernate配置文件
User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.bean.User" table="users"> <id name="id" column="id" type="int"> <generator class="increment"></generator> </id> <property name="firstname" column="firstname" type="java.lang.String"></property> <property name="lastname" column="lastname" type="java.lang.String"></property> <property name="age" column="age" type="int"></property> </class> </hibernate-mapping>
其对应的dao接口
UserDAO.java
package com.test.dao; import java.util.List; import com.test.bean.User; public interface UserDAO { public void saveUser(User user); public List<User> findAllUsers(); public void removeUser(User user); public void updateUser(User user); public User findUserById(Integer id); }
实现类UserDAOImpl.java
package com.test.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.test.bean.User; import com.test.dao.UserDAO; public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{ public void saveUser(User user) { this.getHibernateTemplate().save(user); } public List<User> findAllUsers() { String hql = " from User user order by user.id desc"; return (List<User>)this.getHibernateTemplate().find(hql); } public void removeUser(User user) { this.getHibernateTemplate().delete(user); } public User findUserById(Integer id) { User user = (User) this.getHibernateTemplate().get(User.class, id); return user; } public void updateUser(User user) { this.getHibernateTemplate().update(user); } }
UserService.java接口
package com.test.service; import java.util.List; import com.test.bean.User; public interface UserService { public void save(User user); public List<User> findAll(); public void delete(User user); public void update(User user); public User findById(Integer id); }
实现类,通过调用 DAO层代码,分别实现用户的新增,删除,更新,查找操作
package com.test.service.impl; import java.util.List; import com.test.bean.User; import com.test.dao.UserDAO; import com.test.service.UserService; public class UserServiceImpl implements UserService { private UserDAO userDao; public UserDAO getUserDao() { return userDao; } public void setUserDao(UserDAO userDao) { this.userDao = userDao; } public void save(User user) { this.userDao.saveUser(user); } public List<User> findAll() { return this.userDao.findAllUsers(); } public void delete(User user) { this.userDao.removeUser(user); } public void update(User user) { this.userDao.updateUser(user); } public User findById(Integer id) { return this.userDao.findUserById(id); } }
以下是Action类,一共五个:action类中不处理业务逻辑,业务逻辑都放在service类中处理,action主要处理转发。其中UpdatePUserAction用于查找需要被修改的数据。并将其值返回到update.jsp页面
ListUserAction.java ,查找所有用户,其值返回到list.jsp
package com.test.action.user; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.test.service.UserService; public class ListUserAction extends ActionSupport { private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute() throws Exception { Map request = (Map) ActionContext.getContext().get("request"); request.put("list", this.userService.findAll()); return "success"; } }
删除用户
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport; import com.test.bean.User; import com.test.service.UserService; public class RemoveUserAction extends ActionSupport { private User user; private UserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute() throws Exception { this.userService.delete(user); return "success"; } }
保存
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport; import com.test.bean.User; import com.test.service.UserService; public class SaveUserAction extends ActionSupport { private User user; private UserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute() throws Exception { // 调用 service相关方法,完成实际的业务处理 this.getUserService().save(user); return "success"; } }
根据传入的ID值返回需要被更新的user信息
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport; import com.test.bean.User; import com.test.service.UserService; public class UpdatePUserAction extends ActionSupport { private User user; private UserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute() throws Exception { this.user = userService.findById(user.getId()); return "success"; } }
更新
package com.test.action.user; import com.opensymphony.xwork2.ActionSupport; import com.test.bean.User; import com.test.service.UserService; public class UpdateUserAction extends ActionSupport { private User user; private UserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute() throws Exception { this.userService.update(user); return super.execute(); } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="ssh2" extends="struts-default"> <!--下面这个class的值对应的是spring配置文件中的saveUserAction bean--> <action name="saveUser" class="saveUserAction"> <result name="success" type="redirect">listUser.action</result> </action> <action name="listUser" class="listUserAction"> <result name="success">/list.jsp</result> </action> <action name="deleteUser" class="removeUserAction"> <result name="success" type="redirect">listUser.action</result> </action> <action name="updatePUser" class="updatePUserAction"> <result name="success">update.jsp</result> </action> <action name="updateUser" class="updateUserAction"> <result name="success" type="redirect">listUser.action</result> </action> </package> </struts>
spring配置文件,所有的bean之前的依赖关系都写在里面
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 配置连接池数据源,destroy-method="close"表示退出时,将所有连接释放 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/hibernate</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> </bean> <!-- session 工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/test/bean/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> <!-- spring事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="userServiceTarget" class="com.test.service.impl.UserServiceImpl"> <property name="userDao"> <ref local="userDao"/> </property> </bean> <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="target"> <ref local="userServiceTarget"/> </property> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype"> <property name="userService"> <ref local="userService"/> </property> </bean> <bean id="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype"> <property name="userService"> <ref local="userService"/> </property> </bean> <bean id="removeUserAction" class="com.test.action.user.RemoveUserAction" scope="prototype"> <property name="userService"> <ref local="userService"/> </property> </bean> <bean id="updatePUserAction" class="com.test.action.user.UpdatePUserAction" scope="prototype"> <property name="userService"> <ref local="userService"/> </property> </bean> <bean id="updateUserAction" class="com.test.action.user.UpdateUserAction" scope="prototype"> <property name="userService"> <ref local="userService"/> </property> </bean> </beans>
hibernate配置文件,基本无配置
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> </session-factory> </hibernate-configuration>
以下是JSP页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <h1><font color="red">operation list</font></h1> <s:a href ="save.jsp">save user</s:a><br><br><br> <s:a href="listUser.action">List Users</s:a> </body> </html>
显示所有用户界面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <SCRIPT type="text/javascript"> function del(){ if(confirm("are you sure?")){ return true; } return false; } </SCRIPT> </head> <body> <h1><font color="red">list user</font></h1> <table border="1" width="80% align="center"> <tr> <td> 序号 </td> <td> 姓 </td> <td> 名 </td> <td> 年龄 </td> <td> 删除 </td> <td> 更新 </td> </tr> <s:iterator value="#request.list" id="us"> <tr> <td> <s:property value="#us.id"/> </td> <td> <s:property value="#us.firstname"/> </td> <td> <s:property value="#us.lastname"/> </td> <td> <s:property value="#us.age"/> </td> <td> <!-- 通过updatePUser.action从数据库中取出此ID对应的数据,并传到update.jsp上 --> <s:a href="updatePUser.action?user.id=%{#us.id}">update</s:a> </td> <td> <s:a href="deleteUser.action?user.id=%{#us.id}" onclick="return del();"> delete</s:a> </td> </tr> </s:iterator> </table> </body> </html>
保存页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <h1><font color="red">save user</font></h1> <s:form action="saveUser"> <s:textfield name="user.firstname" label="first name"></s:textfield> <s:textfield name="user.lastname" label="last name"></s:textfield> <s:textfield name="user.age" label="age"></s:textfield> <s:submit value="submit"></s:submit> </s:form> </body> </html>
更新页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <h1><font color="red">update user</font></h1> <s:form action="updateUser"> <table> <tr> <td> <s:hidden name="user.id" value="%{user.id}"></s:hidden> </td> </tr> <tr> <td> <s:textfield name="user.firstname" value="%{user.firstname}" label="firstname"></s:textfield> </td> </tr> <tr> <td> <s:textfield name="user.lastname" value="%{user.lastname}" label="lastname"></s:textfield> </td> </tr> <tr> <td> <s:textfield name="user.age" value="%{user.age}" label="age"></s:textfield> </td> </tr> <tr> <td> <s:submit value="submit"></s:submit> </td> </tr> </table> </s:form> </body> </html>
相关推荐
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
收集的Java Web整合开发实战:基于Struts 2+Hibernate+Spring-源代码,看到其他人下载币要的太多,给大家分享一下。 不是很全,但可以拿来参考了。 由于大小限制,还有另外一个包······
Java Web整合开发实战--基于Struts 2+Hibernate+Spring.pdf 1章 Web的工作机制 2章 搭建Java Web开发环境 3章 JSP及其相关技术 2篇 表现层框架Struts技术 4章 Struts快速上手 5章 解密Struts之核心文件 6章 ...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自解决应用程序的不同问题,而将这三者整合在一起可以构建高效、灵活的企业级应用。Struts2作为MVC(Model-View-Controller)框架,负责处理用户请求...
SSH框架,即Struts2、Hibernate和Spring的组合,是Java Web开发中广泛使用的三大开源框架。它们各自负责Web应用程序的不同层面,通过整合可以提供一套完整的解决方案,实现MVC(Model-View-Controller)设计模式,...
Struts2、Spring4和Hibernate4是Java Web开发中的三大主流框架,它们分别负责MVC模式中的表现层、业务层和服务层。这个最新的项目系统整合了这三个框架,旨在提供一个高效、灵活且易于维护的开发环境。下面将详细...
Struts 2、Hibernate 和 Spring 是 Java Web 开发中的三个重要框架,它们组合起来可以构建高效、可维护的Web应用程序,尤其是对于复杂的企业级论坛系统。这个基于Struts 2+Hibernate+Spring实现的论坛系统,充分利用...
Struts2、Hibernate和Spring是Java企业级应用中常用的三个开源框架,它们分别负责MVC模式中的表现层、持久层和业务层管理。SSH(Struts2+Spring+Hibernate)框架整合是Java Web开发中常见的一种技术栈,能有效地提高...
在现代Web应用开发中,SSH(Struts2、Spring、Hibernate)框架因其各自独特的功能和优势,常被组合使用以构建高效、稳定且易于维护的系统。以下是针对SSH框架整合的具体实施步骤和思路。 #### 二、整合步骤详解 ##...
在IT行业中,SSH(Spring、Struts2、Hibernate)是一个经典的Java Web开发框架组合,而Redis则是一个高性能的键值存储系统,常用于缓存和数据持久化。将SSH与Redis整合,可以提升应用程序的性能和响应速度。下面将...
Struts2、Hibernate和Spring是Java企业级应用中三大核心框架,它们的整合使用能够构建出高效、可维护性高的Web应用程序。本篇将深入剖析这三者如何协同工作,并提供实际范例应用。 首先,Struts2作为MVC(模型-视图...
Java论坛系统源码是基于三大主流Java开源框架——Struts 2、Hibernate和Spring构建的。这个系统的设计和实现展示了如何有效地整合这三个框架,以构建一个功能完善的Web应用程序。下面将详细阐述这些技术及其在论坛...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自负责不同的职责:Struts2作为MVC框架处理请求和展示,Spring提供依赖注入和事务管理,Hibernate则作为ORM框架处理数据库操作。将这三个框架整合在...
在IT行业中,SSH...通过这些配置,SSH能够协同工作,Spring负责整体控制和事务管理,Struts2处理用户交互,Hibernate处理数据库操作。这种整合提供了强大的功能,使开发者能更专注于业务逻辑,而非底层技术细节。
Struts2 Spring Hibernate IBatis Struts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatis 只需要导入相应的jar包就行了 ,数据库是mysql :数据库名叫做mydatabase,表名...
这个项目整合了三个关键的开源框架:Struts2、Hibernate和Spring,它们在Java Web开发中扮演着重要角色。 【Struts2】作为MVC(模型-视图-控制器)架构的框架,负责处理HTTP请求,控制应用程序流程,并将数据传递给...
Struts2、Hibernate3和Spring是Java开发中常用的三大框架,它们各自负责不同的职责:Struts2用于控制应用程序的流程,Hibernate3则是优秀的对象关系映射(ORM)框架,Spring则提供全面的依赖注入(DI)和面向切面...
Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自在应用程序的不同层面发挥着重要作用。Struts2是用于构建MVC(Model-View-Controller)架构的框架,负责控制应用程序的流程;Hibernate是一个对象...
Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自负责应用程序的不同层面:Struts2处理MVC(Model-View-Controller)架构中的控制层,Hibernate专注于数据持久化,而Spring则提供了全面的依赖注入和...