Spring+Struts2+Hibernate整合(基于注解)

文章摘要:
本文通过演示在前端JSP页面中往Oracle数据库插入一条数据,描述了Spring3、Struts2和Hibernate3三大框架的整合步骤。
工程不使用Struts、Hibernate的配置文件,通过注解完成三大框架的整合。

软件环境:
struts-2.3.15.1
spring-framework-3.2.0
hibernate-distribution-3.6.10
Oracle 11.2.0.1.0
MyEclipse 10.7

一、三大框架导入jar包
上一篇文章相同
新增支持struts2的注解开发的jar包:
struts2-convention-plugin-2.3.15.3.jar

二、web.xml配置文件
与非注解式的整合一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

   
    <!-- 配置Spring的监听器 -->
    <listener>
        <!-- 监听器默认加载的是:WEB-INF/applicationContext.xml -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
    <!-- 指定Spring框架配置文件的所在位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
   
    <!-- Struts2的核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

三、Spring配置文件
applicationContext.xml
主要区别:
sessionFactory采用annotation.AnnotationSessionFactoryBean而非LocalSessionFactoryBean;
映射使用packagesToScan扫描,不直接指定hbm文件;
无需配置Action、Service、Dao的注入关系(在Java类中使用注解注入)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

   
    <context:component-scan base-package="cn.jianyu.action,cn.jianyu.service,cn.jianyu.dao"></context:component-scan>
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
   
    <!-- 配置c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
   
    <!-- 配置Hibernate的其他属性 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置Hibernate的属性 -->
        <property name="hibernateProperties" >
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
            </props>
        </property>
       
        <!-- 映射扫描 -->
        <property name="packagesToScan">
            <list>
                <value>cn.jianyu.vo</value>
            </list>
        </property>
    </bean>
   
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <!-- 开启注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

四、包结构
1、Action类
取代了Struts的配置文件,通过继承ActionSupport与Spring集成;
定义Action并注入Service对象。
BookAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cn.jianyu.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.jianyu.service.BookService;
import cn.jianyu.vo.Book;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/")
@ParentPackage("struts-default")
@Controller("bookAction")
@Scope("prototype")
public class BookAction extends ActionSupport implements ModelDriven<Book>{
    private Book book = new Book();
    public Book getModel() {
        return book;
    }
   
    //在Action中注入Service
    @Autowired
    @Qualifier("bookService")
    private BookService bookService;
   
    @Action(value="book_add")
    public String add(){
        System.out.println("web层的add方法执行了");
        bookService.add(book);
        return NONE;
    }
}

2、Book实体类
主键和自动增长方式通过注解方式配置。
Book.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.jianyu.vo;

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

@Entity
@Table
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO) //Oracle不能使用IDENTITY
    private Integer id;
    private String name;
    private Double price; //不能定义成基本类型double
   
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
}

3、Service类
注入Dao对象
BookService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.jianyu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jianyu.dao.BookDao;
import cn.jianyu.vo.Book;

@Transactional
@Service("bookService")
public class BookService {
    @Autowired
    @Qualifier("bookDao")
    private BookDao bookDao;
   
    public void add(Book book) {
        System.out.println("Service层的add方法执行了");
        bookDao.save(book);
    }

}

4、Dao类
注入hibernateTemplate模板类对象,执行save方法。
BookDao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.jianyu.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.jianyu.vo.Book;

@Repository("bookDao")
public class BookDao {
   
    @Autowired
    @Qualifier("hibernateTemplate")
    private HibernateTemplate hibernateTemplate;
   
    public void save(Book book) {
        System.out.println("DAO层的save方法执行了");
        hibernateTemplate.save(book);
    }

}

源码见压缩包spring3_ssh3工程
https://github.com/baijy/springwork/tree/master/spring3_ssh3