spring2.5整合hibernate3.0整合Struts
首先:这是spring framework+hibernate+struts集成,spring主要用于aop和ioc,hibernate主要是用于持久层,struts主要是用于mvc。
同时关于springMVC和spring,这两个不是一个概念, springMVC从名字来看,就知道这是一个mvc框架,它和struts是一个层次的,springMVC有自己的类hibernate的存储持久化模块,也支持集成hibernate来完成存储。
项目结构图:
所需jar包:
一、定义一个Bean,person.java
package cn.itcast.bean; public class Person {
private Integer id;
private String name; public Person() {} public Person(String name) {
this.name = name;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
二、定义一个接口,PersonService
package cn.itcast.Service; import java.util.List; import cn.itcast.bean.Person; public interface PersonService { public abstract void save(Person person); public abstract void update(Person person); public abstract Person getPerson(Integer personId); public abstract void delete(Integer personId); public abstract List<Person> getPersons(); }
三、定义接口的实现类 PersonServiceBean
package cn.itcast.Service.Impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import cn.itcast.Service.PersonService;
import cn.itcast.bean.Person;
@Transactional
public class PersonServiceBean implements PersonService {
@Resource
private SessionFactory sessionFactory; @Override
public void save(Person person){
//取被spring容器管理的session
sessionFactory.getCurrentSession().persist(person);
} @Override
public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
} @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
@Override
public Person getPerson(Integer personId){
return (Person) sessionFactory.getCurrentSession().get(Person.class, personId); } @Override
public void delete(Integer personId){
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));
} @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true)
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").list(); }
}
四、配置文件beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<context:annotation-config/>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
<property name="username" value="tsrescue"/>
<property name="password" value="123456"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props> </property>
</bean>
<!-- spring提供的针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 基于注解的方式声明事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/>
</beans>
五,定义Person.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">
<!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
六、定义测试类 PersonServiceBeanTest
package cn.itcast.Service.Impl; import static org.junit.Assert.*; 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.Service.PersonService;
import cn.itcast.bean.Person; public class PersonServiceBeanTest {
private static PersonService ps;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ps = (PersonService) ctx.getBean("personService");
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testSave() {
for(int i = 1;i<=10;i++){
ps.save(new Person("小明"+i));
}
} @Test
public void testUpdate() {
Person person = ps.getPerson(1);
person.setName("小明修改版");
ps.update(person);
} @Test
public void testGetPerson() {
Person p = ps.getPerson(1);
System.out.println(p.getName());
} @Test
public void testDelete() {
ps.delete(3);
} @Test
public void testGetPersons() {
List<Person> list = ps.getPersons();
for (Person person : list) {
System.out.println(person.getName());
}
} }
以上实现了对数据库的操作,下面通过整合Struts来实现将数据传递到页面
一、首先定义一个action,PersonAction 继承Action
package cn.itcast.web.action; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import cn.itcast.Service.PersonService; public class PersonAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
PersonService personService = (PersonService) ctx.getBean("personService");
request.setAttribute("persons", personService.getPersons());
return mapping.findForward("list"); }
}
二、定义struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action path = "/person/list" type = "cn.itcast.web.action.PersonAction"
validate = "false">
<forward name = "list" path = "/WEB-INF/page/personlist.jsp"/>
</action>
</action-mappings>
</struts-config>
三、定义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_3_0.xsd" id="WebApp_ID" version="3.0">
<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> <servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
四、定义一个jsp页面,用来显示数据。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>人员列表</title>
</head>
<body>
<c:forEach items="${persons }" var="person">
${person.id },${person.name }<br>
</c:forEach>
</body>
</html>
好了,至此简单的ssh框架整合完毕!
当然,上面的方案存在小小的缺陷,action每次取的spring容器实例都要采用这种方式:
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext()); PersonService personService = (PersonService) ctx.getBean("personService");
所以我们不如采用spring依赖注入的方式,将这个对象注入进来,
首先在beans.xml中注入PersonAction,
注意:1、确保struts-config.xml中action的path属性和bean名称相同。
<bean name = "/person/list" class="cn.itcast.web.action.PersonAction"/>
2、在struts-config.xml中定义一个控制器如下:
<controller>
<set-property value="processorClass" value = "org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
3、在PersonAction.java中注入PersonService即可!
package cn.itcast.web.action; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; import cn.itcast.Service.PersonService; public class PersonAction extends Action {
@Resource
private PersonService personService; @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
// PersonService personService = (PersonService) ctx.getBean("personService"); request.setAttribute("persons", personService.getPersons());
return mapping.findForward("list"); }
}
spring2.5整合hibernate3.0整合Struts的更多相关文章
- spring2.5整合hibernate3.0
第一步:配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- Struts2.1.8 + Spring3.0+ Hibernate3.2整合笔记
body, p, th, td, li, ul, ol, h1, h2, h3, h4, h5, h6, pre { font-family: simsun; line-height: 1.4; } ...
- Struts2整合Hibernate3实现用户登录功能
所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...
- Spring3 整合Hibernate3.5 动态切换SessionFactory (切换数据库方言)
一.缘由 上一篇文章Spring3.3 整合 Hibernate3.MyBatis3.2 配置多数据源/动态切换数据源 方法介绍到了怎么样在Sping.MyBatis.Hibernate整合的应用中动 ...
- Apache2.2+Tomcat7.0整合配置详解
一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...
- OA学习笔记-006-SPRING2.5与hibernate3.5整合
一.为什么要整合 1,管理SessionFactory实例(只需要一个) 2,声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二.整合步骤 1.整合sessionFac ...
- Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)
依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递
- 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action
SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib 开发基本包 Struts2有一 ...
- 帝国cms7.0整合百度编辑器ueditor教程
帝国cms7.0整合百度编辑器ueditor教程开始 1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/do ...
随机推荐
- window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马
网站安全中,对目录的执行权限是非常敏感的,一般来说,可以写入的目录是不能够拥有脚本的执行权限的,可写入的目录如: data.uploads,data目录主要是基本配置文件和缓存数据,uploads则是 ...
- codeforces 192 D
link: http://codeforces.com/contest/330/problem/D The discription looks so long, but the problem is ...
- leetcode 92 Reverse Linked List II ----- java
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- FZU 1343 WERTYU --- 水题
FZU 1343 题目大意:手放在键盘上时,稍不注意就会往右错一位.这样Q就会输入成W,输入J就会变成K 给定一串大写敲错后输入,输出正确的输入(输入保证合法,如输入中不会出现Q,A,Z): 解题思路 ...
- PHP 页面自动刷新可借助JS来实现,简单示例如下:
<?php echo "系统当前时间戳为:"; echo ""; echo time(); //<!--JS 页面自动刷新 --> echo ...
- oh-my-zsh的使用
一.自动安装wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh 二.配置文件~/. ...
- 如何安装 VLFeat工具包到Linux (Ubuntu) 64_bit system matlab 中?
最近要提一个数据集的feature,想先用HOG特征做一个baseline,听师兄说VLFeat 是一个不错的工具包,就下载了试试,刚刚配置成功,网上各种搜索教程啊 但是都不行,最后还是硬着头皮看官网 ...
- Checking the content of the pointer
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/d481b7ec-cd67-476c-b2d7-ca3334effa16/h ...
- Linux文件普通权限
1.文件所有者,所属用户组,其他用户1)文件所有者:创建文件的用户2)所属用户组:文件属于哪个用户组3)其他用户:不是文件所有者,不属于文件所属用户组的用户,称为其他用户 2.Linux文件权限我们切 ...
- PHP 文件迭代器
使用了SPL的 迭代器, 可以直接对打开的文件进行foreach读取, 类的构造如下 class fileIterator implements Iterator { private $fp; pri ...