- 浏览: 25166 次
- 性别:
- 来自: 北京
最新评论
步骤1.新建项目:ssh
步骤2.首先整合Spring 和 Hibernate,将spring下的包和hibernate下的包导入进来,还有mysql的驱动类 步骤3.在类路径下新建beans.xml
步骤4.新建实体bean Employee Employee的映射文件 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="cn.itcast"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
package cn.itcast.bean;
public class Employee {
private String username;
private String password;
private Gender gender = Gender.MAN;
public Employee(){}
public Employee(String username, String password) {
this.username = username;
this.password = password;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.bean"> <class name="Employee"> <id name="username" length="20" /> <property name="password" length="20" not-null="true"/> <property name="gender" not-null="true" length="5"> <type name="org.hibernate.type.EnumType"> <param name="enumClass">cn.itcast.bean.Gender</param> <!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字面值到数据库,如果不指定type参数,保存到数据库的值为枚举的索引值(从0开始) --> <param name="type">12</param> </type> </property> </class> </hibernate-mapping>
Gander: 步骤5.新建数据库操作类 其实现类: 步骤6.编写junit测试 测试完成后,然后 步骤7.将struts2.1.8的包导入进来 步骤8.在类路径下新建struts.xml 步骤9. web.xml 步骤10.新建action 增: 删:
package cn.itcast.bean;
/**
* 性别
*/
public enum Gender {
MAN,WOMEN;
}
package cn.itcast.service;
import java.util.List;
import cn.itcast.bean.Employee;
public interface EmployeeService {
public void save(Employee employee);
public void update(Employee employee);
public Employee find(String username);
public List<Employee> list();
public void delete(String... usernames);
}
package cn.itcast.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;
@Service @Transactional
public class EmployeeServiceBean implements EmployeeService {
@Resource SessionFactory sessionFactory;
//删除
public void delete(String... usernames) {
for(String username :usernames){
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Employee.class, username));
}
}
//查找一个
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Employee find(String username) {
return (Employee) sessionFactory.getCurrentSession().get(Employee.class, username);
}
//查找所有
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public List<Employee> list() {
return sessionFactory.getCurrentSession().createQuery("from Employee").list();
}
//保存
public void save(Employee employee) {
sessionFactory.getCurrentSession().persist(employee);
}
//修改
public void update(Employee employee) {
sessionFactory.getCurrentSession().merge(employee);
}
}
package junit.test;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.Employee;
import cn.itcast.bean.Gender;
import cn.itcast.service.EmployeeService;
public class EmployeeTest {
private static EmployeeService employeeService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext act = new ClassPathXmlApplicationContext(
"beans.xml");
employeeService = (EmployeeService) act
.getBean("employeeServiceBean");
} catch (Exception e) {
System.out.println("错了");
}
}
@Test
public void save(){
employeeService.save(new Employee("lichao","2586"));
}
@Test
public void update(){
Employee em = employeeService.find("liming");
em.setGender(Gender.WOMEN);
employeeService.update(em);
}
@Test
public void find(){
Employee employee = employeeService.find("lichao");
System.out.println(employee.getPassword());
}
@Test
public void list(){
List<Employee> list = employeeService.list();
for(Employee ems : list)
System.out.println("用户名:"+ems.getUsername()+",密码:"+ems.getPassword());
}
@Test
public void delete(){
employeeService.delete("liming","lichao");
}
}
<?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>
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.objectFactory" value="spring" />
<package name="employee" namespace="/employee" extends="struts-default">
<!-- 列表 -->
<action name="list" class="employeeListAction">
<result name="list">/WEB-INF/page/employeelist.jsp</result>
</action>
<!-- 添加 -->
<action name="add_*" class="employeeAddAction" method="{1}">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="token"/>
<result name="addUI">/WEB-INF/page/employeeadd.jsp</result>
<result name="add">/WEB-INF/page/message.jsp</result>
<result name="invalid.token">/WEB-INF/page/employeeadd.jsp</result>
</action>
<!-- 删除 -->
<action name="delete" class="employeeDeleteAction">
<result name="delete" type="redirectAction">list</result>
</action>
<!-- 修改 -->
<action name="update_*" class="employeeUpdateAction" method="{1}">
<result name="select">/WEB-INF/page/employeeupdate.jsp</result>
<result name="update" type="redirectAction">list</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package cn.itcast.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.itcast.bean.Employee;
import cn.itcast.service.EmployeeService;
@Controller @Scope("prototype")
public class EmployeeAddAction {
@Resource EmployeeService employeeService;
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String addUI(){
return "addUI";
}
public String add(){
employeeService.save(employee);
ActionContext.getContext().put("message", "保存成功");
return "add";
}
}
package cn.itcast.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.service.EmployeeService;
@Controller @Scope("prototype")
public class EmployeeDeleteAction {
@Resource EmployeeService employeeservice;
public String execute(){
HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("username");
System.out.println(username);
employeeservice.delete(username);
return "delete";
}
}
改:
package cn.itcast.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import cn.itcast.bean.Employee; import cn.itcast.service.EmployeeService; @Controller @Scope("prototype") public class EmployeeUpdateAction { @Resource EmployeeService employeeService; private Employee employee; public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } //查询出来 public String select(){ HttpServletRequest request = ServletActionContext.getRequest(); String username = request.getParameter("username"); Employee employee = employeeService.find(username); ActionContext.getContext().put("employee", employee); return "select"; } //修改 public String update(){ employeeService.update(employee); return "update"; } }
查:
package cn.itcast.action; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import cn.itcast.service.EmployeeService; @Controller public class EmployeeListAction { @Resource EmployeeService employeeService; public String execute(){ ActionContext.getContext().put("employees", employeeService.list()); return "list"; } }
步骤11.在WEB-INF下新建page文件夹,并新建一下jsp文件
employeeadd.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成员添加</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="add_add" namespace="/employee" method="post"> 姓名:<s:textfield name="employee.username"/><br/> 密码:<s:password name="employee.password"/><br/> 性别:<s:radio list="#{'MAN':'男','WOMEN':'女'}" listKey="key" listValue="value" name="employee.gender" value="{'MAN'}"/><br/> <s:token></s:token> <input type="submit" value="提交"/> </s:form> <a href="<s:url action="list" namespace="/employee"/>">成员列表</a> </body> </html>
message.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成功信息</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <H1><font color="red"><s:property value="#request.message"/></font></H1> </body> </html>
employeelist.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成员列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> OGNL: <br/> <s:iterator value="#request.employees" id="obj"> <s:property value="username"/>,<s:property value="password"/>,<s:property value="gender"/> <a href="/ssh/employee/delete.action?username=<s:property value='username'/>">删除</a> <a href="/ssh/employee/update_select.action?username=<s:property value='username'/>">修改</a> <br/> </s:iterator><br/> JSTL/EL: <br/> <c:forEach items="${employees}" var="employee"> ${employee.username},${employee.password},${employee.gender}<br/> </c:forEach> <br/> <a href="<s:url action="add_addUI" namespace="/employee"/>">添加页面</a> </body> </html>
employeeupdate.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成员修改</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="update_update" namespace="/employee" method="post"> 姓名:<s:textfield value="%{#request.employee.username}" name="employee.username"/><br/> 密码:<s:password value="%{#request.employee.password}" name="employee.password"/><br/> 性别:<s:radio list="#{'MAN':'男','WOMEN':'女'}" listKey="key" listValue="value" name="employee.gender" value="%{#request.employee.gender}"/><br/> <s:token></s:token> <input type="submit" value="提交"/> </s:form> <a href="<s:url action="list" namespace="/employee"/>">成员列表</a> </body> </html>
步骤12.运行测试
首先访问如下路径:http://localhost:8080/ssh/add_addUI.action
- ssh.rar (485.4 KB)
- 下载次数: 10
- spring2.5.6.rar (5.3 MB)
- 下载次数: 6
- hibernate3.3.2.rar (5.1 MB)
- 下载次数: 5
- struts2.rar (3.1 MB)
- 下载次数: 5
- mysql.rar (463.8 KB)
- 下载次数: 3
相关推荐
SSH2整合之structs2.1.8 jar包免费下载
Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合 超级详细的文档,透彻讲解JBPM与SSH的整合过程
最新的spring整合。随着spring从2.5直接升至3.X,打包也不一样了。所以里面的有些包出现了细分和整合。只靠spring提供的架包现在不足以能够整合hibernate和struts 了。
s2sh整合总结(struts2.1.8-spring2.5-hibernate3.2) 三大框架
SSH框架版本 (1)Struts2.1.8 (2)Spring2.0.2 (3)Hibernate3.2.5 开发环境: (1)OS:Windows XP SP3 (2)DB:MySql 5.1.6 (3)JDK:1.6.0_17 (4)Server:Apache Tomcat 6.0.26 (5)IDE:MyEclipse 5.5 注:附详细的整合...
structs2.1.8与hibernate3与spring3整合一起
struts2.1.8+hibernate3.5.5+spring3.0.2+mysql5.1+jdk1.6 使用SSH整合实现对一个bean的增删改查 实现的功能为: 排序,搜索,分页
6.struts2-core-2.1.8.jar(struts2的核心jar包) 7.xwork-core-2.1.6.jar(xwork的核心jar包) 8.servlet.jar 9.commons-lang-2.5.jar(commons项目中的lang包,一般不需要,不用也不出错) 10.struts2-spring-plugin...
SSH项目整合,包含本项目所有的JAR包,基于mysql数据库,完成了基本的“增,删,改,查,完整分页”,另外做了一点js验证,以及建表,插入指定数量的随机测试数据...... 绝对值这个分。
新版SSH+DWR配置(Struts2.1.8+Spring3.0+Hibernate3.3.2+DWR2.0.6) 很好的整合示例 目前最新框架整合
整合成功的struts2.1.6+spring2.5+hibernate3.2框架,其中解决了struts2.1.6的中文编码问题,此项目已成功运行,如有技术交流欢迎联系本人liyujuan0915@126.com
struts2.1.8+spring+hibernate框架整合所需jar包 其中还包括一个mysql数据库驱动包
JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例(已上传) 1、JSON0.34使用的是struts2附带的struts2-json-plugin-2.1.8.1.jar 2、db是mysql,名字为test,用户名root,密码空 3、...
J2EE SSH2架构设计 Struts2.1.8+Spring2.5.6+Hibernate3.3整合,采用AOP事务管理方式
Spring3.0+hibernate3.3+struts2.1.8资源共享
strut2.1.8 hibernate3.5 spring3.0 整合jar包。最新ssh框架整合,已经测试通过,带有msql驱动,log4j,直接可用 付出了不少心血,5分贡献之
Struts2.1.8+Hibernate3.3+Spring3.0整合所需Jar包
最新整理出來的struts2.1.8+spring2.5.6+hibernate3.3.2 框架整合后的配置文件..以及搭建時候的jar包!