`
L.G.Alexander
  • 浏览: 158379 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

3、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里面配置
<!-- 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的声明式事务:
   
<!-- 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
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 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 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