Hibernate的单独配置和结合Spring的配置:
下面一一展示一下,主要以MySql数据库为例:
Hibernate.cfg.xml
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">810606</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第二种是和Spring的AOP结合
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<description>Spring Quick Start</description>
<!-- datasource -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>810606</value>
</property>
</bean>
<!-- hibernate support -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean"
>
<property name="dataSource">
<ref local="datasource"/>
</property>
<property name="mappingResources">
<list>
<value>test/bcxy/spring/dao/user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<!-- hibernate dao -->
<bean id="hibernatedao"
class="com.bcxy.spring.dao.TestDAO"
>
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>