struts2+spring2+hibernate3 Annotation的整合

本文介绍如何在Struts2框架中整合Spring和Hibernate3Annotation,实现从数据库读取数据并展示的方法。主要步骤包括配置web.xml和struts.xml,设置数据源和SessionFactory,并提供相关实体类和数据库脚本。
继续前两篇文章[在struts2中使用JFreeChart ]和[在struts2中使用JasperReports ],这次是要从数据库中读取数据,再用图表的形式来显示,所以就需要整合struts2,spring2,hibernate3 Annotation,现在把遇到的问题整理出来,以备查。

1、首先要在web.xml中添加:
Java代码 复制代码
  1. <context-param>   
  2.     <param-name>contextConfigLocation</param-name>   
  3.     <param-value>classpath:spring-*.xml</param-value>   
  4.  </context-param>   
  5.     
  6. <filter>   
  7.     <filter-name>struts2</filter-name>   
  8.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
  9. </filter>   
  10.   
  11. <filter>   
  12.     <filter-name>hibernateFilter</filter-name>   
  13.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>   
  14.    </filter>   
  15.       
  16. <filter-mapping>   
  17.     <filter-name>struts2</filter-name>   
  18.     <url-pattern>/*</url-pattern>   
  19. </filter-mapping>   
  20.       
  21. <filter-mapping>   
  22.     <filter-name>hibernateFilter</filter-name>   
  23.     <url-pattern>/*</url-pattern>   
  24. </filter-mapping>   
  25.     
  26.  <listener>   
  27.     <listener-class>   
  28.         org.springframework.web.context.ContextLoaderListener   
  29.     </listener-class>   
  30.  </listener>  
<context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>classpath:spring-*.xml</param-value>
 </context-param>
 
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter>
   	<filter-name>hibernateFilter</filter-name>
   	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
   </filter>
   
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
   
<filter-mapping>
	<filter-name>hibernateFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
 
 <listener>
 	<listener-class>
 		org.springframework.web.context.ContextLoaderListener
 	</listener-class>
 </listener>

添加spring的ContextLoaderListener,还有OpenSessionInViewFilter到web.xml中。
2、在struts.xml中添加:
Java代码 复制代码
  1. <constant name="struts.objectFactory" value="spring" />  
<constant name="struts.objectFactory" value="spring" />

添加一个constant,声明struts2使用的objectFactory是spring,不过好象struts2默认已经是使用spring作为objectFactory了,不加这一行也是可以的。不过我看在struts2-core-2.0.11.jar中的default.properties中的struts.objectFactory = spring是用#注释掉的,很奇怪???   所以我就把它加到struts.xml中了。
3、就是添加spring的配置文件了
spring-datasource.xml的代码:
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  4.        
  5. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">   
  6.     <property name="driverClassName" value="com.mysql.jdbc.Driver" />   
  7.     <property name="url" value="jdbc:mysql://localhost/LIYAN" />   
  8.     <property name="username" value="root" />   
  9.     <property name="password" value="123456" />   
  10. </bean>   
  11.   
  12. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost/LIYAN" />
	<property name="username" value="root" />
	<property name="password" value="123456" />
</bean>

</beans>

我用的是mysql,可以替换成其他database的连接。
spring-hibernate.xml的代码:
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   
  4.        
  5. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
  6.     <property name="dataSource" ref="dataSource" />   
  7.     <property name="hibernateProperties">   
  8.         <props>   
  9.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
  10.         </props>   
  11.     </property>   
  12.     <property name="annotatedClasses">   
  13.         <list>   
  14.             <value>com.liyanframework.chart.domain.Fruit</value>   
  15.             <value>com.liyanframework.chart.domain.Sales</value>   
  16.         </list>   
  17.     </property>   
  18. </bean>   
  19.   
  20. <!--bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  21.     <property name="dataSource" ref="dataSource" />   
  22.     <property name="hibernateProperties">   
  23.         <props>   
  24.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
  25.         </props>   
  26.     </property>   
  27.     <property name="mappingResources">   
  28.         <list>   
  29.             <value>com/liyanframework/chart/Fruit.hbm.xml</value>   
  30.         </list>   
  31.     </property>   
  32. </bean-->   
  33.   
  34. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
		</props>
	</property>
	<property name="annotatedClasses">
		<list>
			<value>com.liyanframework.chart.domain.Fruit</value>
			<value>com.liyanframework.chart.domain.Sales</value>
		</list>
	</property>
</bean>

<!--bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/liyanframework/chart/Fruit.hbm.xml</value>
		</list>
	</property>
</bean-->

</beans>

现在jdk早就支持Annotation了,所以我这里就是使用的hibernate的Annotation,省去了许多麻烦的hbm.xml文件了,同时也把普通的sessionFactory的配置晒出来了。
4、以下是domain object和sql script的相关文件
Sales.java代码:
Java代码 复制代码
  1. package com.liyanframework.chart.domain;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. import javax.persistence.Column;   
  6. import javax.persistence.Entity;   
  7. import javax.persistence.GeneratedValue;   
  8. import javax.persistence.GenerationType;   
  9. import javax.persistence.Id;   
  10. import javax.persistence.Table;   
  11.   
  12. @Entity  
  13. @Table(name="T_SALES")   
  14. public class Sales implements Serializable {   
  15.     private Long id;   
  16.   
  17.     private String month;   
  18.   
  19.     private int amount;   
  20.   
  21.     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)   
  22.     public Long getId() {   
  23.         return id;   
  24.     }   
  25.   
  26.     public void setId(Long id) {   
  27.         this.id = id;   
  28.     }   
  29.     @Column(name="SALES_AMOUNT")   
  30.     public int getAmount() {   
  31.         return amount;   
  32.     }   
  33.   
  34.     public void setAmount(int amount) {   
  35.         this.amount = amount;   
  36.     }   
  37.   
  38.     @Column(name="SALES_MONTH")   
  39.     public String getMonth() {   
  40.         return month;   
  41.     }   
  42.   
  43.     public void setMonth(String month) {   
  44.         this.month = month;   
  45.     }   
  46.   
  47. }  
package com.liyanframework.chart.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="T_SALES")
public class Sales implements Serializable {
	private Long id;

	private String month;

	private int amount;

	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	@Column(name="SALES_AMOUNT")
	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	@Column(name="SALES_MONTH")
	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

}

数据库脚本:
Java代码 复制代码
  1. CREATE TABLE IF NOT EXISTS T_SALES (   
  2.     ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,   
  3.     SALES_MONTH VARCHAR(255) NOT NULL UNIQUE,   
  4.     SALES_AMOUNT INTEGER NOT NULL   
  5. )ENGINE=INNODB;   
  6.   
  7. ALTER TABLE T_SALES AUTO_INCREMENT = 20000;  
CREATE TABLE IF NOT EXISTS T_SALES (
    ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    SALES_MONTH VARCHAR(255) NOT NULL UNIQUE,
    SALES_AMOUNT INTEGER NOT NULL
)ENGINE=INNODB;

ALTER TABLE T_SALES AUTO_INCREMENT = 20000;


5、最后就是要把相关的jar放到classpath里就可以了。

需要注意的一点是,一定要到struts2里面把struts2-spring-plugin-2.0.11.jar加进来,在struts2中,spring的引入采用了plugin的方式,不同与之前的webwork2,本人觉得这样更好呀。

除了jar以外的source code晒出来,给大家分享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值