在实现类中使用HibernateTemplate 来进行和数据库的连接
第一种:直接在hibernateTemplate 的property配置sessionFactory ,在sessionFactory中指向hibernate配置文件
在applicationContext.xml的部分配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name="configLocations"> <list> <value>hibernate.cfg.xml</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" > <property name="sessionFactory" ref="sessionFactory" ></property> </bean> <bean id="userDao" class="com.gem.hbn.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean>
在hibernate.cfg.xml中的配置
<hibernate-configuration> <session-factory> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.url"> jdbc:mysql://localhost:3306/spring </property> <property name="connection.username">root</property> <property name="connection.password">11111</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="show_sql">true</property> <mapping resource="com/gem/hbn/pojo/User.hbm.xml" /> </session-factory> </hibernate-configuration>
第二种 hibernateTemplate ->sessionFactory ->,sessionFactory 指定dataSource-ref,mappingResources,hibernateProperties
通过属性来指定方言和显示sql语句
hibernateProperties配置
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dialect}</prop>
<prop key="hibernate.show_sql">${show_sql}</prop>
</props>
propertyConfigurer的内容
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
username=root
password=11111
dialect=org.hibernate.dialect.MySQLDialect
show_sql=true
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>source.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${driverClassName}" p:url="${url}" p:username="${username}" p:password="${password}" /> </beans>
第三种 类似第二种,不需要用dataSource来设置数据库信息,直接用hibernateProperties,mappingResources
指定属性和mapping地址
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:mappingResources="com/gem/hbn/xml/User.hbm.xml"> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.driver_class">${driverClassName}</prop> <prop key="hibernate.connection.url">${url}</prop> <prop key="hibernate.connection.username">${username}</prop> <prop key="hibernate.connection.password">${password}</prop> <prop key="hibernate.dialect">${dialect}</prop> <prop key="hibernate.show_sql">${show_sql}</prop> </props> </property> </bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>source.properties</value> </list> </property> </bean>
propertyConfigurer的内容
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
username=root
password=11111
dialect=org.hibernate.dialect.MySQLDialect
show_sql=true