Spring数据源配置总结

1.c3p0数据源

	<!-- 定义c3p0数据源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 指定连接数据库url -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost/sampledb" />
		<!-- 指定连接数据库用户名-->
		<property name="user" value="root" />
		<!-- 指定连接数据库密码-->
		<property name="password" value="123" />
	</bean>

 

2.DBCP数据源

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close"
		p:driverClassName="com.mysql.jdbc.Driver"
		p:url="jdbc\:mysql\://localhost\:3306/sampledb"
		p:username="root"
		p:password="123"/>

 3.JNDI数据源

 我们也可以采用下面的方式,但是要加入以下命名空间
xmlns:jee="http://www.springframework.org/schema/jee"

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

	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/sampledb</value>
		</property>
	</bean>

使用 Tomcat服务器 + SpringFramework 进行JavaEE项目的开发部署的时候可以在Tomcat的配置文件中进行JDBC数据源的配置 

添加如下代码到tomcat的conf目录下的server.xml中

<Context> 
	<Resource name="jdbc/demoDB" auth="Container" 
	type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	url="jdbc:mysql://localhost:3306/sampledb"
	username="root"
	password="123"
	maxActive="50"
	maxIdle="30"
	maxWait="10000" />
</Context>


或在tomcat目录下conf\context.xml中的<Context>标签中加入  

<?xml version="1.0" encoding="UTF-8"?>
<Context>
      <Resource name="jdbc/sampledb" auth="Container" 
      type="javax.sql.DataSource"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/demo"
      username="root"
      password="123"
      maxActive="50"
      maxIdle="30"
      maxWait="10000" />
</Context>

4.Spring的数据源实现类DataManagerDataSource

没有数据库连接池,只用于测试

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/demo" />
        <property name="username" value="root" />
        <property name="password" value="123" />
</bean>

猜你喜欢

转载自szlxh002.iteye.com/blog/2264754
今日推荐