共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 /ej[oR
/oW]? 9
1、spring容器自带连接池 G^N@r:RS
T/A2Y+@N;
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> CuD}Uo+u
<property name="driverClassName"> _RzFh
<value>com.mysql.jdbc.Driver</value> %3O))Ug5
</property> 8v z h5,U
<property name="url">
99.F'Gz
<value>jdbc:mysql://127.0.0.1:3306/test</value> p^|6 /b
</property> NT0n[o^
<property name="username"> XmD(&3;v-
<value>admin</value> IITUM)
</property> Th$Z9+()
<property name="password"> WW.@S5
<value/> 5F+G8
</property> tAE(`ow/Ur
</bean> RyP MzxV
JO*/UC>"
2、项目中创建连接池(一般利用开源项目) 4(sHUWT
}tw+8YWkz
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
`!t-$i
<property name="driverClassName"> Gbjh|j=
<value>com.mysql.jdbc.Driver</value> 9Vqy<7i1
</property> T>]T=
<property name="url"> 4e/!BGkAS
<value>jdbc:mysql://127.0.0.1:3306/test</value> UOsK(mB
</property> }NoP(&ebz*
<property name="username"> `3i<jZMG
<value>admin</value> xu_,0ZT]{
</property> Rw%%
9
<property name="password"> ]iGeqwT
<value/> F|{uA/P{
</property> S
:8
</bean> yq49fEgc@U
ax>j3HKi
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 9Q^cE\j
O>pv/Ns
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> CvmIDRP*
<property name="jndiName"> zPND$3&'
<value>java:comp/env/jdbc/TestDB</value> E(j#R"
</property> :IU7dpwDl
</bean> %0} ^M1
z4:<?K
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB `:Gzjngc
k'PvTWR
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常