ssh整合:spring与hibernate的整合

时间:2021-10-22 15:04:31

ssh的整合主要是spring与hibernate +  spring与struts的整合,struts的逻辑中使用业务层的对象只需使用spring的ioc得到需要的依赖对象。

一、spring与hibernate

spring与hibernate的整合有2种方式,一种是把hibernate的配置放在spring的配置中,一种是独立在spring外

hibernate独立在外:

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"
	   xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   	<property name="configLocations">
   		<value>classpath:hibernate.cfg.xml</value>
   	</property>
  </bean>
 </bean>  </beans>

使用注解:

<!-- sessionFactory定义 -->
 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!--使用注解-->
  <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  <property name="packagesToScan">
   <list>
    <value>cn.jiabeis.s2shstudy.model</value>
   </list>
  </property>


 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">***</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">10</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/model/User.hbm.xml"/>
<!--        <mapping class="com.model.User"/>-->

    </session-factory>

</hibernate-configuration>

hibernate的配置整合在spring中:

<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource"
   destroy-method="close">
   <property name="driverClassName" value="${jdbc.driverClassName}"></property>
   <property name="url" value="${jdbc.url}"></property>
   <property name="username" value="${jdbc.username}"></property>
   <property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置 hibernate sessionFactory -->
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">         <!--可使用proxool等来作为数据源-->
    <ref bean="dataSource" />
   </property>
   <property name="mappingDirectoryLocations">    <!--可使用mappingResources直接写出配置文件的位置-->
    <list>
     <value>classpath:/config/hbm/</value>
    </list>
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.connection.release_mode">auto</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="connection.useUnicode">true</prop>
     <prop key="connection.characterEncoding">gbk</prop>
<!--出现中文乱码的问题 -->
     <prop key="hibernate.query.factory_class">
     org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
    </props>
   </property>
</bean>

数据源的配置还可通过属性文件来获得:

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName" value="${jdbc.driverClassName}" />
       <property name="url" value="${jdbc.url}" />
       <property name="username" value="${jdbc.username}" />
       <property name="password" value="${jdbc.password}" />
    </bean>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc/:mysql/://localhost/:3306/spring
jdbc.username=root
jdbc.password=31415926

通过Spring提供的HibernateTemplate操作数据库:

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
       <property name="sessionFactory" ref="sessionFactory"></property>
</bean>


事务管理:
<!-- 事务处理 --> 
<bean id ="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name ="sessionFactory"> 
            <ref local ="sessionFactory" /> 
        </property > 
    </bean > 
    <!-- 自动创建事务代理 --> 
<bean id ="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
        <property name ="transactionManager" ref ="transactionManager" /> 
        <property name ="transactionAttributes"> 
            <props > 
                <prop key ="delete*"> PROPAGATION_REQUIRED</prop > 
                <prop key ="add*"> PROPAGATION_REQUIRED</prop > 
                <prop key ="update*"> PROPAGATION_REQUIRED</prop > 
                <prop key ="save*"> PROPAGATION_REQUIRED</prop > 
                <prop key ="find*"> PROPAGATION_REQUIRED,readOnly</prop > 
                <prop key ="*"> PROPAGATION_REQUIRED</prop > 
            </props > 
        </property > 
</bean >


使用HibernateDaoSupport类:
@Component("logDAO") 
public class LogDAOImpl extends HibernateDaoSupport implements LogDAO {
	public void save(Log log) {
		this.getHibernateTemplate().save(log);
	}
}

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  	<property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="logDao" class="com.zchen.spring.LogDaoImpl">
  	<property name="hibernateTemplate" ref="hibernateTemplate"></property>
 </bean>