共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 89+m?H]K
"2Js[uf
1、spring容器自带连接池 ]+d.X]
/DZKz"N
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> %C'!L]#
<property name="driverClassName"> ctH`71Y
<value>com.mysql.jdbc.Driver</value> pZ OVD%
</property> >SSF:hI"J
<property name="url"> D#^v=U
<value>jdbc:mysql://127.0.0.1:3306/test</value> $].< /
</property> % 0fj~s;
<property name="username"> dKZffDTZ
<value>admin</value> f^m8 4o'
</property> VUagZ7p
<property name="password"> Z+I[
<value/> 'X@j
</property> mbJ#-^}V
</bean> VEE:Z^U!
*Fp )/Ih
2、项目中创建连接池(一般利用开源项目) ~`8`kk8
l$)pCo
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> jO!!. w
<property name="driverClassName"> ?6tuo:gP
<value>com.mysql.jdbc.Driver</value> T"dWrtO
</property> )]X_')K
<property name="url"> fvN2]@:
<value>jdbc:mysql://127.0.0.1:3306/test</value> is#?O5:2
</property> Kax85)9u
<property name="username"> 0#XZ_(@%
<value>admin</value> Gq+!%'][P
</property> c1jgBty
<property name="password"> 4+ yd/^S
<value/> #UI@<0P)
</property> 0^:O:X
</bean> O_KL#xo
_oe2pL&
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 *8X: fq
:N%]<Mq
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> o5. q
<property name="jndiName"> 3 T&m
<value>java:comp/env/jdbc/TestDB</value> 0o(/%31]
</property> 'rq
[P",
</bean> oy/#,R_n%
z4_>6sf{
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB j.AAY?L
<7?MutHM-
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常