共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 G[*z,2Kb>
6$0<&')Yb
1、spring容器自带连接池 cU-A1W
r
\[|'hA
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> XUKlgl!+.
<property name="driverClassName"> 5F!Qn\{u{
<value>com.mysql.jdbc.Driver</value> 6O,:I
</property> [2YPV\=
<property name="url"> <W>A }}q
<value>jdbc:mysql://127.0.0.1:3306/test</value> rd<43
</property> ZyDNtX%
<property name="username"> PU[<sr#,
<value>admin</value> 7r50y>
</property> :|$cG~'J
<property name="password"> F+GX{e7E\
<value/> LR{bNV[i
</property> j!y9E~Zz
</bean> K a&
2>F
L N
Fe7<y
2、项目中创建连接池(一般利用开源项目) z9g ++]rkJ
x=IZ0@p
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> wP*3Hx;S
<property name="driverClassName"> /t<
&
<value>com.mysql.jdbc.Driver</value> Ov<c1y;f
</property> nGe4IY\-w
<property name="url"> iCA-X\E
<value>jdbc:mysql://127.0.0.1:3306/test</value> P3]K'*Dyd
</property> XI ;] c5
<property name="username"> Pc =ei
<value>admin</value> 4].o:d;`/
</property> K#N9N@W jR
<property name="password"> /8Y8-&K0
<value/> CQNMCYjg(R
</property> V8):!
</bean> 4r`I)
-3i(N.)<;
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 %qz-b.
`kxC#
&HO
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> yf0v,]v[
<property name="jndiName"> xGQP*nZ
<value>java:comp/env/jdbc/TestDB</value> ;Z"MO@9:
</property> p<(a);<L
</bean> Jz>P[LcB
R;pW,]}g,
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB t6\H
N2U&TCc
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常