整合时建议一步步来,把每一个框架都做通,然后在处理整合的问题整合环境是struts2.2和hibernate4.2和spring4.2,jdk1.6
第一步:列举需要的jar包:
hibernate4.2jar包:
c3p0数据库jar包,当然也可以是使用spring提供的一个c3p0包:
数据库驱动,这里采用mysql数据库:mysql-connector-java-5.1.28-bin.jar
struts2jar包,注意有重复的jar,要删除,javassist,一般保留版本高的:
整合包spring和struts2:struts2-spring-plugin-2.3.28
做分布整合时,建议先不要导入,不然struts会无法起作用
spring4.2包:
aop联盟:
最后要注意的是如果使用注解开发,struts需要导入注解包:
第二步:各个层次的代码接口就不在写了:
po类:
public class User {
private Integer id;
private String name;
private Double money;
sett..
getter..
}
hibernate映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.leige.domain">
<class name="User" table="t_user">
<id name="id" type="int" column="id">
<generator class="native"></generator>
</id>
<property name="name" type="string" length="20" column="name"/>
<property name="money" type="double" column="money"/>
</class>
</hibernate-mapping>
dao层:
package com.leige.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.leige.dao.UserDao;
import com.leige.domain.User;
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void addUser(User user) {
//保存对象
hibernateTemplate.save(user);
}
@Override
public void update(User user) {
this.hibernateTemplate.update(user);
}
@Override
public User findById(Integer id) {
// TODO Auto-generated method stub
return this.hibernateTemplate.get(User.class, id);
}
}
service层:
package com.leige.service.impl;
import javax.ejb.FinderException;
import com.leige.dao.UserDao;
import com.leige.dao.impl.UserDaoImpl;
import com.leige.domain.User;
import com.leige.service.UserService;
public class UserServiceImpl implements UserService {
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* 实现注册业务*/
@Override
public void regist(User form) {
userDao.addUser(form);
}
//实现转账业务
@Override
public void transfer(Integer from, Integer to, Double money) {
/*调用dao层实现转账业务:
*
*
* */
User fr=userDao.findById(from);
fr.setMoney(fr.getMoney()-money);
userDao.update(fr);
User tt=userDao.findById(to);
//int i=5/0;
tt.setMoney(tt.getMoney()+money);
userDao.update(tt);
}
}
action层:
package com.leige.web.action;
import com.leige.domain.User;
import com.leige.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/* service由spring注入*/
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
private User user;
//转账业务所需参数
private Integer userTo;
private Integer userFrom;
private Double money;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getUserTo() {
return userTo;
}
public void setUserTo(Integer userTo) {
this.userTo = userTo;
}
public Integer getUserFrom() {
return userFrom;
}
public void setUserFrom(Integer userFrom) {
this.userFrom = userFrom;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String add(){
userService.regist(user);
return "success";
}
public String pay(){
/*System.out.println("from----"+userFrom);
System.out.println("to----"+userTo);
System.out.println("money----"+money);*/
/* 调用service层实现转账*/
userService.transfer(userFrom, userTo, money);
return "success";
}
}
第三步各个层次的配置文件配置,详细解释都在配置文件中:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
<span style="white-space:pre"> </span>xmlns="http://java.sun.com/xml/ns/javaee"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
<span style="white-space:pre"> </span>http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>SSh</display-name>
<!-- 加载spring的配置文件:默认是在WEB-INF下名称为applicationContext.xml,
如果我们放在其他位置,需要配置contextparam参数
-->
<context-param>
<span style="white-space:pre"> </span><param-name>contextConfigLocation</param-name>
<span style="white-space:pre"> </span><param-value>classpath:applicationContext.xml</param-value>
<span style="white-space:pre"> </span></context-param>
<span style="white-space:pre"> </span><!-- spring监听器,加载xml文件 -->
<span style="white-space:pre"> </span><listener>
<span style="white-space:pre"> </span><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<span style="white-space:pre"> </span></listener>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> <!-- -struts2的拦截器的类全名称很重要,一定要配置 -->
<filter>
<filter-name>Struts2.0</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2.0</filter-name>
<!-- -过滤路径-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<span style="white-space:pre"> </span>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts2.的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 低版本的struts2需声明将action交给spring管理 -->
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<!-- 这里的useractioin交给spring管理,需要注意的是,名称要写的和spring中的一致
,另外action是每个线程一个action,所以需要把作用域设置为prototype
如果action在spring中配置完毕后,需要注意的是在struts中需要是使用spring中的action的id作为struts的class属性,
另外整合之后action中属性注入,即service注入默认是按照属性id装配的,
采用struts配置action时,service的属性名称一定要和spring中id相同
-->
<action name="login_*" class="com.leige.web.action.UserAction" method="{1}">
<result name="success">/show.jsp</result>
</action>
</package>
</struts>
hibernate配置这一步,我们可以将在spring配置文件中配置也可以在hibernate中配置:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="connection.driver_class"></property>
<!-- 配置连接参数 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.isolation">4</property>
<!-- 配置c3p0连接池 -->
<property name="c3p0.max_size">10</property>
<property name="c3p0.min_size">3</property>
<property name="c3p0.timeout">4000</property>
<property name="c3p0.acquire_increment">3</property>
<property name="c3p0.max_statements">100</property>
<!-- 绑定session,这一步我也不知道为什么出错,可以高版本已经默认开启事务线程了吧,已配置就出错,所以注掉,希望有大神来告诉我 -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- 添加映射 -->
<mapping resource="com/leige/domain/User.hbm.xml"/>
<!-- 这里就不配置二级缓存了 -->
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 0; 整合action层和spring将action交给spring管理
但是别忘记将action的作用于配置为prototype,表示每次请求都是单独的action
这一步可以选择,可以将action在spring的配置文件中配置也可以在struts中配置
,如果在spring中配置完毕后,需要注意的是在struts中需要是使用spring的总的id作为struts的class属性,另外如果struts版本较低的话还需要声明将action交给spring管理
另外整合之后action中属性注入,即service注入默认是按照属性id装配的,采用struts配置action时,service的属性名称一定要和spring中id相同
<bean id="userAction" class="com.leige.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> -->
<!-- 1: 将service层的对象交给spring管理,并注入dao层对象 -->
<bean id="userService" class="com.leige.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!--2: 配置dao层 ,注入hibernateTemplate-->
<bean id="userDao" class="com.leige.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 3:配置jdbc模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4:配置sessionfactory ,加载hibernate的配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--
5:配置事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 6: 配置事务详情,匹配事务方法 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="regist" />
<tx:method name="transfer" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 ,保证service方法在一个事务内执行 -->
<aop:config >
<aop:pointcut expression="execution(* com.leige.service..*.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut" />
</aop:config>
</beans>
业务表单:
<form action="login_add" method="post">
姓名:<input type="text" name="user.name"><br/>
金钱: <input type="text" name="user.money"><br/>
<input type="submit" value="提交"><br/>
</form>
<form action="login_pay" method="post">
转账人:<input type="text" name="userFrom"><br/>
收账人:<input type="text" name="userTo"><br/>
金钱: <input type="text" name="money"><br/>
<input type="submit" value="提交"><br/>
</form>
hibernate配置在spring中配置的方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 0; 整合action层和spring将action交给spring管理
但是别忘记将action的作用于配置为prototype,表示每次请求都是单独的action
这一步可以选择,可以将action在spring的配置文件中配置也可以在struts中配置
,如果在spring中配置完毕后,需要注意的是在struts中需要是使用spring的总的id作为struts的class属性,另外如果struts版本较低的话还需要声明将action交给spring管理
另外整合之后action中属性注入,即service注入默认是按照属性id装配的,采用struts配置action时,service的属性名称一定要和spring中id相同
<bean id="userAction" class="com.leige.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> -->
<!-- 1: 将service层的对象交给spring管理,并注入dao层对象 -->
<bean id="userService" class="com.leige.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 2:加载properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.1配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--3: 配置dao层 ,注入sessionFactory,且dao层实现类必须继承HibernateDaoSupport类-->
<bean id="userDao" class="com.leige.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4:配置sessionfactory ,加载hibernate的配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 采用spring中配置hibernate配置时,需要这么配置 ,
配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate特有配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<!-- 配置映射文件
mappingResources : 加载hbm映射文件(资源)
<property name="mappingResources" value="com/leige/domain/User.hbm.xml"></property>
mappingLocations : 加载映射文件(位置),建议位置都以"classpath:"开头
支持通配符*
<property name="mappingLocations" value="classpath:com/leige/domain/*.hbm.xml"></property>
mappingDirectoryLocations : 映射文件目录位置
<property name="mappingDirectoryLocations" value="classpath:com/leigedomain"></property>
<property name="mappingDirectoryLocations" value="classpath:com/leige/*"></property>
<property name="mappingDirectoryLocations" value="classpath:com/leige/crm/*/domain"></property>
mappingJarLocations : 映射文件存在jar包
classpath:com/leige/domain
-->
<property name="mappingLocations" value="classpath:com/leige/domain/*.hbm.xml"></property>
</bean>
<!--
5:配置事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 6: 配置事务详情,匹配事务方法 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="regist" />
<tx:method name="transfer" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 ,保证service方法在一个事务内执行 -->
<aop:config >
<aop:pointcut expression="execution(* com.leige.service..*.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut" />
</aop:config>
</beans>
dao层继承
HibernateDaoSupport
package com.leige.dao.impl;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.leige.dao.UserDao;
import com.leige.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
//保存对象
super.getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
super.getHibernateTemplate().update(user);
}
@Override
public User findById(Integer id) {
return super.getHibernateTemplate().get(User.class, id);
}
}