共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 s5nB(L*Pjp
NuIT{3S
1、spring容器自带连接池 Z%KL[R}^w;
@{'o#EJY
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -S`TEX
<property name="driverClassName"> rAL1TU(vm
<value>com.mysql.jdbc.Driver</value> n}42'9p
</property> 4Sqvhz
<property name="url"> q$3HvZP
<value>jdbc:mysql://127.0.0.1:3306/test</value> kGruo5A
</property> X1O65DMr`g
<property name="username"> f>p; siR)
<value>admin</value> hJ<:-u+yk}
</property> k"0%' Y
<property name="password"> ]}_p3W "Y9
<value/> :l4^iSf
</property> ysL0hwir
</bean> Ia=&.,xub
4 iik5
2、项目中创建连接池(一般利用开源项目) JThk Wx
!B0v<+;P8
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> JN-D/s
<property name="driverClassName"> bJANZn|H
<value>com.mysql.jdbc.Driver</value> H&w(]PDh
</property> zI1(F67d`
<property name="url"> G,+xT}@wu
<value>jdbc:mysql://127.0.0.1:3306/test</value> FYxUOO
</property> b8eDD+ul k
<property name="username"> )iT.A
<value>admin</value> )~1.<((<
</property> L,f^mX0<
<property name="password"> @wg&6uQ
<value/> /DK*yS
</property> 58qaA\iw
</bean> o-L|"3P
o]dK^[/*
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 \o0z@Ntq
"tqS|ok.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> unx;m$-c
<property name="jndiName"> D7%^Ly
<value>java:comp/env/jdbc/TestDB</value> yjeqv-7
</property> \x3^
</bean> IiG4ib>)W
TXA. 6e
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB <{3q{VW*
L~'^W/N
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常