构建S2SH项目后,会发现有诸多的配置文件,hibernate.cfg.xml配置文件完全可以删掉,毕竟dataSource和sessionFactory可以在applicationContext.xml中配置。
以下示例:
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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
applicationContext.xml
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db_driverClassName}"/>
<property name="jdbcUrl" value="${db_path}"/>
<property name="user" value="${db_username}"/>
<property name="password" value="${db_password}"/>
<property name="minPoolSize" value="${db_miniPoolSize}" />
<property name="maxPoolSize" value="${db_maxPoolSize}" />
<property name="initialPoolSize" value="${db_initialPoolSize}" />
<property name="maxIdleTime" value="${db_maxIdleTime}" />
<property name="checkoutTimeout" value="${db_timeout}" />
</bean>
<!--配置hibernateSessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--添加 mapping文件(一个个添加)-->
<!-- <property name="mappingResources">
<list>
<value>hibernate/User.hbm.xml</value>
</list>
</property>-->
<!--添加 mapping文件(指定文件夹)-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:hibernate</value>
</list>
</property>
</bean>
这样配置看起来更加清晰,也更容易理解一点,提高编程效率