您的位置:首页 > 其它

三大框架整合所需要注意的问题

2016-03-14 20:53 363 查看
对于一个学习web开发的人来说,三大框架是必不可少需要学习的,所以三大框架的整合也是要经历的,这几天我在整合三大框架时所到的问题,让我对整合有了一些理解,在这里给大家分享一下,希望对大家的学习有所帮助。

首先很重要的一点就是要先加如三大框架所需要的jar包,这些大家如网上下载就可以。我今天主要说的是配置。

第一,对于hibernate来说,hibernate是对底层数据库所进行的操作,虽然hibernate可以自主生成表但是我们不建议这样做,一般都是首先建好表,对于hibernate的配置首先最重要的就是po类和表的映射文件,这些都可以利用开发工具的反向工程生成,反向工程大家有兴趣可以学习一下,网上都有资源,下一步就是配置hibernate的配置文件,里面需要配置你的数据库驱动,用户名密码,连接数据库的一些配置,加载所生成表的映射文件<hibernate-configuration>

<session-factory>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/shop

</property>

<property name="connection.username">root</property>

<property name="connection.password">******</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<!-- 开启hibenate二级缓存 缺省是真

<property name="hibernate.cache.use_second_level_cache">

true

</property>

<property name="hibernate.cache.provider_class">

org.hibernate.cache.EhCacheProvider

</property>

-->

<property name="show_sql">true</property>

<property name="myeclipse.connection.profile">mySQL</property>

下一步就是配置spring的配置文件,需要配置数据源和一些service服务,并在web.xml里面加载spring的配置文件

加载action

<bean id="baseAction" class="com.oracle.match.action.BaseAction"

scope="prototype">

<property name="userService" ref="userService" />

</bean>

<bean id="userAction" class="com.oracle.match.action.UserAction" parent="baseAction"

scope="prototype" />

</beans>

加载bean,配置事务

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 加载hibernate的配置文件 -->

<property name="configLocation" value="classpath:hibernate.cfg.xml" />

</bean>

<!-- 2 -->

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

aop <!-- 需要sessionFactory -->

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 3 -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!--

此类不能直接实例化,因为此类的构造方法要通过子类调用, 获取子类的相关信息

-->

<bean id="baseService" class="com.oracle.match.service.impl.BaseServiceImpl" lazy-init="true">

<property name="hibernateTemplate" ref="hibernateTemplate" />

</bean>

<!-- parent 在Spring配置文件中 找父类, 父类通过Spring的方式创建, 这样父类就可以实现依赖注入 -->

<bean id="userService" class="com.oracle.match.service.impl.UserServiceImpl" parent="baseService" />

aop配置

<tx:advice id="advice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="save*" propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="*" propagation="NEVER" read-only="true"/>

</tx:attributes>

</tx:advice>

<!-- 5: 切入表达式: 配置运行的时候*包的*类切入通知 -->

<aop:config>

<aop:pointcut expression="execution(* com.oracle.match.service.impl.*.*(..))" id="pointcut"/>

<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>

</aop:config>

以上就是spring的基本配置

最后就是Struts的配置,主要是控制action的所跳转的页面,这些大家可以去网上学习一下,自己配置,比较简单。

最后大家不要忘了在web.xml里配置监听器,加载spring配置文件啊

<welcome-file-list>

<welcome-file>

login.jsp

</welcome-file>

</welcome-file-list>

<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>*.action</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>*.jsp</url-pattern>

</filter-mapping>

<filter>

<filter-name>openSessionInView</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

<init-param>

<param-name>flushMode</param-name>

<param-value>AUTO</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>openSessionInView</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext-*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

希望对大家有所帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: