-
<property name="connection.password">admin</property>
-
<property name="hibernate.c3p0.max_size">20</property>
-
<property name="hibernate.c3p0.max_statements">1</property>
-
<property name="hibernate.c3p0.timeout">1800</property>
-
<property name="hibernate.c3p0.max_statements">50</property>
-
<property name="hibernate.dialect">
-
org.hibernate.dialect.Oracle10gDialect
-
</property>
-
<property name="show_sql">true</property>
-
<property name="hibernate.hbm2ddl.auto">update</property>
-
<mapping resource="com/wzm/bean/User.hbm.xml"/>
-
</session-factory>
-
</hibernate-configuration>
Spring的application.xml文件整合Hibernate代码如下:
-
<beans
-
xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
<property name="configLocation" value="classpath:hibernate.cfg.xml">
-
</property>
-
</bean>
-
</beans>
2.2 方法二 废弃使用hibernate.cfg.xml文件
将数据库的配置放在Spring的配置文件application.xml文件中,代码如下:
-
<beans
-
xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-2.5.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
-
<context:component-scan base-package="com.wzm"/>
-
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
-
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
-
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
-
<property name="user" value="admin"/>
-
<property name="password" value="admin"/>
-
<property name="maxPoolSize" value="20"/>
-
<property name="minPoolSize" value="1"/>
-
<property name="initialPoolSize" value="1"/>
-
<property name="maxIdleTime" value="20"/>
-
</bean>
-
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
<property name="dataSource" ref="dataSource"/>
-
<property name="mappingResources">
-
<list>
-
<value>com/wzm/bean/User.hbm.xml</value>
-
</list>
-
</property>
-
<property name="hibernateProperties">
-
<value>
-
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
-
hibernate.hbm2ddl.auto=update
-
hibernate.show_sql=true
-
</value>
-
</property>
-
</bean>
-
</beans>
2.3 使用properties文件提供数据库接口
这里的接口并非Java中的接口,而是指提供可扩展的接入点,properties文件中的代码如下:
-
URL=jdbc:oracle:thin:@localhost:1521:orcl
-
DRIVER=oracle.jdbc.driver.OracleDriver
-
USERNAME=admin
-
PASSWORD=admin
-
#URL=jdbc:jtds:sqlserver://localhost:1433/qhit02
-
#DRIVER=net.sourceforge.jtds.jdbc.Driver
-
#USERNAME=sa
-
#PASSWORD=sa1234
该文件中提供了Oracle11g和SQL Server两种数据库接口(接入点),甚至可以写入更多的数据库的信息,“#”是注销,即其后的代码不可用。
Spring的application.xml中的配置代码如下:
-
<beans
-
xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-2.5.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
-
<context:component-scan base-package="com.wzm"/>
-
<context:property-placeholder location="classpath:DBOption.properties"/>
-
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
-
<property name="driverClass" value="${DRIVER}"/>
-
<property name="jdbcUrl" value="${URL}"/>
-
<property name="user" value="${USERNAME}"/>
-
<property name="password" value="${PASSWORD}"/>
-
<property name="maxPoolSize" value="20"/>
-
<property name="minPoolSize" value="1"/>
-
<property name="initialPoolSize" value="1"/>
-
<property name="maxIdleTime" value="20"/>
-
</bean>
-
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
<property name="dataSource" ref="dataSource"/>
-
<property name="mappingResources">
-
<list>
-
<value>com/wzm/persistence/entiy/User.hbm.xml</value>
-
</list>
-
</property>
-
<property name="hibernateProperties">
-
<value>
-
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
-
hibernate.hbm2ddl.auto=update
-
hibernate.show_sql=true
最后
由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档
还有更多面试复习笔记分享如下
/persistence/entiy/User.hbm.xml`
-
</list>
-
</property>
-
<property name="hibernateProperties">
-
<value>
-
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
-
hibernate.hbm2ddl.auto=update
-
hibernate.show_sql=true
最后
由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档
[外链图片转存中…(img-kTLF1uK0-1721158662997)]
还有更多面试复习笔记分享如下
[外链图片转存中…(img-pJfuIOEg-1721158662998)]