struts2+spring2+hibernate3 Annotation的整合

[color=darkblue]继续前两篇文章[[url=http://liyanboss.iteye.com/admin/blogs/181282]在struts2中使用JFreeChart [/url]]和[[url=http://liyanboss.iteye.com/admin/blogs/180735]在struts2中使用JasperReports [/url]],这次是要从数据库中读取数据,再用图表的形式来显示,所以就需要整合struts2,spring2,hibernate3 Annotation,现在把遇到的问题整理出来,以备查。[/color]

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是用#注释掉的,很奇怪??? 8) 8) 所以我就把它加到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里就可以了。
[img]http://liyanboss.iteye.com/upload/picture/pic/12039/4f25f53d-e317-3e2a-8152-107dfd80db9b.jpg[/img]
需要注意的一点是,一定要到struts2里面把struts2-spring-plugin-2.0.11.jar加进来,在struts2中,spring的引入采用了plugin的方式,不同与之前的webwork2,本人觉得这样更好呀。

[color=darkblue]除了jar以外的source code晒出来,给大家分享。[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值