共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 MATgJ`lsy
H8$<HhuZM
1、spring容器自带连接池 S1^nC tSF
/ggkb8<3
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> Bug}^t{M
<property name="driverClassName"> YYE8/\+B.
<value>com.mysql.jdbc.Driver</value> Z@,PZ
</property> {!}F
:~*r
<property name="url"> w^])(
<value>jdbc:mysql://127.0.0.1:3306/test</value> G_M:0YI@
</property> QGr\I/Y
<property name="username"> 3g0u#t{
<value>admin</value> }#OqU#
q|
</property> )?B~64N,+
<property name="password"> }B=qH7u.K
<value/> YWRE&MQ_
</property> |ck
ZyDA
</bean> & &" 'dL
|O(-CDQe
2、项目中创建连接池(一般利用开源项目) t1w2u.]
yS)-&t!;
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> w}j6.r
<property name="driverClassName"> kOAY@a
<value>com.mysql.jdbc.Driver</value> UXwB$@8
</property> B)rr7B
<property name="url"> ZUXse1,
<value>jdbc:mysql://127.0.0.1:3306/test</value> s~LZOPN
</property> Z .bit_(
<property name="username"> >v1 y 0zx
<value>admin</value> V~T`&
</property> '<%Nw-
<property name="password"> "*w)puD
<value/> *Mwfod
</property> #dZ/UM(u
</bean> U=F-]lD
4|6&59?pnc
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 |^Ew<
}PI35i1!t
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> LG=X)w)W4S
<property name="jndiName"> \5'O.*pr
<value>java:comp/env/jdbc/TestDB</value> %j*k
</property> V$dJmKg
</bean> G@!_ZM8h
g\o{}Q%X
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB ~V2ajM1Z&O
4=Tpi`
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常