整合Struts2、Hibernate、Spring
将项目中的对象和对象之间的管理,纳入spring容器,由spring管理
1 实现spring+hibernate集成
1.1 新建web项目
建立项目的包结构(package)
1.2加入jar包
1.3 建立pojo类
package org.guangsoft.pojo;
/***
* 定部门的pojo类
* **/
public class Dept
{
private Integer did;
private String dname;
private String ddesc;
public Integer getDid()
{
return did;
}
public void setDid(Integer did)
{
this.did = did;
}
public String getDname()
{
return dname;
}
public void setDname(String dname)
{
this.dname = dname;
}
public String getDdesc()
{
return ddesc;
}
public void setDdesc(String ddesc)
{
this.ddesc = ddesc;
}
}
1.4 建立pojo的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjsxt.pojo">
<!-- 类 到 表 -->
<class name="Dept" table="t_dept">
<id name="did" column="did" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<!-- 其他简单属性 -->
<property name="dname" column="dname" type="java.lang.String"></property>
<property name="ddesc" column="ddesc" type="java.lang.String"></property>
</class>
</hibernate-mapping>
1.5建立Dao接口
package org.guangsoft.dao;
import org.guangsoft.pojo.Dept;
/**
* 部门数据访问接口
* ***/
public interface DeptDao
{
public void addDept(Dept dept);
}
1.6建立Dao接口的实现类
package org.guangsoft.dao.impl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
/***
* 建立dao接口实现类:
* extends HibernateDaoSupport :
* 完成在dao类中获得session对象,hibernateTempldate对象
* ***/
@Repository
public class DeptDaoImpl extends HibernateDaoSupport
implements DeptDao
{
/***
* 给父类注入sessionFactory通过自动装配
* ***/
// private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory01(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}
@Override
public void addDept(Dept dept)
{
super.getHibernateTemplate().save(dept);
}
}
1.7建立业务service接口
package org.guangsoft.service;
import org.guangsoft.pojo.Dept;
/**
* 部门的业务接口
* ***/
public interface DeptService
{
public void saveDeptService(Dept dept);
}
1.8 建立service接口实现类
package org.guangsoft.service.impl;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
import org.guangsoft.service.DeptService;
/***
* 部门业务接口实现类
* ***/
public class DeptServiceImpl implements DeptService
{
// 声明dao对象
private DeptDao deptDao;
@Override
public void saveDeptService(Dept dept)
{
deptDao.addDept(dept);
}
}
1.9配置spring容器
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 加载db.xml -->
<import resource="db.xml" />
<!-- 加载transaction.xml -->
<import resource="transaction.xml" />
<!-- 开启注解扫描 -->
<context:component-scan
base-package="org.guangsoft.action,org.guangsoft.service.impl,
org.guangsoft.dao.impl"></context:component-scan>
</beans>
db.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 配置数据库连接池(DataSource) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 输入连接池的属性 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="1111"></property>
</bean>
<!-- 实例化sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的特性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>org/guangsoft/pojo/Dept.hbm.xml</value>
</list>
</property>
</bean>
</beans>
Transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 实例化事务管理器对象 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<!-- 声明事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 进行aop的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
id="pc" />
<!-- 织入 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
</beans>
1.10 建立测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp
{
@Test
public void addDept()
{
// 加载spring的配置文件,获得bean容器
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获得bean对象
DeptService deptService = (DeptService) ac.getBean("deptServiceImpl");
// 添加部门
Dept dept = new Dept();
dept.setDname("安慰部");
dept.setDdesc("逗你玩");
deptService.saveDeptService(dept);
}
}
2 spring+struts2集成
2.1加入jar
Struts2-spring-plugin.jar
2.2建立Action
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
// DeptAction在spring容器中是非单例的
public class DeptAction implements ModelDriven<Dept>
{
// 自动装配
@Autowired
private DeptService deptService;
// 声明部门对象
private Dept dept = new Dept();
@Override
public Dept getModel()
{
return dept;
}
/***
* 处理部门的添加请求
* **/
public String addDept()
{
deptService.saveDeptService(dept);
return Action.SUCCESS;
}
}
2.3进行struts2的配置
<?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>
<package name="dept" namespace="/" extends="struts-default">
<!-- class:是Action在spring容器中对应的id -->
<action name="deptAction_*" class="deptAction" method="{1}">
<result>/index.jsp</result>
</action>
</package>
</struts>
3.4在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<!-- 配置上下的初始化参数application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--通过监听器加载spring的配置iwenijan -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置open Session in view -->
<filter>
<filter-name>osiv</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置struts2的核心控制器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
2.5建立UI页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@
taglib prefix="s" uri="/struts-tags"%>
<%
String path =
request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="deptAction_addDept.action" method="post" theme="simple">
<div>
部门名称:
<s:textfield name="dname"></s:textfield>
</div>
<div>
部门描述:
<s:textfield name="ddesc"></s:textfield>
</div>
<div>
<s:submit value="提交"></s:submit>
</div>
</s:form>
</body>
</html>
整合Struts2、Hibernate、Spring的更多相关文章
- 重新学习之spring第四个程序,整合struts2+hibernate+spring
第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContex ...
- struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决
最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境
上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- Struts2+hibernate+spring 配置事物
今天自信看了看hibernate的事物配置问题,转载了其他人的日志,仅用来学习. struts+hibernate+spring事务配置 (2009-01-14 21:49:47) 转载▼ 标签: i ...
- Struts2+Hibernate+Spring 整合示例[转]
原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...
- Spring整合Struts2,Hibernate的xml方式
作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...
- 整合Struts2与Spring
一.需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件
- cpj-swagger分别整合struts2、spring mvc、servlet
cpj-swagger 原文地址:https://github.com/3cpj/swagger 1. Swagger是什么? 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可 ...
随机推荐
- 关于转录组比对STAR软件使用
参考文章:http://weibo.com/p/23041883f77c940102vbkd?sudaref=passport.weibo.com 软件连接:https://github.com/al ...
- Sturts2的action不执行任何方法的原因
今天用<s:url action="xxx">调用action的时候出现了一个“异常”, action里的任何方法都没有执行,直接返回success,而且没有任何报错. ...
- LUXURY 8
A - Gargari and Bishops Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- telnet: connect to address 127.0.0.1: Connection refused
telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...
- 数据源和JNDI的关系:
DataSource对象是由Tomcat提供的,因此不能在程序中采用创建一个实例的方式来生产DataSource对象,而需要采用Java的另一个技术JNDI,来获得DataSource对象的引用. T ...
- HDU 5074 Hatsune Miku(2014鞍山赛区现场赛E题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 解题报告:给出一个长度为n的序列,例如a1,a2,a3,a4......an,然后这个序列的美丽 ...
- 1 python学习——python环境配置
1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...
- java的ArrayList使用方法
在第N个数据后面添加一个数据 用法: 在第1个元素后面添加E list.add(1, "E");
- hibernate query.list() 返回的数据类型
在hibernate中,用hql语句查询实体类,采用list方法的返回结果为一个List,该List中封装的对象分为以下三种情况: 1.查询全部字段的情况下,如"from 实体类" ...
- centos 随意截屏
yum install kdegraphics 菜单路径: applications(应用程序)/Graphics(图形图像)/KSnapshot