原文 http://blog.csdn.net/tkd03072010/article/details/7468769
Spring整合Struts2、Hibernate原理概述:
从用户角度来看,用户发出HTTP请求,当MVC框架的控制器组件拦截到用户请求时,将调用系统的业务逻辑组件,业务逻辑组件则调用系统的DAO组件,而DAO组件则依赖于SessionFactory和DataSource等底层组件实现数据库访问。
从系统实现角度看,Ioc容器先创建SessionFactory和DataSource等底层组件,然后将这些底层组件注入给DAO组件,提供一个完整的DAO组件,并将此DAO组件注入给业务逻辑组件,从而提供一个完整的业务逻辑组件,而业务逻辑组件又被注入给控制器组件,控制器组件负责拦截用户请求,并将处理结果呈现给用户——这一系列的衔接,都由Spring的IoC容器提供实现。
简单来说,Spring通过IoC容器上管(Struts2)Action的创建并依赖注入给控制器,下管(Hibernate)SessionFactory的创建并依赖注入给DAO组件,是一个巨大的工厂。(个人理解)
源代码下载:Regist_S2SH.rar
1.项目结构:
2.web.xml配置文件:
- 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">
- <welcome-file-list>
- <welcome-file>index.jspwelcome-file>
- welcome-file-list>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
3.applicationContext.xml配置
- xml version="1.0" encoding="GBK"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"/>
- <property name="user" value="root"/>
- <property name="password" value="1120"/>
- <property name="maxPoolSize" value="40"/>
- <property name="minPoolSize" value="1"/>
- <property name="initialPoolSize" value="1"/>
- <property name="maxIdleTime" value="20"/>
- bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>com/xqh/model/Person.hbm.xmlvalue>
- list>
- property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
- hibernate.hbm2ddl.auto=update
- hibernate.show_sql=true
- hibernate.format_sql=true;
- value>
- property>
- bean>
- <bean id="personDao" class=
- "com.xqh.dao.impl.PersonDaoHibernate">
- <property name="sessionFactory" ref="sessionFactory"/>
- bean>
- <bean id="service" class=
- "com.xqh.service.impl.ServiceImpl">
- <property name="personDao" ref="personDao"/>
- bean>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true"/>
- <tx:method name="*"/>
- tx:attributes>
- tx:advice>
- <aop:config>
- <aop:pointcut id="pointcut"
- expression="bean(service)"/>
- <aop:advisor advice-ref="txAdvice"
- pointcut-ref="pointcut"/>
- aop:config>
- beans>
4.struts.xml配置:
- xml version="1.0" encoding="GBK"?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <constant name="struts.i18n.encoding" value="GBK"/>
- <constant name="struts.devMode" value="true"/>
- <package name="xqh" extends="struts-default">
- <action name="registPro"
- class="com.xqh.action.RegistAction">
- <result name="error">/error.jspresult>
- <result name="success">/welcome.jspresult>
- action>
- <action name="*">
- <result>/{1}.jspresult>
- action>
- package>
- struts>
5.RegistAction.java代码清单:
- package com.xqh.action;
- import com.opensymphony.xwork2.Action;
- import com.xqh.model.Person;
- import com.xqh.service.Service;
- public class RegistAction
- implements Action
- {
- //下面是用于封装用户请求参数的属性
- private Person person;
- //用于封装处理结果的属性
- private String tip;
- //系统所用的业务逻辑组件
- private Service service;
- //设置注入业务逻辑组件所必需的setter方法
- public void setService(Service service)
- {
- this.service = service;
- }
- //person属性的setter和getter方法
- public void setPerson(Person person)
- {
- this.person = person;
- }
- public Person getPerson()
- {
- return this.person;
- }
- //tip属性的setter和getter方法
- public void setTip(String tip)
- {
- this.tip = tip;
- }
- public String getTip()
- {
- return this.tip;
- }
- //处理用户请求的execute方法
- public String execute()
- throws Exception
- {
- //调用业务逻辑组件的regist方法来处理请求
- if (service.regist(person))
- {
- setTip("哈哈,注册成功!");
- return SUCCESS;
- }
- else
- {
- return ERROR;
- }
- }
- }
6.personDao.java 代码清单:
- package com.xqh.dao;
- import java.util.List;
- import com.xqh.model.Person;
- public interface PersonDao
- {
- /**
- * 加载Person实例
- * @param id 需要加载的Person实例的主键值
- * @return 返回加载的Person实例
- */
- Person get(Integer id);
- /**
- * 保存Person实例
- * @param person 需要保存的Person实例
- * @return 刚刚保存的Person实例的标识属性值
- */
- Integer save(Person person);
- /**
- * 修改Person实例
- * @param person 需要修改的Person实例
- */
- void update(Person person);
- /**
- * 删除Person实例
- * @param id 需要删除的Person实例的标识属性值
- */
- void delete(Integer id);
- /**
- * 删除Person实例
- * @param person 需要删除的Person实例
- */
- void delete(Person person);
- /**
- * 根据用户名查找Person
- * @param name 查询的人名
- * @return 指定用户名对应的全部Person
- */
- List
findByName(String name); - /**
- * 查询全部Person实例
- * @return 全部Person实例
- */
- public List
findAllPerson(); - }
7.personDaoHibernate.java代码清单:
- package com.xqh.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.xqh.dao.PersonDao;
- import com.xqh.model.Person;
- import java.util.*;
- public class PersonDaoHibernate
- extends HibernateDaoSupport implements PersonDao
- {
- /**
- * 加载Person实例
- * @param id 需要加载的Person实例的标识属性值
- * @return 指定id对应的Person实例
- */
- public Person get(Integer id)
- {
- return getHibernateTemplate()
- .get(Person.class, id);
- }
- /**
- * 保存Person实例
- * @param person 需要保存的Person实例
- * @return 刚刚保存的Person实例的标识属性值
- */
- public Integer save(Person person)
- {
- return (Integer)getHibernateTemplate()
- .save(person);
- }
- /**
- * 修改Person实例
- * @param person 需要修改的Person实例
- */
- public void update(Person person)
- {
- getHibernateTemplate().update(person);
- }
- /**
- * 删除Person实例
- * @param id 需要删除的Person实例的标识属性值
- */
- public void delete(Integer id)
- {
- getHibernateTemplate().delete(get(id));
- }
- /**
- * 删除Person实例
- * @param person 需要删除的Person实例
- */
- public void delete(Person person)
- {
- getHibernateTemplate().delete(person);
- }
- /**
- * 根据用户名查找Person
- * @param name 查询的人名
- * @return 指定用户名对应的全部Person
- */
- @SuppressWarnings("unchecked")
- public List
findByName(String name) - {
- return (List
)getHibernateTemplate() - .find("from Person p where p.name like ?" , name);
- }
- /**
- * 查询全部Person实例
- * @return 全部Person实例
- */
- @SuppressWarnings("unchecked")
- public List
findAllPerson() - {
- return (List
)getHibernateTemplate() - .find("from Person");
- }
- }
8.Person.java代码清单:
- package com.xqh.model;
- public class Person
- {
- private Integer id;
- private String name;
- private int age;
- //无参数的构造器
- public Person()
- {
- }
- //初始化全部属性的构造器
- public Person(String name , int age)
- {
- this.name = name;
- this.age = age;
- }
- //id属性的setter和getter方法
- public void setId(Integer id)
- {
- this.id = id;
- }
- public Integer getId()
- {
- return this.id;
- }
- //name属性的setter和getter方法
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return this.name;
- } //age属性的setter和getter方法
- public void setAge(int age)
- {
- this.age = age;
- }
- public int getAge()
- {
- return this.age;
- }
- }
9.Person.hbm.xml配置:
- xml version="1.0" encoding="GBK"?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.xqh.model">
- <class name="Person" table="tb_person">
- <id name="id" column="Id"
- type="int">
- <generator class="increment"/>
- id>
- <property name="name" type="string"/>
- <property name="age" type="int"/>
- class>
- hibernate-mapping>
10.Service.java代码清单:
- package com.xqh.service;
- import com.xqh.model.Person;
- public interface Service
- {
- //注册用户
- boolean regist(Person person);
- }
11.ServiceImpl.java代码清单:
- package com.xqh.service.impl;
- import com.xqh.dao.PersonDao;
- import com.xqh.model.Person;
- import com.xqh.service.Service;
- public class ServiceImpl
- implements Service
- {
- private PersonDao personDao;
- //依赖注入DAO组件所需的setter方法
- public void setPersonDao(PersonDao personDao)
- {
- this.personDao = personDao;
- }
- //注册用户
- public boolean regist(Person person)
- {
- //调用DAO组件的方法来实现业务逻辑
- int result = personDao.save(person);
- if (result > 0)
- {
- return true;
- }
- return false;
- }
- }