我们知道在做SSH的时候spring有配置文件applicationContext.xml,而hibernate有配置文件hibernate.cfg.xml
那么怎么做可以不再需要hibernate有配置文件hibernate.cfg.xml呢?
以下示例:
applicationContext.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
hibernate.cfg.xml
<!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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:LWF</property>
<property name="hibernate.connection.username">lwf</property>
<property name="hibernate.connection.password">lwf</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">True</property>
<mapping resource="com/ssh/pojo/guestbook/GuestBook.hbm.xml"/>
<mapping resource="com/ssh/pojo/guestbook/Log.hbm.xml"/>
</session-factory>
</hibernate-configuration>
那么下面将两个文件内容合并到applicationContex.xml中去.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/guestbook/GuestBook.hbm.xml</value>
<value>com/ssh/pojo/guestbook/Log.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:LWF"/>
<property name="username" value="lwf"/>
<property name="password" value="lwf"/>
</bean>
好了.现在就可以把hibernate.cfg.xml删除啦