【spring配置】——spring配置数据源

来源:互联网 发布:软件开发技术联盟 编辑:程序博客网 时间:2024/05/25 05:37

spring整合数据源,数据源交由Spring管理后,可以方便的配置事务,配置hibernate或者mybatis等。


常用的配置方式有三种:

1.dbcp

2.c3p0

3.jndi


配置文件:

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"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-3.0.xsd"><!--*********************** Spring集成数据源(Oracle)第一种:dbcp数据源第二种:c3p0数据源第三种:jndi数据源 ***********************--><!-- dbcp数据源 --><!-- destory-method是保证spring容器关闭时,数据源能够正常工作--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" /><property name="username" value="test" /><property name="password" value="test"/></bean><!-- c3p0数据源 --><bean id="dataSource1" 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:XE"/> <property name="user" value="test"/> <property name="password" value="test"/> </bean><!-- jndi数据源(需要在容器tomcat或者weblogic中配置对应数据源) --> <bean id="dataSource3" class="org.springframework.jndi.JndiObjectFactoryBean">             <!--weblogic             <property name="jndiName" value="jdbc/pool" /> -->             <!--tomcat-->             <property name="jndiName" value="java:comp/env/jdbc/pool">        </bean></beans>
如果使用jndi数据源,则需要在weblogic控制台中配置数据源。

如果使用tomcat服务器,需要在tomcat的配置文件context.xml中:

<Resource name="jdbc/pool"  auth="Container"        type="javax.sql.DataSource"        username="test"        password="test"        driverClassName="oracle.jdbc.driver.OracleDriver"        url="jdbc:oracle:thin:@localhost:1521:XE"        maxActive="100"        maxIdle="30"        maxWait="10000"/>


0 0
原创粉丝点击