码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-09-02 15:35:04      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   java   strong   

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

bubuko.com,布布扣

bubuko.com,布布扣

 

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

web.xml

bubuko.com,布布扣
 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

 

bubuko.com,布布扣
 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

 

bubuko.com,布布扣
  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

 

bubuko.com,布布扣
 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

 

bubuko.com,布布扣
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

 

bubuko.com,布布扣
 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

 

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

项目布局

bubuko.com,布布扣

student.hbm.xml

bubuko.com,布布扣
 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方法

bubuko.com,布布扣
 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

 

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

标签:des   style   blog   http   color   os   io   java   strong   

原文地址:http://www.cnblogs.com/shangxiaofei/p/3951469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!