共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 wj/r)rv
E
DzYno-]A]
1、spring容器自带连接池 r6^DD$X
_+UD>u{
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> nI6`/
<property name="driverClassName"> 'Ct+0X:D
<value>com.mysql.jdbc.Driver</value> 5!AzEB
</property> Bdq/Ohw|!
<property name="url"> oV*3Mec
<value>jdbc:mysql://127.0.0.1:3306/test</value> %3q@\:s
</property> ~<|xS
<property name="username"> BqR8%F
<value>admin</value> b2Ct^`|M5
</property> c=ZX7U
<property name="password"> QutQG
<value/> }ulFW]A^7
</property> bJ9>,,D
</bean> \
X uu|]
QtRKmry{
2、项目中创建连接池(一般利用开源项目) A }dl@
qD%Jf4.0j
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> -tDmzuD6
<property name="driverClassName"> &
K7+V
<value>com.mysql.jdbc.Driver</value> *s_)E2
</property> nkvzv
<property name="url"> gvz&ppcG
<value>jdbc:mysql://127.0.0.1:3306/test</value> [/#;u*n
</property> wKwireOs
<property name="username"> YncY_Hu
<value>admin</value> lGs fs(
</property> 8TuOf(qE
<property name="password"> #.) qQ8*(
<value/> 7XdLZ4ub
</property> #A?U_32z/2
</bean> XO\P4x:c
-[ F<u
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 YT#"HYO
Byon2| nf7
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> Dtelr=/s
<property name="jndiName"> w(s"r p}
<value>java:comp/env/jdbc/TestDB</value> P E.^!j
</property> :eDwkzlHH
</bean> I,.>tC
C7,Ol0`v
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB n;4`IK|
7j{SCE;
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常