`
liyanboss
  • 浏览: 140283 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

struts2+spring2+hibernate3 Annotation的整合

阅读更多
继续前两篇文章[在struts2中使用JFreeChart ]和[在struts2中使用JasperReports ],这次是要从数据库中读取数据,再用图表的形式来显示,所以就需要整合struts2,spring2,hibernate3 Annotation,现在把遇到的问题整理出来,以备查。

1、首先要在web.xml中添加:
<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中添加:
<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的代码:
<?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的代码:
<?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代码:
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;
	}

}

数据库脚本:
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晒出来,给大家分享。
4
3
分享到:
评论
4 楼 liyanboss 2008-12-31  
caoyangx 写道

&nbsp;&nbsp;

有那么吃惊吗?
3 楼 caoyangx 2008-12-31  
  
2 楼 tiandis 2008-12-04  
加struts2-spring-plugin-2.0.11.jar,web.xml里却是ContextLoaderListener?
1 楼 kingshare01 2008-08-11  

相关推荐

Global site tag (gtag.js) - Google Analytics