不留hibernate的配置文件
<beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--Spring已经提供好编写HIbernate连接数据库信息的属性了-->
<property name="hibernateProperties">
<!--配置Hibernate的数据库信息-->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.driver_class">com.MySQL.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql:///myaccount</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">root</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--指定Hibernate的具体的有注解的实体类-->
<property name="packagesToScan">
<list>
<value>com.sram.entity</value>
</list>
</property>
</ bean >留下hibernate的配置文件 也就是在spring中的部分配置信息 写在 hibernate配置文件中
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.sram.entity.Account"/>
</session-factory>
</ hibernate-configuration >
三大框架整合的时候
首先在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">
<display-name></display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans-*.xml</param-value>
</context-param>
<!-- 调用Spring实现的过滤器实现字符编码集的设置 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后是spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.zr"></context:component-scan>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="${jdbc_driver}"></property>
<property name="jdbcUrl" value="${jdbc_url}"></property>
<property name="user" value="${jdbc_user}"></property>
<property name="password" value="${jdbc_psw}"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="hibernate.cfg.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.zr.service.impl.*.*(..) )" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
hibernate的配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.zr.entity.Account"/><!-- 这里还需要配置实体类 -->
</session-factory>
</hibernate-configuration>
Struts2的配置文件
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 配置struts请求的后缀,符合条件的可以被struts识别。 -->
<constant name="struts.action.extension" value="html,action,," />
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 此处设置将Struts中的对象的创建交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="default" namespace="/" extends="struts-default">
<!-- 由于Action对象的创建已经交给Spring处理,所以,
此处的class属性不再指向action的完整路径,
而是指向由spring创建的对象 -->
<action name="*_*" class="{1}Action" method="{2}">
<result name="main">/frame/main.jsp</result>
<result name="error">/login.jsp</result>
<result name="{2}">/WEB-INF/jsp/{1}/{2}.jsp</result>
</action>
</package>
注意 在整合中 action中调用service的时候 虽然说 注解在serviceImpl上加的 但是不能用service的实现类来接受 必须是接口来接收 spring创建的实现类
在没有进行事务的配置的时候不能用 sessionFactory.getCurrentSession来创建session
本文介绍如何在Spring框架中整合Hibernate与Struts2,包括配置数据库连接、定义会话工厂、设置事务管理器等内容,并展示了具体的XML配置示例。
649

被折叠的 条评论
为什么被折叠?



