ITEEDU

Spring集成Hibernate配制详解

配置sessionFactory

配制文件方式:

直接加载Hibernate的配制文件。

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

属性注入方式:

将Hibernate的各种属性通过Spring注入。

<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
	</property>
	<property name="url">
		<value>jdbc:mysql://localhost/hibernate</value>
	</property>
	<property name="username">
		<value>sa</value>
	</property>
	<property name="password">
		<value>sa</value>
	</property>
</bean>
<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource">
		<ref local="dataSource" />
	</property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
			<prop key="hibernate.default_schema">dbo</prop>
			<prop key="hibernate.connection.autocommit">true</prop>
			<prop key="hibernate.show_sql">false</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/iteedu/hibernate/User.hbm.xml</value>
			<value>com/iteedu/hibernate/Role.hbm.xml</value>
		</list>
	</property>
</bean>

关于映射文件的批量加载

在加载映射文件时一个一个添加显然不如让Spring自动加载方便,尤其在映射文件很多时。

LocalSessionFactoryBean提供了如下几种添加方式,有几种可以实现批量加载功能。

看LocalSessionFactoryBean的源码,这几种方式应该是可能混合使用的。

mappingResources

其实mappingResources设置的是LocalSessionFactoryBean中的mappingLocations属性。配制时要添加一个文件路径的列表。在setMappingResources方法中用于创建Resource赋给mappingLocations。

public void setMappingResources(String[] mappingResources) {
    this.mappingLocations = new Resource[mappingResources.length];
    for (int i = 0; i < mappingResources.length; i++) {
        this.mappingLocations[i] = new ClassPathResource(
                mappingResources[i].trim());
    }
}

mappingLocations

配制映射文件位置,可以文件路径也可以是classpath,可以有通配符。

<property name="mappingLocations"
		 value="classpath:com/iteedu/ usermgr/model/*.hbm.xml">

还可以设置为一个列表

<property name="mappingLocations">
	<list>
		<value>classpath:com/iteedu/usermgr/model/*.hbm.xml</value>
		<value>classpath:com/iteedu/hibernate/*.hbm.xml</value>
	</list>
</property>

具体的路径设置可参考Spring加载配制文件详解

public void setMappingLocations(Resource[] mappingLocations) {
    this.mappingLocations = mappingLocations;
}

mappingDirectoryLocations

指定文件目录,加载其中所有映射文件。

public void setMappingDirectoryLocations(Resource[] mappingDirectoryLocations) {
	this.mappingDirectoryLocations = mappingDirectoryLocations;
}

mappingJarLocations

加载jar包中的映射文件。

public void setMappingJarLocations(Resource[] mappingJarLocations) {
	this.mappingJarLocations = mappingJarLocations;
}

cacheableMappingLocations

看源码注释好像是可以加载各种资源的位置,如”/WEB-INF/mapping/example.hbm.xml”。

public void setCacheableMappingLocations(Resource[] cacheableMappingLocations) {
	this.cacheableMappingLocations = cacheableMappingLocations;
}

配置事务管理器

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

配置事务的传播特性

<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="add*" propagation="REQUIRED" />
		<tx:method name="del*" propagation="REQUIRED" />
		<tx:method name="modify*" propagation="REQUIRED" />
		<tx:method name="*" read-only="true" />
	</tx:attributes>
</tx:advice>

声明事务

<aop:config>
	<aop:pointcut id="allManagerMethod"
		expression="execution(* com.iteedu.manager.*.*(..))" />
	<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>