这次来看看Struts2+spring+hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样。
首先看一下分工吧:
Struts2做的MVC的流程框架,主要完成从客户端访问到选择anction的过程,其中过滤器起到了Controller的作用,action属于model,而jsp则是view页面的展示。
Spring主要利用Ioc的特长来管理各种对象:action,service,dao,数据访问源,Hibernate的核心对象SessionFactory等,还有就是声明式事务的管理等。
而Hibernate框架主要是实体层和数据库中表的映射,以及封装JDBC代码,提供相应的方法,供我们选择。这样从前台页面一直到后台数据库,都有了框架帮我们维护,使我们的开发变得简单快捷,效率大大提高。当然了,前提是我们对这些框架能够灵活的运用。
再者,就是需要三大框架的导入的对应jar包,这里不再列出。以及对应的配置文件,这里需要说明一下,由于Hibernate框架的核心对象SessionFactory交给了Spring框架进行维护,这里就不需要hibernate.cfg.xml配置文件了,我们将这些信息写到Spring核心配置文件中即可。
好,看一下各个配置文件的编写吧!
1,web.xml文件的几个重点项:
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
-
- <!-- 服务器启动时,通过监听器初始化Spring的配置环境
- 监听器,默认加载文件是:/WEB-INF/applicationContext.xml
- -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
-
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
-
- <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>
2,Struts2核心配置文件的编写:
- <package name="example" namespace="/user" extends="struts-default">
- <action name="login" class="loginAction" method="login">
- <result name="success" type="dispatcher">/success.jsp</result>
- <result name="login" type="redirect">/login.jsp</result>
- </action>
- </package>
3,Spring核心配置文件的编写:
- <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-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"
- default-autowire="byName">
-
-
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-
- <bean id="loginAction" class="com.ljh.egov.action.LoginAction" scope="prototype"></bean>
-
- <bean id="userService" class="com.ljh.egov.service.UserService" ></bean>
-
- <bean id="userDao" class="com.ljh.egov.dao.UserDao">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
-
-
- <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:db.properties</value>
- </list>
- </property>
- </bean>
-
-
- <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="user" value="${username}"></property>
- <property name="password" value="${password}"></property>
- <property name="jdbcUrl" value="${url}"></property>
- <property name="driverClass" value="${driverClass}"></property>
- </bean>
-
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name="dataSource" ref="c3p0"></property>
- <property name="hibernateProperties">
- <props>
-
- <prop key="hibernate.show_sql">true</prop>
-
- <prop key="hibernate.format_sql">true</prop>
-
- <prop key="hibernate.hbm2ddl.auto">update</prop>
-
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- </props>
- </property>
-
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:com/ljh/egov/bean</value>
- </list>
- </property>
- </bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="c3p0"></property>
- </bean>
-
-
- <aop:config>
- <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.ljh.egov..service.*.*(..))"/>
- </aop:config>
-
- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
- <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
- <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
- <tx:method name="select*" read-only="true"/>
- </tx:attributes>
- </tx:advice>
-
- </beans>
4,在Dao层的编写中,这里简单说一下如何获得session对象,去进行相关方法的调用,完成我们的数据操作功能。Dao层首先需要实现我们的HibernateDaoSupport,以便使用此父类中的getHibernateTemplate方法,进行相关方法的调用,先看一个查询方法的实现:
-
-
-
-
- return (User)this.getHibernateTemplate().execute(new HibernateCallback(){
-
- public Object doInHibernate(Session session) throws HibernateException,SQLException {
-
-
-
- Query query = session.createQuery("from User u where u.usercode=? and u.userpswd=?");
-
- query.setParameter(0, user.getUsercode());
- query.setParameter(1, user.getUserpswd());
-
- return query.uniqueResult();
- }
- });
- }
对于其它配置文件,这里不再给出,和单自使用框架差不多!当然对于这些配置文件,我们也可以根据实际业务进行划分,是每一个配置文件的功能单一,容量不至于过大,方便我们的管理。
最后简单说一下,三大框架应用中优化的考虑问题吧!
Struts2:
1,发布时需要关闭开发模式(DevMode)
2,不使用用不到的拦截器,拦截器等各种组件越多,性能越低,奔着一个达到目标使用最少的原则,进行相关开发。
3,过滤器的过滤规则尽量不要使用/*,这里我们开发可以制定规则,将过滤器的过滤范围降低到最小,方便框架的快速过滤!
Spring:
1,尽量不要采用自动装配,使用手动装配指明方向,框架能够更快的寻找到相关的类。
2,尽量使用延迟加载功能,这样可以减少和数据库的通信,提高性能。
Hibernate:
1,所有框架使用较新的版本,可以提供更好的性能支持,毕竟什么东西都是向着更好的方向发展。但是也不要一味的追求最新,如果有bug或什么情况还是不好的,奔着一个在有把握的基础上寻求最新的原则。
2,使用合理的缓存策略,主要是二级缓存和查询缓存的选择使用,根据实际情况,合理使用。
3,尽量使用延迟加载功能。
4,推荐使用uuid的主键生成策略,移植性好,支持大数据量。
5,推荐使用乐观锁代替悲观锁。