重新学习之spring第四个程序,整合struts2+hibernate+spring

简介: 第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4)   第二步:编写web.xml 和struts.xml和applicationContext.

第一步:导入三大框架的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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   
 8     <context-param>
 9               <description>
10                 将applicationContext.xml放在src目录下,依然能够找到该配置文件
11             </description>
12           
13             <param-name>contextConfigLocation</param-name>
14               <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
15     </context-param>
16      <!-- 配置CharacterEncoding,设置字符集      -->  
17       <filter>     
18         <filter-name>characterEncodingFilter</filter-name>     
19         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>     
20         <init-param>     
21           <param-name>encoding</param-name>     
22           <param-value>UTF-8</param-value>     
23         </init-param>     
24         <init-param>     
25           <param-name>forceEncoding</param-name>     
26           <param-value>true</param-value>     
27         </init-param>     
28       </filter>     
29          
30       <filter-mapping>     
31         <filter-name>characterEncodingFilter</filter-name>     
32         <url-pattern>/*</url-pattern>     
33       </filter-mapping>
34    
35     <listener>
36           <description>
37             项目启动时,创建Ioc容器,将项目下所有费数据类创建对象,并注入,建立对象之间的关系 
38         </description>
39           
40         <listener-class>
41             org.springframework.web.context.ContextLoaderListener</listener-class>
42     </listener>
43     
44     
45      <!-- 配置Spring自动管理Session. 要配置到struts过滤器之前扩大session生命周期!-->  
46      <filter>  
47          <filter-name>hibernateSessionFilter</filter-name>  
48          <filter-class>  
49      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   
50          </filter-class>  
51      </filter>  
52      <filter-mapping>  
53          <filter-name>hibernateSessionFilter</filter-name>  
54          <url-pattern>/*</url-pattern>  
55      </filter-mapping>
56      
57        <!-- 页面session配置 -->
58       <session-config>     
59         <session-timeout>20</session-timeout>     
60       </session-config>     
61        
62   
63     <!-- struts2拦截器,将所有请求拦截到struts2的框架中 -->
64   <filter>
65         <filter-name>struts2</filter-name>
66         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
67     </filter>
68 
69     <filter-mapping>
70         <filter-name>struts2</filter-name>
71         <url-pattern>/*</url-pattern>
72     </filter-mapping>
73   
74 </web-app>
View Code

struts.xml

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 如果请求地址=actionName!methodName ,则该配置需要进行设置,否则访问地址错误-->
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 9     
10     <!-- 开发模式 -->
11     <constant name="struts.devMode" value="true" />
12     
13     <!-- 编码格式过滤 -->
14     <constant name="struts.i18n.encoding" value="utf-8"></constant>
15     
16    <!-- 告诉struts.xml不要自己通过反射new,对象,去spring的ioc容器中找
17             action中的class='spring中Ioc容器中对象的id'
18             annotation注解生成对象默认情况下id值为是:类名首字符小写
19             需要加jar包struts-spring-plugin..jar
20          -->
21     <constant name="struts.objectFactory" value="spring"></constant>
22     
23     
24     <package name="default" namespace="/" extends="struts-default">
25         <!-- actionName!methodName请求方式的配置 -->
26         <action name="StudentAction" class="StudentAction">
27             <result name="success">/page/success.jsp</result>
28         </action>
29     </package>
30 </struts>
View Code

 

applicationContext.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:aop="http://www.springframework.org/schema/aop"
  5         xmlns:tx="http://www.springframework.org/schema/tx"
  6         xsi:schemaLocation="
  7             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  9             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 10     
 11         
 12         <!-- 数据库连接池,以及建立数据库连接 -->
 13         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 14             <!-- 驱动 -->
 15             <property name="driverClass" value="com.mysql.jdbc.Driver">
 16             
 17             </property>
 18             <!-- 数据库地址 -->
 19             <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test">
 20                 
 21             </property>
 22             <!-- 默认初始化获取3个连接  -->
 23             <!-- 空闲连接检查时间 -->
 24             <property name="idleConnectionTestPeriod" value="18000"></property>
 25             <!-- 最大空闲连接时间 3小时 -->
 26             <property name="maxIdleTime" value="25000"></property>
 27             <!-- 检查获取的连接是否有效 -->
 28             <property name="testConnectionOnCheckin" value="true"></property>
 29             <property name="testConnectionOnCheckout" value="true"></property>
 30             <!-- 测试语句 -->
 31             <property name="preferredTestQuery" value="select 1"></property>
 32             <!-- 连接数据库 -->
 33             <property name="properties">
 34                 <props>
 35                         <prop key="user">root</prop>
 36                         <prop key="password">1234</prop>
 37                         <prop key="c3p0.acquire_increment">5</prop>
 38                         <prop key="c3p0.idle_test_period">18000</prop>
 39         
 40                         <!--  连接空闲超时时间  -->
 41                         <prop key="c3p0.timeout">20000</prop>
 42                         <prop key="c3p0.max_size">40</prop>
 43                         <prop key="c3p0.max_statements">100</prop>
 44                         <prop key="c3p0.min_size">10</prop>
 45                 </props>
 46             </property>    
 47         </bean>
 48         
 49         
 50         
 51         <!-- 替代hibernate中hibernate.cfg.xml包括:连接数据库信息,实体类和表的映射桥梁,全局参数的配置 -->
 52         <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 53             <!-- 
 54                 一般有三类配置信息!!
 55                     1.  和数据库连接信息。 
 56                     2.  全局参数配置   比如缓存配置, sql打印信息,等等。。
 57                     3.  mapping         
 58          -->
 59             <!-- 【1】数据库连接 -->
 60             <property name="dataSource" >
 61                 <ref bean="dataSource"/>
 62             </property>
 63             <!-- 【2】参数配置 -->
 64             <property name="hibernateProperties">
 65                 <props>
 66                     <!-- 一些hibernate框架的设置 -->
 67                         <!-- hibernate 会自动生成sql。  为了能够屏蔽 数据库的差异。  需要配置  数据库方言-->
 68                          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 69                          
 70                         <!-- 如果数据库中无相应的表的话,则自动生成一个与po对应的表  -->
 71                          <prop key="hibernate.hbm2ddl.auto">update</prop>
 72                             
 73                         <!-- 在服务器后台打印出hibernate映射的sql语句 ,格式打印sql语句-->
 74                         <prop key="hibernate.show_sql" >true</prop>
 75                         <prop key="hibernate.format_sql" >true</prop>     
 76                </props>
 77             </property>
 78         
 79             <!-- 【3】mapping数据模型和数据库的桥梁,只需要配置到路径,无需一个个配置 -->
 80             <property name="mappingDirectoryLocations">
 81                 <list>
 82                     <value>classpath:/com/bjsxt/sxf/po</value>
 83                 </list>
 84             </property>
 85     </bean>
 86     
 87     
 88     <!-- 在ioc容器中创建HibernateTemplate对象,并将sessionFactory工厂的对象,注入其中。 -->
 89     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
 90         <property name="sessionFactory" ref="sessionFactory"></property>
 91     </bean>
 92     
 93     
 94     <!-- 事务 -->
 95     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 96         <property name="sessionFactory" ref="sessionFactory"></property>
 97     </bean>
 98     
 99     <!-- 动态批量代理,事务,确保实际业务逻辑的完整性,合理性。比如银行转账的事务 -->
100      <tx:advice id="txadvice" transaction-manager="transactionManager">     
101         <tx:attributes>     
102             <tx:method name="get*" propagation="REQUIRED"   read-only="true"/>     
103             <tx:method name="find*" propagation="REQUIRED"  read-only="true"/>
104             <tx:method name="query*" propagation="REQUIRED" read-only="true"/>        
105             <tx:method name="*" propagation="REQUIRED" />     
106         </tx:attributes>     
107     </tx:advice>   
108     <!-- 面向切面编程 -->
109     <aop:config proxy-target-class="true">     
110         <!-- 切面 -->
111         <aop:pointcut id="serviceMethods" expression="execution(* com.bjsxt.sxf.service.impl.*.*(..))"/>     
112         <aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethods"/>     
113     </aop:config>  
114     
115     <!-- 导入各种包,将bean分类。进行配置。 -->
116     <import resource="applicationContext-dao.xml"/>
117     <import resource="applicationContext-service.xml"/>
118     <import resource="applicationContext-action.xml"/>
119     
120 </beans>
View Code

 

applicationContext-dao.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7 
 8 
 9     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
10             <property name="sessionFactory" ref="sessionFactory"></property>
11     </bean>
12     
13     <bean id="StudentDao" class="com.bjsxt.sxf.dao.StudentDao" scope="prototype"  >
14         <property name="hibernateTemplate" ref="hibernateTemplate"></property>
15     </bean>
16 </beans>
View Code

 

applicationContext-service.xml

 

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3     xmlns="http://www.springframework.org/schema/beans"
4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5     xmlns:p="http://www.springframework.org/schema/p"
6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7     
8     <bean id="StudentService" class="com.bjsxt.sxf.service.impl.StudentServiceImpl" scope="prototype" autowire="byType"></bean>
9 </beans>
View Code

 

applicationContext-action.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7     
 8     <bean id="StudentAction" class="com.bjsxt.sxf.action.StudentAction" scope="prototype" autowire="byType" ></bean>
 9     
10 </beans>
View Code

 

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

项目布局

student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping 
 6     package="com.bjsxt.sxf.po"><!-- 实体类包名 -->
 7 
 8     <class name="Student" table="shang_student">
 9         
10         
11         <!-- 主键递增 -->
12         <id name="id" column="id">
13             <generator class="native"></generator>
14         </id>
15         
16         <!-- 学生名字 -->
17         <property name="name" column="name"></property>
18         <!-- 学生性别 -->
19         <property name="sex" column="sex"></property>
20         
21         
22     </class>
23     
24 </hibernate-mapping>
View Code

添加一个用户

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

StudenAction省去setget方法

 1 public class StudentAction {
 2     private StudentService studentService;
 3     
 4     
 5     public String add(){
 6         System.out.println("StudentAction.add(8888888888888)");
 7         Student student =new Student();
 8         student.setName("shangxiaoyan");
 9         student.setSex("女");
10         studentService.addStudent(student);
11         return null;
12     }
View Code

 

相关文章
|
8天前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
1月前
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
|
7月前
|
监控 Java 应用服务中间件
微服务——SpringBoot使用归纳——为什么学习Spring Boot
本文主要探讨为什么学习Spring Boot。从Spring官方定位来看,Spring Boot旨在快速启动和运行项目,简化配置与编码。其优点包括:1) 良好的基因,继承了Spring框架的优点;2) 简化编码,通过starter依赖减少手动配置;3) 简化配置,采用Java Config方式替代繁琐的XML配置;4) 简化部署,内嵌Tomcat支持一键式启动;5) 简化监控,提供运行期性能参数获取功能。此外,从未来发展趋势看,微服务架构逐渐成为主流,而Spring Boot作为官方推荐技术,与Spring Cloud配合使用,将成为未来发展的重要方向。
221 0
微服务——SpringBoot使用归纳——为什么学习Spring Boot
|
4月前
|
安全 Java 数据库
Spring Boot 框架深入学习示例教程详解
本教程深入讲解Spring Boot框架,先介绍其基础概念与优势,如自动配置、独立运行等。通过搭建项目、配置数据库等步骤展示技术方案,并结合RESTful API开发实例帮助学习。内容涵盖环境搭建、核心组件应用(Spring MVC、Spring Data JPA、Spring Security)及示例项目——在线书店系统,助你掌握Spring Boot开发全流程。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
523 2
|
6月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
155 20
|
10月前
|
前端开发 Java 开发者
这款免费 IDEA 插件让你开发 Spring 程序更简单
Feign-Helper 是一款支持 Spring 框架的 IDEA 免费插件,提供 URL 快速搜索、Spring Web Controller 路径一键复制及 Feign 与 Controller 接口互相导航等功能,极大提升了开发效率。
1129 1
|
11月前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
237 9
|
12月前
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
149 9
|
12月前
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
101 1
|
Java 测试技术 开发者
springboot学习四:Spring Boot profile多环境配置、devtools热部署
这篇文章主要介绍了如何在Spring Boot中进行多环境配置以及如何整合DevTools实现热部署,以提高开发效率。
540 2
下一篇
oss教程