共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 YVLaO*(f
=i)k@w_(x
1、spring容器自带连接池 7^:0?Q
3~!PJI1
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> eqE%ofW
<property name="driverClassName"> \=/^H
<value>com.mysql.jdbc.Driver</value> [P_1a`b
</property> nK9A=H'Hc
<property name="url"> 6|:]2S
<value>jdbc:mysql://127.0.0.1:3306/test</value> 3F0:v,+;
</property> y/@.T\p
<property name="username"> "&/&v
<value>admin</value> DV/P/1E
</property> G(~"Zt}?
<property name="password"> (yel
<value/> M e
</property> G?6[K&w
</bean> pYs"Y;%
#zcnc$x\
2、项目中创建连接池(一般利用开源项目) =w!14@W
BqKh&m
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> sf5koe
<property name="driverClassName"> L~jKx)S%
<value>com.mysql.jdbc.Driver</value> !>+Na~eN
</property> V+l>wMeo
<property name="url"> =r:-CRq(
<value>jdbc:mysql://127.0.0.1:3306/test</value> u{ .UZTn
</property> v2Bks2
<property name="username"> z_J"Qk
<value>admin</value> Q4MTedj1H
</property> }A"%YDrNbG
<property name="password"> DjjG?(1
<value/> s],+]<qX
</property> v(t?d
</bean> hQfxz,X
b|*A%?m
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 s^$zOp9
<3>Ou(F
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> xCV3HnZ
<property name="jndiName"> U:`g12
<value>java:comp/env/jdbc/TestDB</value> HJ*W3Mg
</property> L6O@ q`\z
</bean> n'JwT!
A
i- E~ZfJ
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB %!HmtpS
DP NUm<>
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常