整合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是一个规范和完整的框架,用于生成.描述.调用和可 ...
随机推荐
- nyoj 4 779 兰州烧饼
兰州烧饼 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...
- 【C语言入门教程】2.2 常量 与 变量
2.2 常量 与 变量 顾名思义,常量是运算中不能改变数值的数据类型,变量是可改变数值的数据类型.根据需要,可将一些在程序中不必改变数值的类型定义为常量,这样也可避免因修改数值造成程序错误.任何改变常 ...
- linux查找某一进程并杀死
1. 查找redis进程 ps -ef|grep redis-server 2.打印第二个参数,因为上面第二列是进程号 3.这两个进程号有一个是grep进程号,所以要去掉,反选 grep ps ...
- 深入理解Java虚拟机之读书笔记三 内存分配策略
一般的内存分配是指堆上的分配,但也可能经过JIT编译后被拆散为标量类型并间接地在栈上分配.对象主要分配在新生代的Eden区上,如果启动了本地线程分配缓冲,将按线程优先在TLAB上分配,少数情况下直接分 ...
- linux中chmod更改文件权限命令
1. 命令格式: chmod [-cfvR] [--help] [--version] mode file 2. 命令功能: 用于改变文件或目录的访问权限,用它控制文件或目录的访问权限. 3. 命令参 ...
- Linux_VPN—pptpd构架方法
以下是由本人测试可用的pptpd构架方法 按步骤: 运行环境Centeros 6 *首先运行如下命令: cat /dev/net/tun 返回的必须是: cat: /dev/net/tun: File ...
- 11.6---矩阵查找元素(CC150)
思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...
- python日志浅析
输出日志对于追踪问题比较重要. 默认logger(root) python使用logging模块来处理日志.通常下面的用法就能满足常规需求: import logging logging.debug( ...
- python gui之tkinter语法杂记
随手写的,后续整理. 1. Entry如何调用父类的__init__? 以下错,原因后续分析 super(Your_Entry_Class, self).__init__(self,**kw) 报错: ...
- 4. 如何解释dalvik字节码
如何解释dalvik字节码 文档: 在Android系统源码目录dalvik\docs有相关指令文档 dalvik-bytecode.html 实战: 来直接实战模拟来理解枯燥的理论 用IDA打开一个 ...