共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 ujDAs%6MZ
! a1j c_
1、spring容器自带连接池 ]%NCKOM
$z`
jR*
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> t+66kB N
<property name="driverClassName"> JlGyGr^MD
<value>com.mysql.jdbc.Driver</value> egKYlfe"
</property> ZP?](RV>xg
<property name="url"> ][TS|\\
<value>jdbc:mysql://127.0.0.1:3306/test</value> hu6)GOZbv
</property> |[xi"E\
<property name="username"> _Z 9I')
<value>admin</value> 8f#YUK
sW=
</property> b/E1v,/<
<property name="password"> nEs l
<value/> Vd|/]Zj
</property> -BNW\]}
</bean> E6BW&Xp
vUj7rDT|
2、项目中创建连接池(一般利用开源项目) 'O2{0
];oED?I
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> yUBic~S
<property name="driverClassName"> <sd
Qvlx$-
<value>com.mysql.jdbc.Driver</value> XMuZ'I
</property> ~l.]3wyk
<property name="url"> 9/^4W.
<value>jdbc:mysql://127.0.0.1:3306/test</value> 4yjAi@ /2
</property> _3ZZ-=J:=*
<property name="username"> P]INYH
<value>admin</value> >YPfk=0f0
</property> Qg1LT8
<property name="password"> 2R.YHj
<value/> :qw:)i
</property> \b~zyt6-
</bean> vE{QN<6T
%lEPFp
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 4oCnF+(
x4fLe5xv
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> |1rBK.8
<property name="jndiName"> ca$D|3
<value>java:comp/env/jdbc/TestDB</value> R?^FO:nM%!
</property> -cJ(iz9!
</bean> iSHNt0Nl
&a1agi7M
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB A@&+!sO
+Hv%m8'0|
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常