上面一篇文章讲到了集成hibernate4,主要是就如何在spring3的环境下如何配置的问题,忽然发现,hibernate的映射文件在sessionFactory的配置内,也和hibernateProperties的配置在一块儿,sessionFactory是hibernate的核心配置,开发人员不该该也不必总去看到这个文件,而开发人员又必须添加不少的entity的映射文件。在这个操做中,若是有任何的误操做,损失仍是满大的,与是如何把常常会频繁操做的hibernate-mapping内容从SessionFactory的配置中分离出去就成了一个良好的框架设计的当务之急的选择。因而动手,其实很简单,在spring的ioc环境下,这一些都变的是那么的优雅。spring
首先:编写IMapping接口:sql
- public interface IMapping
- {
- public String[] getMappingResources();
- }
而后写实现:session
- public class HibernateMappingImpl implements IMapping
- {
- private String[] mappingResources;
- public String[] getMappingResources()
- {
- return mappingResources;
- }
- public void setMappingResources(String[] mappingResources)
- {
- this.mappingResources = mappingResources;
- }
- }
而后编写类去继承LocalSessionFactoryBean:app
- public class XKLocalSessionFactoryBean extends LocalSessionFactoryBean
- {
- private IMapping iMapping;
- public IMapping getiMapping()
- {
- return iMapping;
- }
- public void setiMapping(IMapping iMapping)
- {
- this.iMapping = iMapping;
- super.setMappingResources(iMapping.getMappingResources());
- }
- }
到这你们也许都看明白了。 框架
下面将这些类配置到相关配置中去:ide
sessionFactory的配置改成:学习
- <bean id="sessionFactory" class="com.xk.commons.config.XKLocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="iMapping" ref="hibernateMapping">
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">false</prop>
- <prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>
- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
- </props>
- </property>
- </bean>
对应的已经换成咱们本身实现的com.xk.commons.config.XKLocalSessionFactoryBean, 而原来的 mappingResources 的属性配置,也换成了<property name="iMapping" ref="hibernateMapping"> 引用的是个类,就是咱们本身定义的那个IMapping接口的类,咱们单独的新建一个文件: hibernate-mapping.xml,之后的实体映射的配置,就不用去核心的配置里添加修改了,就都到这个项目级别的文件中去修改,下面看这个类的配置:this
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- 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-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/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <bean id="hibernateMapping" class="com.xk.commons.config.HibernateMappingImpl">
- <property name="mappingResources">
- <list>
- <value>com/xk/model/XkBaseCity.hbm.xml</value>
- <value>com/xk/model/XkBaseProvince.hbm.xml</value>
- <value>com/xk/model/XkSystemDate.hbm.xml</value>
- <value>com/xk/model/XkTrain.hbm.xml</value>
- </list>
- </property>
- </bean>
- </beans>
看看,干净明了,给开发人员培训的时候,能够叫他们忽略掉,那些核心配置的内容,只须要叫他们知道开发的规则,和如何作就ok了,能够大大的下降开发人员的学习成本和技术要求。spa