第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4)

第二步:编写web.xml 和struts.xml和applicationContext.xml和applicationContext-service.xml和application-actionContext.xml和applicationContext-dao.xml

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"> <context-param>
<description>
将applicationContext.xml放在src目录下,依然能够找到该配置文件
</description> <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 配置CharacterEncoding,设置字符集 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<description>
项目启动时,创建Ioc容器,将项目下所有费数据类创建对象,并注入,建立对象之间的关系
</description> <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置Spring自动管理Session. 要配置到struts过滤器之前扩大session生命周期!-->
<filter>
<filter-name>hibernateSessionFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 页面session配置 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> <!-- struts2拦截器,将所有请求拦截到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> </web-app>

struts.xml

 <?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>
<!-- 如果请求地址=actionName!methodName ,则该配置需要进行设置,否则访问地址错误-->
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 开发模式 -->
<constant name="struts.devMode" value="true" /> <!-- 编码格式过滤 -->
<constant name="struts.i18n.encoding" value="utf-8"></constant> <!-- 告诉struts.xml不要自己通过反射new,对象,去spring的ioc容器中找
action中的class='spring中Ioc容器中对象的id'
annotation注解生成对象默认情况下id值为是:类名首字符小写
需要加jar包struts-spring-plugin..jar
-->
<constant name="struts.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default">
<!-- actionName!methodName请求方式的配置 -->
<action name="StudentAction" class="StudentAction">
<result name="success">/page/success.jsp</result>
</action>
</package>
</struts>

applicationContext.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: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/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"> <!-- 数据库连接池,以及建立数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"> </property>
<!-- 数据库地址 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"> </property>
<!-- 默认初始化获取3个连接 -->
<!-- 空闲连接检查时间 -->
<property name="idleConnectionTestPeriod" value="18000"></property>
<!-- 最大空闲连接时间 3小时 -->
<property name="maxIdleTime" value="25000"></property>
<!-- 检查获取的连接是否有效 -->
<property name="testConnectionOnCheckin" value="true"></property>
<property name="testConnectionOnCheckout" value="true"></property>
<!-- 测试语句 -->
<property name="preferredTestQuery" value="select 1"></property>
<!-- 连接数据库 -->
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.idle_test_period">18000</prop> <!-- 连接空闲超时时间 -->
<prop key="c3p0.timeout">20000</prop>
<prop key="c3p0.max_size">40</prop>
<prop key="c3p0.max_statements">100</prop>
<prop key="c3p0.min_size">10</prop>
</props>
</property>
</bean> <!-- 替代hibernate中hibernate.cfg.xml包括:连接数据库信息,实体类和表的映射桥梁,全局参数的配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--
一般有三类配置信息!!
1. 和数据库连接信息。
2. 全局参数配置 比如缓存配置, sql打印信息,等等。。
3. mapping
-->
<!-- 【1】数据库连接 -->
<property name="dataSource" >
<ref bean="dataSource"/>
</property>
<!-- 【2】参数配置 -->
<property name="hibernateProperties">
<props>
<!-- 一些hibernate框架的设置 -->
<!-- hibernate 会自动生成sql。 为了能够屏蔽 数据库的差异。 需要配置 数据库方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 如果数据库中无相应的表的话,则自动生成一个与po对应的表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 在服务器后台打印出hibernate映射的sql语句 ,格式打印sql语句-->
<prop key="hibernate.show_sql" >true</prop>
<prop key="hibernate.format_sql" >true</prop>
</props>
</property> <!-- 【3】mapping数据模型和数据库的桥梁,只需要配置到路径,无需一个个配置 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/bjsxt/sxf/po</value>
</list>
</property>
</bean> <!-- 在ioc容器中创建HibernateTemplate对象,并将sessionFactory工厂的对象,注入其中。 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 动态批量代理,事务,确保实际业务逻辑的完整性,合理性。比如银行转账的事务 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 面向切面编程 -->
<aop:config proxy-target-class="true">
<!-- 切面 -->
<aop:pointcut id="serviceMethods" expression="execution(* com.bjsxt.sxf.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethods"/>
</aop:config> <!-- 导入各种包,将bean分类。进行配置。 -->
<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
<import resource="applicationContext-action.xml"/> </beans>

applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="StudentDao" class="com.bjsxt.sxf.dao.StudentDao" scope="prototype" >
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
</beans>

applicationContext-service.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="StudentService" class="com.bjsxt.sxf.service.impl.StudentServiceImpl" scope="prototype" autowire="byType"></bean>
</beans>

applicationContext-action.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="StudentAction" class="com.bjsxt.sxf.action.StudentAction" scope="prototype" autowire="byType" ></bean> </beans>

第三步:测试--项目布局。

项目布局

student.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">
<hibernate-mapping
package="com.bjsxt.sxf.po"><!-- 实体类包名 --> <class name="Student" table="shang_student"> <!-- 主键递增 -->
<id name="id" column="id">
<generator class="native"></generator>
</id> <!-- 学生名字 -->
<property name="name" column="name"></property>
<!-- 学生性别 -->
<property name="sex" column="sex"></property> </class> </hibernate-mapping>

添加一个用户

访问localhost:8080/Struts2HibernateSpring/StudentAction!add向数据库中的表添加一个用户

StudenAction省去setget方法

 public class StudentAction {
private StudentService studentService; public String add(){
System.out.println("StudentAction.add(8888888888888)");
Student student =new Student();
student.setName("shangxiaoyan");
student.setSex("女");
studentService.addStudent(student);
return null;
}

重新学习之spring第四个程序,整合struts2+hibernate+spring的更多相关文章

  1. struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决

    最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...

  2. Struts2+hibernate+spring 配置事物

    今天自信看了看hibernate的事物配置问题,转载了其他人的日志,仅用来学习. struts+hibernate+spring事务配置 (2009-01-14 21:49:47) 转载▼ 标签: i ...

  3. 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效

    基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...

  4. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  5. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  6. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  7. Spring第四天——SSH整合

    (从整合开始,使用回归使用eclipse) 一.三大框架版本:  struts2 hibernate5 spring4 二.SSH三大框架回顾: Hibernate: ORM思想 核心配置文件: 单独 ...

  8. struts1,struts2,hibernate,spring的运行原理结构图

    一.struts1运行原理 1.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(s ...

  9. Spring整合Struts2,Hibernate的xml方式

    作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...

随机推荐

  1. CCPC2018-湖南全国邀请赛 Solution

    A - Easy $h$-index 后缀扫一下 #include <bits/stdc++.h> using namespace std; #define ll long long #d ...

  2. JSDoc 注释规范

    命令名描述 @param @argument 指定参数名和说明来描述一个函数参数@returns 描述函数的返回值@author 指示代码的作者@deprecated 指示一个函数已经废弃,而且在将来 ...

  3. “System.Runtime.InteropServices.COMException (0x80070422): 无法启动服务”解决方法

    应用程序中发生了无法处理的异常.如果单击“退出”,应用程序将立即关闭.无法启动服务,原因可能是已被禁用或其相关联设备没有启动.(异常来自HRESULT:0X80070422).点击详细内容:有关调用实 ...

  4. Thinkphp在Lnmp环境下部署项目先后报错问题解决:_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Home/...Access denied.

    首先报错:_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Home/769e70f2e46f34ceb60619bbda5e4691.php 解决此 ...

  5. Windows Shell编程之如何编写为文件对象弹出信息框的Shell扩展

    有关COM编程资料 转载:http://www.cnblogs.com/lzjsky/archive/2010/11/22/1884702.html 活动桌面引入一项新特性, 当你在某些特定对象上旋停 ...

  6. SPOJ—VLATTICE Visible Lattice Points(莫比乌斯反演)

    http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维 ...

  7. ZOJ 3747 Attack on Titans

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3747 题意: 现在有n个士兵进行排序,只有G.R.P三种士兵,要求至少有m ...

  8. UVa 11419 我是SAM(最小点覆盖+路径输出)

    https://vjudge.net/problem/UVA-11419 题意:一个网格里面有一些目标,可以从某一行,某一列发射一发子弹,可以打掉它:求最少的子弹,和在哪里打? 思路: 每个点的x坐标 ...

  9. UVa 10491 奶牛和轿车(全概率公式)

    https://vjudge.net/problem/UVA-10491 题意: 假设有a头牛,b辆车,在最终选择前主持人会替你打开c个有牛的门,输出"总是换门"的策略下,赢得车的 ...

  10. QWebEngine_C++_交互

    参考网址:http://blog.csdn.net/liuyez123/article/details/50509788 ZC: 该文章里面的: “ <ahref="javascrip ...