`
xiangxingchina
  • 浏览: 520257 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2+Spring+Hibernate整合(续Struts2+Spring整合)

阅读更多

前面已经讲述了Struts2+Spring整合,这里我们接着整合Hibernate
整合Hibernate
1)导入Hibernate3.1的jar包:antlr-2.7.6.jar,commons-collections- 3.1.jar,dom4j-1.6.1.jar,javassist-3.4.GA.jar,jta- 1.1.jar,hibernate3.jar,slf4j-api-1.5.6.jar,slf4j- log4j12-1.5.6.jar,log4j-1.2.13.jar。暂时导入这些jar包,到时候需要再倒入。
2)将spring的配置文件applicationContext-*.xml文件放置在OA工程的src目录下,这里我们有三个:
applicationContext-action.xml,applicationContext-beans.xml,applicationContext-common.xml
3)在web.xml里面配置

Xml代码
  1. <!-- when application server started,loading the applicationContext-*.xml -->   
  2.     < context-param >   
  3.         < param-name > contextConfigLoaction </ param-name >   
  4.         < param-value > classpath*:applicationContext-*.xml </ param-value >   
  5.     </ context-param >   
  6.       
  7.     <!-- integerate spring -->   
  8.     < listener >   
  9.     < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >   
  10.     </ listener >   
  11.       
  12.     <!-- let spring manage hibernate's session,that we can focus bussiness layer -->   
  13.     < filter >   
  14.         < filter-name > hibernateFilter </ filter-name >   
  15.         < filter-class > org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </ filter-class >   
  16.     </ filter >   
  17.     < filter-mapping >   
  18.         < filter-name > hibernateFilter </ filter-name >   
  19.         < url-pattern > *.action </ url-pattern >   
  20.     </ filter-mapping >   
  21.       
  22.      <!-- configrate struts2 core filter -->   
  23.     < filter >   
  24.         < filter-name > struts2 </ filter-name >   
  25.         < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >   
  26.     </ filter >   
  27.     < filter-mapping >   
  28.         < filter-name > struts2 </ filter-name >   
  29.         < url-pattern > /* </ url-pattern >   
  30.     </ filter-mapping >   
  31.       
  32.     <!-- configurate web character encoding -->   
  33.     < filter >   
  34.         < filter-name > encodingFilter </ filter-name >   
  35.         < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >   
  36.         < init-param >   
  37.             < param-name > encoding </ param-name >   
  38.             < param-value > utf-8 </ param-value >   
  39.         </ init-param >   
  40.     </ filter >   
  41.       
  42.     < filter-mapping >   
  43.         < filter-name > encodingFilter </ filter-name >   
  44.         < url-pattern > /* </ url-pattern >   
  45.     </ filter-mapping >   
<!-- when application server started,loading the applicationContext-*.xml -->
	<context-param>
	    <param-name>contextConfigLoaction</param-name>
	    <param-value>classpath*:applicationContext-*.xml</param-value>
	</context-param>
	
	<!-- integerate spring -->
	<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- let spring manage hibernate's session,that we can focus bussiness layer -->
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
     <!-- configrate struts2 core filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- configurate web character encoding -->
    <filter>
        <filter-name>encodingFilter</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>
    </filter>
    
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

现在新出了gb18030,它的范围比gbk大一点,而gbk又比gb2312大一点
gb18030>gbk>gb2312
4)spring里面有声明式事务,它对hibernate做了封装,在applicationContext-common.xml里面配置spring的声明式事务:

   
Xml代码
  1. <!-- configure sessionFactory -->   
  2.     < bean   id = "sessionFactory"   class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
  3.         < property   name = "configLocation" >   
  4.             < value > classpath:hibernate.cfg.xml </ value >   
  5.         </ property >   
  6.     </ bean >   
  7.       
  8.     <!-- configure transaction manager-->   
  9.     < bean   id = "transactionManager"   class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >   
  10.         < property   name = "sessionFactory" >   
  11.             < ref   bean = "sessionFactory" />   
  12.         </ property >   
  13.     </ bean >   
  14.       
  15.     <!-- configure transaction's propagational feature -->   
  16.     < tx:advice   id = "txAdvice"   transaction-manager = "transactionManager" >   
  17.         < tx:attributes >   
  18.             < tx:method   name = "add*"   propagation = "REQUIRED" />   
  19.             < tx:method   name = "delete*"   propagation = "REQUIRED" />   
  20.             < tx:method   name = "modify*"   propagation = "REQUIRED" />   
  21.             < tx:method   name = "*"   read-only = "true" />   
  22.         </ tx:attributes >   
  23.     </ tx:advice >   
  24.       
  25.     <!-- configure which class's which method take part in transaction -->   
  26.     < aop:config >   
  27.         < aop:pointcut   id = "allManagerMethod"   expression = "execution(* com.struts2.server.*.*(..))" />   
  28.         < aop:advisor   pointcut-ref = "allManagerMethod"   advice-ref = "txAdvice" />   
  29.     </ aop:config >   
<!-- configure sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>
    
    <!-- configure transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    <!-- configure transaction's propagational feature -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- configure which class's which method take part in transaction -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution(* com.struts2.server.*.*(..))"/>
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
    </aop:config>

用到了spring的aop,因此我们把spring的aop的jar包 aspectjrt.jar和aspectjweaver.jar加入到lib目录。spring没有开发自己的aop的jar包,它也是利用别人已经开 发好的jar包aspectjrt.jar和aspectjweaver.jar。

5)测试Struts2+Spring+Hibernate整合是否成功
第一步:测试Spring+Hibernate整合是否成功
写一个需要映射的值对象Person

Java代码
  1. package  com.oa.model;  
  2. /**  
  3.  * @author lukuijun@hanqinet.com  
  4.  * @hibernate.class talbe="t_person"  
  5.  */   
  6. public   class  Person {  
  7.     /**  
  8.      * @hibernate.id generator-class="native"   
  9.      */   
  10.     private   int  id;  
  11.     /**  
  12.      * @hibernate.property  
  13.      */   
  14.     private  String name;  
  15.     /**  
  16.      * @hibernate.property  
  17.      */   
  18.     private  String sex;  
  19.     /**  
  20.      * @hibernate.property  
  21.      */   
  22.     private  String address;  
  23.     /**  
  24.      * @hibernate.property  
  25.      */   
  26.     private  String duty;  
  27.     /**  
  28.      * @hibernate.property  
  29.      */   
  30.     private  String phone;  
  31.     /**  
  32.      * @hibernate.property  
  33.      */   
  34.     private  String description;  
  35.     public   int  getId() {  
  36.         return  id;  
  37.     }  
  38.     public   void  setId( int  id) {  
  39.         this .id = id;  
  40.     }  
  41.     public  String getName() {  
  42.         return  name;  
  43.     }  
  44.     public   void  setName(String name) {  
  45.         this .name = name;  
  46.     }  
  47.     public  String getSex() {  
  48.         return  sex;  
  49.     }  
  50.     public   void  setSex(String sex) {  
  51.         this .sex = sex;  
  52.     }  
  53.     public  String getAddress() {  
  54.         return  address;  
  55.     }  
  56.     public   void  setAddress(String address) {  
  57.         this .address = address;  
  58.     }  
  59.     public  String getDuty() {  
  60.         return  duty;  
  61.     }  
  62.     public   void  setDuty(String duty) {  
  63.         this .duty = duty;  
  64.     }  
  65.     public  String getPhone() {  
  66.         return  phone;  
  67.     }  
  68.     public   void  setPhone(String phone) {  
  69.         this .phone = phone;  
  70.     }  
  71.     public  String getDescription() {  
  72.         return  description;  
  73.     }  
  74.     public   void  setDescription(String description) {  
  75.         this .description = description;  
  76.     }  
  77. }  
package com.oa.model;
/**
 * @author lukuijun@hanqinet.com
 * @hibernate.class talbe="t_person"
 */
public class Person {
	/**
	 * @hibernate.id generator-class="native" 
	 */
	private int id;
	/**
	 * @hibernate.property
	 */
	private String name;
	/**
	 * @hibernate.property
	 */
	private String sex;
	/**
	 * @hibernate.property
	 */
	private String address;
	/**
	 * @hibernate.property
	 */
	private String duty;
	/**
	 * @hibernate.property
	 */
	private String phone;
	/**
	 * @hibernate.property
	 */
	private String description;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getDuty() {
		return duty;
	}
	public void setDuty(String duty) {
		this.duty = duty;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}

运行ant生成Person.hbm.xml映射文件:

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  3.   
  4. < hibernate-mapping >   
  5.   < class   table = "t_person"   name = "com.oa.model.Person" >   
  6.     < id   access = "field"   name = "id" >   
  7.       < generator   class = "native" />   
  8.     </ id >   
  9.     < property   name = "name"   access = "field" />   
  10.     < property   name = "sex"   access = "field" />   
  11.     < property   name = "address"   access = "field" />   
  12.     < property   name = "duty"   access = "field" />   
  13.     < property   name = "phone"   access = "field" />   
  14.     < property   name = "description"   access = "field" />   
  15.   </ class >   
  16. </ hibernate-mapping >   
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="t_person" name="com.oa.model.Person">
    <id access="field" name="id">
      <generator class="native"/>
    </id>
    <property name="name" access="field"/>
    <property name="sex" access="field"/>
    <property name="address" access="field"/>
    <property name="duty" access="field"/>
    <property name="phone" access="field"/>
    <property name="description" access="field"/>
  </class>
</hibernate-mapping>

运行ant生成hiberante.cfg.xml文件:

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  3.   
  4. < hibernate-configuration >   
  5.   < session-factory >   
  6.     < property   name = "hibernate.connection.driver_class" > com.mysql.jdbc.Driver </ property >   
  7.     < property   name = "hibernate.connection.url" > jdbc:mysql://localhost:3306/oa </ property >   
  8.     < property   name = "hibernate.connection.username" > root </ property >   
  9.     < property   name = "hibernate.connection.password" > hanqinet </ property >   
  10.     < property   name = "hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >   
  11.     < property   name = "hibernate.show_sql" > true </ property >   
  12.     < property   name = "hibernate.hbm2ddl.auto" > update </ property >   
  13.     < mapping   resource = "com/oa/model/Person.hbm.xml" />   
  14.   </ session-factory >   
  15. </ hibernate-configuration >   
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/oa</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">hanqinet</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="com/oa/model/Person.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

接着启动Tomcat,启动完毕检查数据库里面新增加了表t_person,Spring2+Hibernate整合成功
第二步:测试Struts2+Spring整合是否成功
参考1、Struts2+Spring整合,测试成功
完成这个两步成功,说明我们的框架整合成功!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics