共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 kO4C^pl"v
k4@GjO1"$
1、spring容器自带连接池 5u&jNU5m_
mB\5bSFY`
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> u,C-U!A
<property name="driverClassName"> b&ADj8cKC
<value>com.mysql.jdbc.Driver</value> vH=I#Ajar
</property> G$Dg*<
<property name="url"> 41.+3VP
<value>jdbc:mysql://127.0.0.1:3306/test</value> RsbrD8*AD
</property> vw3W:TL
<property name="username"> 2vwT8/
<value>admin</value> GP[$&8\M
</property> ZGrV? @o,6
<property name="password"> [`&cA#C9Yp
<value/> >A)he!I
</property> ua{eri[
</bean> Ze~\=X" "
E )PEKWK\
2、项目中创建连接池(一般利用开源项目) ^O?$}sr
*D'VW{
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> D H/1 :H
<property name="driverClassName"> U!Lws#\X
<value>com.mysql.jdbc.Driver</value> j04Q3d
\f
</property> e#AB0-f
<property name="url"> qj|GAGrQ2
<value>jdbc:mysql://127.0.0.1:3306/test</value> q\~7z1
</property> D Lu]d$G
<property name="username"> b"gYNGgX
<value>admin</value> +vQyHo
</property> <
;g0?M\
<property name="password"> { sZrI5
<value/> '
Sl9xd
</property> mMMu'N
</bean> 464Z0C
b/ynCf8X
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 bi5'- .B
u&<LW4
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> iZ58;`
<property name="jndiName"> ZpZ~[BtQ
<value>java:comp/env/jdbc/TestDB</value> mdk:2ndP
</property> ^^[,aBu
</bean> YziQU_
5p~Z-kU&
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB B<oi,S
Ywni2-)<
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常