一、SSH
Struts2,Spring和Hibernate在项目中的简单集成过程
二、过程
1.从Dao层开始,首先集成Hibernate实现数据库层的访问
配置文件hibernate.cfg.xml文件,默认会在src目录下寻找该文件并加载
配置内容,以SessionFactory为根节点,如下
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--配置hibernate管理的实体类-->
<mapping class="com.huimin.model.User"/>
</session-factory>
核心是数据库的连接,实体类的配置,其他仅供参考,User实体使用hibernate注解,映射到数据库
@Entity
public class User {
private String username;
private String password;
private int id;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建SessionFactory的实例(单例)
public class HibernateUtil {
private static SessionFactory sf;
static{
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static SessionFactory getSf(){
return sf;
}
}
Dao实现层实现,SessionFactory会有当前的Session,通过getCurrentSession得到,然后通过事务管理
SessionFactory sf = HibernateUtil.getSf();
Session s = sf.getCurrentSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
根据不同的需求,实现dao层对数据库的增删改查
2.配置Spring,并把hibernate交给spring管理,xmlns命名空间必不可少
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--启用注解-->
<context:annotation-config />
<!--启动扫描-->
<context:component-scan base-package="com.huimin" />
<!--配置properties文件-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!--配置数据源-->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置hibernate3的sesionFactory的annotation注解-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource" />
<!--扫描实体-->
<property name="packagesToScan">
<list>
<value>com.huimin.model</value>
</list>
</property>
<!--声明断言-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--注入hibernateTemplate,在SessionFactory上的进一步封装-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--对于sessionFactory进行事务的管理-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--管理层次在Service层(定义切面)-->
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.huimin.service..*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<!--设置事务的建议,及具体方法的权限-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
dao层对于hibernate的具体实现就可以变为如下
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User user) {
hibernateTemplate.save(user);
// SessionFactory sf = HibernateUtil.getSf();
// Session s = sf.getCurrentSession();
// s.beginTransaction();
// s.save(user);
// s.getTransaction().commit();
}
最后在web.xml加入spring的监听,及配置文件所在路径
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!--default WEB-INF/applicationContext.xml-->
</listener>
<!--指定xml所在路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
3.Struts2引入
Struts2要想被Spring管理起来需要引入struts2-spring-plugin.jar来生成bean,并被spring管理起来
首先在web.xml中配置filter
<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>
然后创建action继承自ActionSupport,并重写execute方法即可
配置Struts2的配置文件
<struts>
<!--指定action名字,和返回结果页面-->
<package name="register" extends="struts-default">
<action name="user" class="com.huimin.action.UserAction">
<result name="success">/pages/registerSuccess.jsp</result>
<result name="fail">/pages/registerFail.jsp</result>
<result name="userlist">/pages/userList.jsp</result>
<result name="load">/pages/load.jsp</result>
</action>
</package>
</struts>
这里有细节需要注意一下,在spring管理struts2的时,action中的所有属性都会默认按照名字自动创建bean,并且在有set,get
方法时我们指定的bean是不起作用的,要想我们手动指定特定名称的bean需要把set,get方法去掉,并且在属性上@Resource
指定我们的bean
到此为止,Struts2,Spring和Hibernate就集成到一块可以开始工作了
三、小结
Struts2用到的jar包, Spring2.5.6用到的jar, Hibernate3用到的jar