Struts+Spring+Hibernate整合

  1. <property name="connection.password">admin</property>

  2. <property name="hibernate.c3p0.max_size">20</property>

  3. <property name="hibernate.c3p0.max_statements">1</property>

  4. <property name="hibernate.c3p0.timeout">1800</property>

  5. <property name="hibernate.c3p0.max_statements">50</property>

  6. <property name="hibernate.dialect">

  7. org.hibernate.dialect.Oracle10gDialect

  8. </property>

  9. <property name="show_sql">true</property>

  10. <property name="hibernate.hbm2ddl.auto">update</property>

  11. <mapping resource="com/wzm/bean/User.hbm.xml"/>

  12. </session-factory>

  13. </hibernate-configuration>

Spring的application.xml文件整合Hibernate代码如下:

  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:p="http://www.springframework.org/schema/p"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  6. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  7. <property name="configLocation" value="classpath:hibernate.cfg.xml">

  8. </property>

  9. </bean>

  10. </beans>

2.2 方法二 废弃使用hibernate.cfg.xml文件

将数据库的配置放在Spring的配置文件application.xml文件中,代码如下:

  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xmlns:p="http://www.springframework.org/schema/p"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  12. http://www.springframework.org/schema/tx

  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  14. http://www.springframework.org/schema/aop

  15. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  16. <context:component-scan base-package="com.wzm"/>

  17. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

  18. <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>

  19. <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>

  20. <property name="user" value="admin"/>

  21. <property name="password" value="admin"/>

  22. <property name="maxPoolSize" value="20"/>

  23. <property name="minPoolSize" value="1"/>

  24. <property name="initialPoolSize" value="1"/>

  25. <property name="maxIdleTime" value="20"/>

  26. </bean>

  27. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  28. <property name="dataSource" ref="dataSource"/>

  29. <property name="mappingResources">

  30. <list>

  31. <value>com/wzm/bean/User.hbm.xml</value>

  32. </list>

  33. </property>

  34. <property name="hibernateProperties">

  35. <value>

  36. hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

  37. hibernate.hbm2ddl.auto=update

  38. hibernate.show_sql=true

  39. </value>

  40. </property>

  41. </bean>

  42. </beans>

2.3 使用properties文件提供数据库接口

这里的接口并非Java中的接口,而是指提供可扩展的接入点,properties文件中的代码如下:

  1. URL=jdbc:oracle:thin:@localhost:1521:orcl

  2. DRIVER=oracle.jdbc.driver.OracleDriver

  3. USERNAME=admin

  4. PASSWORD=admin

  5. #URL=jdbc:jtds:sqlserver://localhost:1433/qhit02

  6. #DRIVER=net.sourceforge.jtds.jdbc.Driver

  7. #USERNAME=sa

  8. #PASSWORD=sa1234

该文件中提供了Oracle11g和SQL Server两种数据库接口(接入点),甚至可以写入更多的数据库的信息,“#”是注销,即其后的代码不可用。

Spring的application.xml中的配置代码如下:

  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xmlns:p="http://www.springframework.org/schema/p"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  12. http://www.springframework.org/schema/tx

  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  14. http://www.springframework.org/schema/aop

  15. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  16. <context:component-scan base-package="com.wzm"/>

  17. <context:property-placeholder location="classpath:DBOption.properties"/>

  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

  19. <property name="driverClass" value="${DRIVER}"/>

  20. <property name="jdbcUrl" value="${URL}"/>

  21. <property name="user" value="${USERNAME}"/>

  22. <property name="password" value="${PASSWORD}"/>

  23. <property name="maxPoolSize" value="20"/>

  24. <property name="minPoolSize" value="1"/>

  25. <property name="initialPoolSize" value="1"/>

  26. <property name="maxIdleTime" value="20"/>

  27. </bean>

  28. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  29. <property name="dataSource" ref="dataSource"/>

  30. <property name="mappingResources">

  31. <list>

  32. <value>com/wzm/persistence/entiy/User.hbm.xml</value>

  33. </list>

  34. </property>

  35. <property name="hibernateProperties">

  36. <value>

  37. hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

  38. hibernate.hbm2ddl.auto=update

  39. hibernate.show_sql=true

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

MySQL全家桶笔记

还有更多面试复习笔记分享如下

Java架构专题面试复习

/persistence/entiy/User.hbm.xml`

  1. </list>

  2. </property>

  3. <property name="hibernateProperties">

  4. <value>

  5. hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

  6. hibernate.hbm2ddl.auto=update

  7. hibernate.show_sql=true

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

[外链图片转存中…(img-kTLF1uK0-1721158662997)]

还有更多面试复习笔记分享如下

[外链图片转存中…(img-pJfuIOEg-1721158662998)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值