Hiberbate是SSH框架中的“H”,是SSH中的持久层,是把数据库中的表映射成一个类。操作数据库时,就只需要对类进行操作。
先总结在applicationContext.xml中配置HibernateTemplate。
1.在Spring的配置文件applicationContext.xml中,配置数据库信息:
<!--获取jdbc配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
2.在Spring的配置文件applicationContext.xml中绑定数据源,配置HibernateTemplate:
<!--配置数据库连接 -->
<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>
<!-- #最大连接数据库连接数,设 0 为没有限制 -->
<property name="maxActive">
<value>50</value>
</property>
<!-- #最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止。设 0 为没有限制 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 最大等待毫秒数, 单位为 ms, 超过时间会出错误信息 -->
<property name="maxWait">
<value>1000</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>xxx.xxx.xxx.bean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>xxx.xxx.xxx.bean.className</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
3.在Spring的配置文件applicationContext.xml 中配置事务管理
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="tansactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事务 -->
<!--
定义哪些方法参与事务
expression="execution(* com.tree.*.service.*.*(..))
第一个*代表 返回值类型是所有。
com.tree.*.service 代表在这个包下的类,第二个*代表中间模糊匹配。
第三个* 代表类的名称是所有类。
第四个* 代表类中的方法名称。
(..)代表参数不限制。
-->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.tree.*.service.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
基本hibernate 在applicationContext.xml的配置就完成了!
PS:如果mysql实现了主从同步,一个服务器上的数据库专门用于读,另一个服务器的数据库用于写,jdbc中可以先配置好不同的读写数据库地址。然后在applicationContext.xml 中配置(下面是write配置,read配置类似,其实就是声明两个bean):
<!--配置数据库连接 -->
<bean id="writeDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${write.jdbc.url}"></property>
<property name="username" value="${write.jdbc.username}"></property>
<property name="password" value="${write.jdbc.password}"></property>
<!-- #最大连接数据库连接数,设 0 为没有限制 -->
<property name="maxActive">
<value>50</value>
</property>
<!-- #最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止。设 0 为没有限制 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 最大等待毫秒数, 单位为 ms, 超过时间会出错误信息 -->
<property name="maxWait">
<value>1000</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
</bean>
<bean id="writeSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="writeDataSource" />
</property>
<property name="packagesToScan">
<list>
<value>xxx.xxx.xxx.bean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>xxx.xxx.xxx.bean.className</value>
</list>
</property>
</bean>
<bean id="writeHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="writeSessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="writeTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="writeSessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="writeTxAdvice" transaction-manager="writeTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事务 -->
<!--
定义哪些方法参与事务
expression="execution(* com.tree.*.service.*.*(..))
第一个*代表 返回值类型是所有。
com.tree.*.service 代表在这个包下的类,第二个*代表中间模糊匹配。
第三个* 代表类的名称是所有类。
第四个* 代表类中的方法名称。
(..)代表参数不限制。
-->
<aop:config>
<aop:pointcut id="writeAllManagerMethod" expression="execution(* com.shundr.database.dao.*.*(..))"/>
<aop:advisor pointcut-ref="writeAllManagerMethod" advice-ref="writeTxAdvice"/>
</aop:config>
4.在Dao层(即操作数据库的一层)调用HibernateTemplate
@Resource
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}