共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 <wA_2S
Y
bpILiC
1、spring容器自带连接池 N?Z?g_a8
!6%mt} h
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> @rF\6I
<property name="driverClassName"> u`~{:V
<value>com.mysql.jdbc.Driver</value> GhT7:_r~
</property> th<]L<BP/
<property name="url"> CNz[@6-cYU
<value>jdbc:mysql://127.0.0.1:3306/test</value> !(~>-;A8
</property> 3$b(iI< "
<property name="username"> :tgTYIF
<value>admin</value> SM<kE<q#
</property> CG7LF
<property name="password"> O^:h _L
<value/> 2=|IOkY
</property> = V , _
</bean> [4t KJ+v
Y>%NuL|s
2、项目中创建连接池(一般利用开源项目) +!Ltn
vqHJc2yYkZ
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> I6fpXPP).
<property name="driverClassName"> -a[{cu{
<value>com.mysql.jdbc.Driver</value> >tzXbmFp;
</property> LNb![Rq
<property name="url"> 4tU~ ^z
<value>jdbc:mysql://127.0.0.1:3306/test</value>
Y[DKj!v
</property> "10VN*)J}
<property name="username"> cmeyCyV*
<value>admin</value> aFym&n\
</property> {P5@2u6S
<property name="password"> .R'i=D`Pz
<value/>
i=D,T[|>a
</property> <j#EyGAV
</bean> -T8
gV1*(<
1sJN^BvuG
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 lN'/Z&62
""d>f4,S
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> a3 x~B=E
<property name="jndiName"> e2fct|'
<value>java:comp/env/jdbc/TestDB</value> X
A|`wAGP
</property> z,)sS<t(
</bean> &^H
"T6
h~@+M5r,
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB [
lW
" M
ni>
;8O]=
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常