共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 !fjDO!,!
7@5}WNr
1、spring容器自带连接池 ,SH))%Cyt
c:M~!CXO
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ;i> |5tEy
<property name="driverClassName"> dFK/
<value>com.mysql.jdbc.Driver</value> RoT}L#!!
</property> N
=)9O
<property name="url"> 89@gYA"Su
<value>jdbc:mysql://127.0.0.1:3306/test</value> YqrieDFay!
</property> 3Jf_3c
<property name="username"> d A[I
<value>admin</value> hgL wxJu
</property> W/L~&.'
<property name="password"> V'^Hn?1^
<value/> D!+d]A[r
</property> .sgP3Ah
</bean> .e~17}Ka}
`~F=
2、项目中创建连接池(一般利用开源项目) *{/BPc0*
txw:m*(%
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 4DaLmQ2O
<property name="driverClassName"> 9])dLL0
<value>com.mysql.jdbc.Driver</value> V)=!pT
</property> *xI0hFJIM
<property name="url"> GMyzQ]@}
<value>jdbc:mysql://127.0.0.1:3306/test</value> n3-5`Jti
</property> p<: bPw
<property name="username"> QJ\
o"c
<value>admin</value> mbK$_HvU
</property> k|'{$/n
<property name="password"> ~*@UQ9*p#
<value/> >/9f>d?w^
</property> $i;%n1VBg
</bean> 1
\:5ow&a
R<I)}<g(A3
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 8XIG<Nc
&Rdg07e;>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> HN]roSt~
<property name="jndiName"> Y92wL}
<value>java:comp/env/jdbc/TestDB</value> 4"U/T1&
</property> O4dJ> O
</bean> =W$
f+
f.-b.nNf
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB FCgr
7j| ^ZuI+
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常