共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 )n@ 3@NV
H_nJST<v`
1、spring容器自带连接池 puF
Z~WZ
Xm'K6JH'
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> krc!BK`V
<property name="driverClassName"> p>3'77
V
<value>com.mysql.jdbc.Driver</value> >/bK?yT<
</property> " SqKS,J
<property name="url"> vI}S6-"<
<value>jdbc:mysql://127.0.0.1:3306/test</value> ?GU!ke p
</property> W=]",<
<property name="username"> T]-~?;Jh8
<value>admin</value> r0
C6Ww7u
</property> 5T#D5Z<m
<property name="password"> VTfaZ/e.
<value/> X{9o8
*V
</property> #)}bUNc'
</bean> 4{t$M} ?N
DNBpIC5&6
2、项目中创建连接池(一般利用开源项目) #!# X3j
^VPl>jTg
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 5>Yd\(`K
<property name="driverClassName">
w{EU9C
<value>com.mysql.jdbc.Driver</value> ?Zp!AV
</property> w{x(YVSH
<property name="url"> de{YgN
<value>jdbc:mysql://127.0.0.1:3306/test</value> <W')
~o}
</property> T<k1?h^7
<property name="username"> BM~niW;k
<value>admin</value> h]$?~YE
</property> TFDCo_>o
<property name="password"> l0xFt
~l
<value/> 5THS5'
</property> z}2e;d 7
</bean> q%Jy>IXt
Jd"s~n<>K
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 a;(zH*/XK
i9k]Q(o
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> $=xQ X
<property name="jndiName"> #ic 2ofI
<value>java:comp/env/jdbc/TestDB</value> h^ $}1[
</property> e`M]ZGrr
</bean> 4@iJ|l
&ntP~!w
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB v|2q2 bz
RXS| -_$
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常