共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 wy-
C~b'Qd
E5BgQ5'
1、spring容器自带连接池 b\;u9C2y'
q VdC ?A|
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> &f<1=2dm
<property name="driverClassName"> _+04M)q0
<value>com.mysql.jdbc.Driver</value> B4:l*P'
</property> jc.JX_/
<property name="url"> H'GYJ ?U"
<value>jdbc:mysql://127.0.0.1:3306/test</value> <h_P+ nz
</property> 1uS-Tx
<property name="username"> 7Jx-W|
<value>admin</value> >`<2}Me6
</property> _`Q It>R
<property name="password"> l \^nC2
<value/> !ObE{2Enf
</property> 2vkB<[tSs
</bean> 5nx<,-N*BP
CSL{Q
2、项目中创建连接池(一般利用开源项目) 7IT l3>
8q?;Hg
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> sjzXJ`s
<property name="driverClassName"> jfY{z=*]u
<value>com.mysql.jdbc.Driver</value> 3U~lI&
</property> ytve1<.Ff
<property name="url"> ft/^4QcyAM
<value>jdbc:mysql://127.0.0.1:3306/test</value> J,1osG<6x
</property> _MdZDhtm
<property name="username"> q^N0abzgP
<value>admin</value> j|&DP-@g/
</property> f' '{.L
<property name="password"> e*P=2*]M
<value/> XW?ybH6
</property> "@{4.v^}!
</bean> >Qm<-g
R#/?AD&
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 :6W^ S/pf
j6e}7
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> #H;hRl
<property name="jndiName"> |0L=8~M(j
<value>java:comp/env/jdbc/TestDB</value> mLg{6qm(q
</property> B` *f(
</bean> rv}mD
R VkU+7
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB l`b1%0y
qx2E-PDL;<
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常