共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 *uEU9fX
u`bWn
1、spring容器自带连接池 ^iV@NVP
Ne^#5 T
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> jb7=1OPD_
<property name="driverClassName"> 'Fonn
<value>com.mysql.jdbc.Driver</value> %i.|bIhmm
</property> ++dV5
<property name="url"> 5@0c@Q
<value>jdbc:mysql://127.0.0.1:3306/test</value> uFok'3!g7%
</property> HhqqJEp0
<property name="username"> DVB:8"Bu
<value>admin</value> dtF6IdAf
</property> @%#(Hse
<property name="password"> kk~{2
<value/> >,] #~d
</property> dtg Ja_
</bean> >p<(CVX[
PA,j;{,(b
2、项目中创建连接池(一般利用开源项目) _I8-0DnOM
*kKGsy
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 9txZ6/
<property name="driverClassName"> ED?s[K
<value>com.mysql.jdbc.Driver</value> sm_:M| [D
</property> qL'3MY.!
<property name="url"> 2X*epU_1h
<value>jdbc:mysql://127.0.0.1:3306/test</value> 8vT:icl
</property> Yt#($}p
<property name="username"> ko5\*!|:lj
<value>admin</value> w}YHCh
</property> )j9FB
<property name="password"> ]$L[3qA.
<value/> {>+$u"*
</property> 5vpf;
</bean> RU{}qPs?
1B1d>V$*
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 TuF:m"4
B"qG-ci
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> SHGO;
<property name="jndiName"> C$tSsw?A
<value>java:comp/env/jdbc/TestDB</value> ':>B%k
</property> TgUQD(d^
</bean> FdSa Ood8
lp9<j1Wl
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB 5G!X4%a
w+MdQ@'5
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常