spring2.5整合hibernate3.0整合Struts

一、定义一个Bean,person.java

 1 package cn.itcast.bean;
 2
 3 public class Person {
 4     private Integer id;
 5     private String name;
 6
 7     public Person() {}
 8
 9     public Person(String name) {
10         this.name = name;
11     }
12
13     public Integer getId() {
14         return id;
15     }
16     public void setId(Integer id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25 }

二、定义一个接口,PersonService 

 1 package cn.itcast.Service;
 2
 3 import java.util.List;
 4
 5 import cn.itcast.bean.Person;
 6
 7 public interface PersonService {
 8
 9     public abstract void save(Person person);
10
11     public abstract void update(Person person);
12
13     public abstract Person getPerson(Integer personId);
14
15     public abstract void delete(Integer personId);
16
17     public abstract List<Person> getPersons();
18
19 }

三、定义接口的实现类 PersonServiceBean 

 1 package cn.itcast.Service.Impl;
 2
 3 import java.util.List;
 4
 5 import javax.annotation.Resource;
 6
 7 import org.hibernate.Session;
 8 import org.hibernate.SessionFactory;
 9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11
12 import cn.itcast.Service.PersonService;
13 import cn.itcast.bean.Person;
14 @Transactional
15 public class PersonServiceBean implements PersonService {
16     @Resource
17     private SessionFactory sessionFactory;
18
19     @Override
20     public void save(Person person){
21         //取被spring容器管理的session
22         sessionFactory.getCurrentSession().persist(person);
23     }
24
25     @Override
26     public void update(Person person){
27         sessionFactory.getCurrentSession().merge(person);
28     }
29
30     @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
31     @Override
32     public Person getPerson(Integer personId){
33         return (Person) sessionFactory.getCurrentSession().get(Person.class, personId);
34
35     }
36
37     @Override
38     public void delete(Integer personId){
39         sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));
40     }
41
42     @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true)
43     @SuppressWarnings("unchecked")
44     @Override
45     public List<Person> getPersons(){
46         return sessionFactory.getCurrentSession().createQuery("from Person").list();
47
48     }
49 }

四、配置文件beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12
13      <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
14      <context:annotation-config/>
15      <!-- 配置dataSource -->
16      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
17         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
18         <property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
19         <property name="username" value="tsrescue"/>
20         <property name="password" value="123456"/>
21          <!-- 连接池启动时的初始值 -->
22          <property name="initialSize" value="1"/>
23          <!-- 连接池的最大值 -->
24          <property name="maxActive" value="500"/>
25          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
26          <property name="maxIdle" value="2"/>
27          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
28          <property name="minIdle" value="1"/>
29      </bean>
30      <!-- 配置sessionFactory -->
31      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
32          <property name="dataSource" ref="dataSource"/>
33          <property name="mappingResources">
34             <list>
35               <value>cn/itcast/bean/Person.hbm.xml</value>
36             </list>
37          </property>
38          <property name="hibernateProperties">
39              <props>
40                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
41                 <prop key="hibernate.show_sql">true</prop>
42                 <prop key="hibernate.format_sql">true</prop>
43             </props>
44
45          </property>
46     </bean>
47     <!-- spring提供的针对hibernate的事务管理器 -->
48     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
49           <property name="sessionFactory" ref="sessionFactory"/>
50     </bean>
51     <!-- 基于注解的方式声明事务 -->
52     <tx:annotation-driven transaction-manager="txManager"/>
53     <bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/>
54 </beans>

五,定义Person.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping package="cn.itcast.bean">
 6     <class name="Person" table="person">
 7         <id name="id">
 8             <generator class="native" />
 9         </id>
10         <property name="name"/>
11     </class>
12 </hibernate-mapping>

六、定义测试类 PersonServiceBeanTest 

 1 package cn.itcast.Service.Impl;
 2
 3 import static org.junit.Assert.*;
 4
 5 import java.util.List;
 6
 7 import org.junit.BeforeClass;
 8 import org.junit.Test;
 9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11
12 import cn.itcast.Service.PersonService;
13 import cn.itcast.bean.Person;
14
15 public class PersonServiceBeanTest {
16     private static PersonService ps;
17     @BeforeClass
18     public static void setUpBeforeClass() throws Exception {
19         try {
20             ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
21             ps = (PersonService) ctx.getBean("personService");
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }
26
27     @Test
28     public void testSave() {
29         for(int i = 1;i<=10;i++){
30             ps.save(new Person("小明"+i));
31         }
32     }
33
34     @Test
35     public void testUpdate() {
36         Person person = ps.getPerson(1);
37         person.setName("小明修改版");
38         ps.update(person);
39     }
40
41     @Test
42     public void testGetPerson() {
43         Person p = ps.getPerson(1);
44         System.out.println(p.getName());
45     }
46
47     @Test
48     public void testDelete() {
49         ps.delete(3);
50     }
51
52     @Test
53     public void testGetPersons() {
54         List<Person> list = ps.getPersons();
55         for (Person person : list) {
56             System.out.println(person.getName());
57         }
58     }
59
60 }

以上实现了对数据库的操作,下面通过整合Struts来实现将数据传递到页面

一、首先定义一个action,PersonAction 继承Action

 1 package cn.itcast.web.action;
 2
 3 import javax.servlet.ServletRequest;
 4 import javax.servlet.ServletResponse;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7
 8 import org.apache.struts.action.Action;
 9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12 import org.springframework.web.context.WebApplicationContext;
13 import org.springframework.web.context.support.WebApplicationContextUtils;
14
15 import cn.itcast.Service.PersonService;
16
17 public class PersonAction extends Action {
18
19     @Override
20     public ActionForward execute(ActionMapping mapping, ActionForm form,
21             HttpServletRequest request, HttpServletResponse response)
22             throws Exception {
23         WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
24         PersonService personService = (PersonService) ctx.getBean("personService");
25         request.setAttribute("persons", personService.getPersons());
26         return mapping.findForward("list");
27
28     }
29 }

二、定义struts-config.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts-config PUBLIC
 3             "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
 4             "http://struts.apache.org/dtds/struts-config_1_3.dtd">
 5 <struts-config>
 6     <action-mappings>
 7         <action path = "/person/list" type = "cn.itcast.web.action.PersonAction"
 8         validate = "false">
 9             <forward name = "list" path = "/WEB-INF/page/personlist.jsp"/>
10         </action>
11     </action-mappings>
12 </struts-config>

三、定义web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 xmlns="http://java.sun.com/xml/ns/javaee"
 4 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">
 5       <context-param>
 6        <param-name>contextConfigLocation</param-name>
 7        <param-value>classpath:beans.xml</param-value>
 8     </context-param>
 9     <!-- 对Spring容器进行实例化 -->
10     <listener>
11           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12     </listener>
13
14     <servlet>
15         <servlet-name>struts</servlet-name>
16         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
17         <init-param>
18             <param-name>config</param-name>
19             <param-value>/WEB-INF/struts-config.xml</param-value>
20         </init-param>
21         <load-on-startup>0</load-on-startup>
22     </servlet>
23     <servlet-mapping>
24         <servlet-name>struts</servlet-name>
25         <url-pattern>*.do</url-pattern>
26     </servlet-mapping>
27     <welcome-file-list>
28         <welcome-file>index.jsp</welcome-file>
29     </welcome-file-list>
30 </web-app>

四、定义一个jsp页面,用来显示数据。

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>人员列表</title>
 9 </head>
10 <body>
11     <c:forEach items="${persons }" var="person">
12         ${person.id },${person.name }<br>
13     </c:forEach>
14 </body>
15 </html>

好了,至此简单的ssh框架整合完毕!

时间: 2024-11-06 03:31:01

spring2.5整合hibernate3.0整合Struts的相关文章

spring2.5整合hibernate3.0

第一步:配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.spring

struts2+hibernate3.2+spring2.0整合篇(三)

在上篇文章中,我们已经将各层之间相关的类创建完毕,这篇我们主要讲解如何通过spring管理类之间的依赖关系. 步骤八:通过spring管理类之间的依赖关系. applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="

ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤

原创整理不易,转载请注明出处:ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤 代码下载地址:http://www.zuidaima.com/share/1760074977233920.htm 最近闲来无事可做,于是开始学习struts2.Struts2和struts1.webwork2有什么区别我也不说了,网上有很多这方面的资料.以前在项目中从未使用过struts,一直使用spring+hibernate,现在既然学习了Struts,也不能

Struts2+Hibernate3+Spring3 整合

Struts2+Hibernate3+Spring3 整合 一.SSH的下载 首先SSH的下载就已经是一个难点.SSH三个地方同时推出各自的新技术,已经要求利用Maven完成其版本的同步.毕竟spring的版本,决定其能整合什么版本的Struts与hibernate.Spring3.0.5就不能整合Hibernate4.x了.因此本文选取的SSH的版本,分别为struts 2.3.20 GA.Hibernate3.6.10Final.Spring3.0.5,最后有一个log4j,因为Spring

【课程分享】jQuery2.0应用开发:SSH框架整合jQuery2.0实战OA办公自动化(VSS、operamasks-UI框架)

我的qq是2059055336,对这个课程有兴趣的可以加我qq联系. 课程下载地址:链接:http://pan.baidu.com/share/link?shareid=395438909&uk=3611155194 密码:mlvl 课程下载地址:http://www.kuaipan.cn/file/id_192202874577496484.htm?source=1 一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景 jQuery 2.0 正式版发布,不在支持 IE 6/7/8

OA学习笔记-006-SPRING2.5与hibernate3.5整合

一.为什么要整合 1,管理SessionFactory实例(只需要一个) 2,声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二.整合步骤 1.整合sessionFactory 在applicationContext.xml添加 1 <!-- 导入外部的properties文件 --> 2 <context:property-placeholder location="classpath:jdbc.properties"/> 3

Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

[0]README 0)本文旨在 review Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合过程: 1)项目整合所涉及的源代码,please visit  https://github.com/pacosonTang/MyBatis/tree/master/spring4mvc_mybatis3 2)由于晚辈我还不怎么熟悉maven,所以没有用maven依赖各种jar 依赖包来构建项目(编译项目),抱歉:(不过有兴趣的朋友,吧上述的源码down 下来,里面

struts-2.3+spring-4.0+hibernate-4.0整合项目

1.加入spring 1)加入jar包 2)配置web.xml applicationContext.xml (监听器) 3)加入spring的配置文件:aop context tx bean 1.加入hibernate 1.1建立持久化类,和其对应的xxx,hbm.xml文件,生产对应的数据表 1.2spring 整合hibernate 1)加入jar包 2)类路径下加入hibernate.cfg.xml文件.其中配置hibernate的基本配置 3)建立持久化类和对应的xxx.hbm.xml

Apache2.2+Tomcat7.0整合配置详解

一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Linux 等操作系统中运行是最流行的Web服务器软件之一.Apache 反应速度快,运行效率高,但只支持HTML等静态页面(加载插件后也可支持 PHP 页面).Apache Tomcat 是由 Apache 软件基金协会与 Sun 公司联合开发的一款Web服务器,它除了支持HTML等静态页面外,还支持J