spring中三种数据库连接配置

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
	<bean id="a" class="example.TestAjax">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
	<!-- DriverManagerDataSource建立连接是只要有连接就新建一个connection,没有连接池的作用。 -->
	<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/mysql" />
		<property name="username" value="root" />
		<property name="password" value="111111" />
	</bean>
	<!-- dbcp使用了连接池技术 -->
	<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
		<property name="username" value="root" />
		<property name="password" value="111111" />
	</bean>
	<!-- jndi -->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:comp/env/jdbc/mysql" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>

web.xml

<resource-ref>
	<description>DataBase Connection</description>
	<res-ref-name>jdbc/mysql</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
	<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	<Resource
	    auth="Application"
	    name="jdbc/mysql"
	    type="javax.sql.DataSource"
	    factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
	    driverClassName="com.mysql.jdbc.Driver"
	    url="jdbc:mysql://localhost:3306/mysql"
	    username="root"
	    password="111111"
	/>
</Context>
使用jndi的要在web.xml,context.xml作修改,另外两种不用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值