共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 TR5"K{WDx
=MRg
1、spring容器自带连接池 rM<c;iQ
t!AHTtI
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> I/_`/mQ
<property name="driverClassName"> )#9/vIQ
<value>com.mysql.jdbc.Driver</value> ZVR0Kzu?Ra
</property> ul>$vUbyf
<property name="url"> y 7|x<Z
<value>jdbc:mysql://127.0.0.1:3306/test</value> [&j!g
</property> N3TkRJZ
<property name="username"> a(NN%'fDD
<value>admin</value> 8 POrD8B
</property> smF#'"{
<property name="password"> OTl\^!
<value/> +D* b!5[
</property> @]Aul9.h
</bean> T8JM4F
Ef;_im
2、项目中创建连接池(一般利用开源项目) 5 b( [1*
2d >kc2=*
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 1%G<gbHpI
<property name="driverClassName"> c_)vWU
<value>com.mysql.jdbc.Driver</value> #^"\WG7{
</property> dMp7 ,{FhF
<property name="url"> hT>h
<value>jdbc:mysql://127.0.0.1:3306/test</value> $C##S@
</property> /Tv=BXL-
<property name="username"> r3mB"("Z'
<value>admin</value> #U{^L{1Gx
</property> u 3wF)B{
<property name="password"> cmmH)6c>
<value/> - (VV
</property> vtc} )s\
</bean> 8 *;G\$+
:CV!:sUm
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 i%GjtYjS
.Kv>*__-Q
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> ?>N82#9Q
<property name="jndiName"> z1aApS
<value>java:comp/env/jdbc/TestDB</value> %qcBM~efT
</property> 4tz8^z[Kw
</bean> 9t(B{S
5@" bx=
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB !u.{<51b
jJg9M'@2!
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常