Spring+Hibernate整合配置
分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报
springhibernateclassactionservletmysql
在公司一直没有什么机会直接折腾SSH“原生态”的SSH当今比较流行的轻量级的框架,用着公司的框架也是郁闷异常,今天没事整整原来用过的一个项目的配置,发现就算是自己曾经用过的东西,如果较长时间不返过去重新学习,许多你半熟不熟的知识就是异常陌生。下面贴上我的一些配置,暂且权当备份吧。
web.xml
作为Java web项目最为重要的配置文件,下面我们看看我的一些相关配置。
<!-- 配置Spring上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</context-param>
<!-- 启动 Spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置 OpenSessionInView -->
<filter>
<filter-name>hibernateOpenSessionInView</filter-name>
<filter-class>com.geek.core.dao.OpenSessionInView</filter-class>
<init-param>
<param-name>excludeSuffixs</param-name>
<param-value>js,css,jpg,gif,jpeg,mp3</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateOpenSessionInView</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 配置 Spring Servlet 分发器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-action.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Spring的相关配置文件,Hibernate也共用了Spring的配置文件
spring-action.xml
<!-- Web层Action类扫描 -->
<context:component-scan base-package="com.geek.core.web.action" />
<!-- Action类注解配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<!-- 视图解析器配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
</bean>
spring-persistence.xml
<!-- 配置注解注入 -->
<context:annotation-config />
<!-- 持久层类所在包扫描-->
<context:component-scan base-package="com.geek.core.dao.impl" />
spring-service.xml
<!-- 配置 Service 类扫描包 -->
<context:component-scan base-package="com.geek.core.service.impl" />
spring.xml [Hibernate共用]
<!-- MySQL 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"value="com.mysql.jdbc.Driver"></property>
<property name="url"value="jdbc:mysql://127.0.0.1:3306/smile"></property>
<property name="username" value="smile"></property>
<property name="password" value="smile"></property>
</bean>
<!-- Session 工厂配置 -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"></ref></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- ORM [注解] 配置实体包 -->
<property name="packagesToScan">
<list>
<value>com.geek.core.pojo</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用注解配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
注:在配置过程中也出现了相关的错误,组要是schema引入的问题,如context和tx的引入,所以需多加注意。
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
…………
:)