spring配置DataSource(数据源的几种方式):
数据源可以注入到sessionFactory中。
<?xml version="1.0" encoding="UTF-8"?>
<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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
>
============================================spring自身提供的数据源
<!-- 配置数据源(Spring自身提供
的数据源DriverManagerDataSourc)
实现类DriverManagerDataSource,它实现了javax.sql.DataSource接口)
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:summer" />
<property name="username" value="xiaoxiao" />
<property name="password" value="xiaoxiao" />
</bean>
-->
============================================C3P0数据源
<!-- 配置C3P0数据源
<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:summer" />
<property name="user" value="xiaoxiao" />
<property name="password" value="xiaoxiao" />
</bean>
-->
============================================DBCP数据源
<!-- 配置DBCP数据源
需要包括/lib/jakarta- commons/commons-pool.jar
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
-->
=========================================================指定数据源配置文件(jdbc.properties)的位置
有两种方式指定properties文件位置:
<!------------------------------方式一、 指定数据源配置文件的位置
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
-->
<!------------------------------方式二、 配置文件的位置 ,可配置多个
<context:property-placeholder
location="classpath:jdbc.properties,classpath:user.properties"/>
-->
===================================================================================引用配置文件的属性值${}
示例如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
======================================================== JNDI 连接池配置
注意:拷贝数据库驱动包ojdbc.jar 到tomcat的lib目录下
======================================================== 1、context.xml配置:
1、在tomcat conf-->context.xml中配置:
<context>
<Resource name="jdbc/test" --jndi的名称,随便命名,但后面都要与它一致
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:summer"
username="xiaoxiao" password="xiaoxiao"
maxActive="20" maxIdle="10"/>
</context>
======================================================== 2、web.xml配置:
2、项目里的web.xml配置:
<resource-ref>
<description>test DataSource</description>
<res-ref-name>jdbc/test</res-ref-name> -- JNDI名称,同上
<res-type>javax.sql.DataSource</res-type> --- 类型 同上
<res-auth>Container</res-auth> ----------- 同上
</resource-ref>
======================================================== 3、spring配置文件(applicationContext.xml):
3、spring配置文件:
有两种方式:
=======================================================方式一:JndiObjectFactoryBean类配置dataSource
3.1、
<!-- 1、配置JNDI数据源(配置在高性能的应用服务器(如WebLogic或Websphere等)上)
Spring为此专门提供引用JNDI资源的JndiObjectFactoryBean类
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/test"/>
</bean>
=======================================================方式一:<jee>元素配置dataSource
3.2、引入命名空间jee
xmlns:jee="http://www.springframework.org/schema/jee"
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/test"/>
=========================================================== 4、将dataSource注入到sesisonFactory
将dataSource注入到sessionFactory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> ----注入dataSource
<property name="hibernateProperties"> --------hibernate的属性值
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources"> ---------------hibernate实体映射文件位置 (此时我们已经不需要hibernate.cfg.xml文件了)
<list>
<value>com/spring/test/Subject.hbm.xml</value>
</list>
</property>
</bean>
</beans>