共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 ZE9*i}r
yP@=x!$
1、spring容器自带连接池 }E=mZZ)
lIf Our
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> j6\{j#q
<property name="driverClassName"> I%ez_VG
<value>com.mysql.jdbc.Driver</value> Lh+^GQ
</property> BdceINI
<property name="url"> $6_J`7
<value>jdbc:mysql://127.0.0.1:3306/test</value> \6N\6=t!A
</property> YC$pT
<property name="username"> 6O"0?wG+
<value>admin</value> e'jR<ln|
</property> '?d[ ip
<property name="password"> 0-5:"SN'
<value/> m'S-h'a
</property> BH}u\K
</bean> N\p3*#M
Zd%*,\`S
2、项目中创建连接池(一般利用开源项目) NzEuiI}
UkdQ#b1
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> [~J4:yDd=
<property name="driverClassName"> N9i>81tY
<value>com.mysql.jdbc.Driver</value> d&fENnt?h
</property> B!5gD
<property name="url"> r4-r
z+x
<value>jdbc:mysql://127.0.0.1:3306/test</value> jj^CW"IB
</property> Q|0[B4e^:
<property name="username"> m\t
%wr
<value>admin</value> YrdK@I
</property> Jlzhn#5c-
<property name="password"> <"t >!I
<value/> | o?@Eh
</property> /5o~$S
</bean> :!R+/5a
,e;(\t:
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 3
-5^$-7_
al5?w{us
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> R4o_zwWgPw
<property name="jndiName"> / og'W j
<value>java:comp/env/jdbc/TestDB</value> X<1# )xC
</property> ~h1'_0t
</bean> ]-O:| q>]
)'qZ6%
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB s^6S {XJ
+>s[w{Svy
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常