共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 9- )qZ
yoS? s
1、spring容器自带连接池 pIO4,VL;W
QLo(i
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> mP^SS
Je
<property name="driverClassName"> +%#8k9Y
<value>com.mysql.jdbc.Driver</value> l-O$ m
</property> `
\ZqgX4
<property name="url"> 82:Wvp6
<value>jdbc:mysql://127.0.0.1:3306/test</value> ?<%=:
Yh
</property> AEg(m<t
<property name="username"> g8pO
Lr'
<value>admin</value> ,sQ93(Vo
</property> Qc"'8kt
<property name="password"> < bUe/m
<value/> 7@;">`zvm
</property> j8$Zv%Ca%
</bean> r`M6!}oa
YT-=;uK^S
2、项目中创建连接池(一般利用开源项目) p
EusTP
Q*ju
sm
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> Sc$UZ/qPT
<property name="driverClassName"> N4{nG,Mo]
<value>com.mysql.jdbc.Driver</value>
&|o$=Ad
</property> {"}V&X160o
<property name="url"> [r-}bp'Gp
<value>jdbc:mysql://127.0.0.1:3306/test</value> <+U|dX
</property> FN87^.^2S
<property name="username"> fZN><3MO>
<value>admin</value> E4oz|2!m
</property> Ciihsm
<property name="password"> x]4Kkpqm
<value/> ?J!3j{4e
</property> ?S[Y:<R{:
</bean> {qpi?oY
.#w6%c@
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 ukAKFc^)k
H3KTir"on
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> i*[n{=*l@
<property name="jndiName"> gK1g]Tc @G
<value>java:comp/env/jdbc/TestDB</value> f`$Gz
</property>
vNDu9ovs-
</bean> a)s;dp}T%
HSz"
tN
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB :E_a0!'
k&6I f0i
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常