SSH之Spring结合Hibernate如何配置
SSH(Struts、Spring、Hibernate)框架是目前java开发者比较喜欢的框架,搭建SSH开发平台是首要的工作,本例不谈struts,只讲Spring和Hibernate的配置,主要涉及Spring的事务配置,Hibernate配置。
操作方法
- 01
准备SSH项目所需的jar文件,数据库用MySQL.
- 02
为了维护与管理方便,建立一个数据库配置文件dbconfig.propernate(这是目前比较常用的方法) #MySQL jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/shopping?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=roo #hibernate.dialect=org.hibernate.dialect.MySQLDialect #原本方言用的是MySQL默认的上面这个,由于数据库字段含有text类型而自定#义了一个方言 hibernate.dialect=com.bobo.shopping.common.util.BlobMySQLDialect jdbc.initialPoolSize=10 hibernate.show_sql=true hibernate.format_sql=true hibernate.cache.use_query_cache=true hibernate.substitutions=true 1, false 0, yes 'Y', no 'N' hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.jdbc.batch_size=50
- 03
自定义方言代码: package com.bobo.shopping.common.util; import java.sql.Types; import org.hibernate.Hibernate; import org.hibernate.dialect.MySQLDialect; public class BlobMySQLDialect extends MySQLDialect{ public BlobMySQLDialect(){ super(); registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName()); } }
- 04
创建Spring链接数据库配置applicationContext-db.xml文件: 数据库链接用的是c3p0. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- To provide a placeholder, mainly for database configuration --> <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:resource/properties/dbconfig.properties</value> </list> </property> </bean> <!--Database connection pool configuration --> <bean id="sysDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"><value>${jdbc.driverClassName}</value></property> <property name="jdbcUrl"><value>${jdbc.url}</value></property> <property name="user"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> <property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property> </bean> <!-- JDBC start , no transaction--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="sysDataSource" /> </bean> <!-- JDBC end --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="sysDataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> </props> </property> <!-- 按照包路径自动绑定hibernate实体 --> <property name="packagesToScan" value="com.bobo.shopping.app.*.model"></property> </bean> </beans>
- 05
创建Spring事务管理配置文件applicationContext.xml文件: <?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" 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"> <!-- 事务管理器,将委托给HibernateTransactionManager进行管理//--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务处理的AOP配置 所有服务层bean声明都要继承此bean//--> <bean id="TransactionProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <!-- 为了保证服务层统一的事务处理。服务层接口,类的方法必须以下面的方法为开口 --> <!--spring 捕获到RuntimeException和其他一些异常时才会回滚,不是所有异常都会回滚,-Exception 设置 为任何异常都回滚 --> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="up*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="mod*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception </prop> <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="execute*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="do*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <!-- 提供普通java类获取spring上下文 通过上下文获取具体bean,调用其中的方法 --> <bean id="springApplicationContextUtil" class="com.bobo.shopping.common.util.SpringApplicationContextUtil"></bean> <!-- CDB数据源管理 --> <!--<bean id="CDBManager" class="com.bobo.shopping.common.util.CDBManager"></bean> --></beans>
- 06
配置web.xml文件: <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:resource/spring/**/application*.xml </param-value> </context-param> <!--Hibernate 的配置 --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--STRUTS 2 的配置 --> <filter> <filter-name>struts-cleanup</filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!--解决Form提交乱码问题 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
- 07
简单的SSH,Spring和Hibernate配置文件主要就是这几点,具体的struts配置就不提了。有更详细的可以给我留下评论一起交流。