共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 ^|(w)Sy
D%0GXUp
1、spring容器自带连接池 b%fn1Ag9
uW8LG\Z>D5
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ci@U
a}T
<property name="driverClassName"> )&j4F)
<value>com.mysql.jdbc.Driver</value> i7fQj,
q
</property> s C9j73vf
<property name="url">
JRm:hf'
<value>jdbc:mysql://127.0.0.1:3306/test</value> [kyF |3k~
</property> tfr*/+F
<property name="username"> IR+dGqIjZb
<value>admin</value> yB7=8 Pcx
</property> ~7W?W<
<property name="password"> ]Xur/C2A
<value/> R^.oM1qu|
</property> Fvy__qcHi
</bean> Q`qHzb~%
L>Y>b4oy3
2、项目中创建连接池(一般利用开源项目) _U.D*f<3)
l)glT]G3+
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 1Z +3=$P
<property name="driverClassName"> Bf;dp`(/
<value>com.mysql.jdbc.Driver</value> cp0@wC#d
</property> ,OX(z=i_
<property name="url"> O;9'0-F ?
<value>jdbc:mysql://127.0.0.1:3306/test</value> Y{TzN%|LV
</property> bUf2uWy7
<property name="username"> ,c@^u6a
<value>admin</value> eU"!X9
</property> )v-sde\
<property name="password"> ;^}gC}tq
<value/> X ?/C9
</property> (bxSN@hp2
</bean> .$/Su3]K/
OAMsqeWYA
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 CBAMAr
~a:0Q{>a
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> .v36xX K(
<property name="jndiName"> m]8rljo
<value>java:comp/env/jdbc/TestDB</value> bwG2=
</property> %nQii?1`i
</bean> G};os+FxF
{)j3Pn
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB %O`e!p
b-8{bP]n
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常