共分spring容器自带连接池、项目中创建连接池、服务器创建连接池三种 pO)EYla9
%4M,f.[e
1、spring容器自带连接池 .7Yox1,
_\k?uUo&,^
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> _fn7-&6
<property name="driverClassName"> L{IMZ+IB2|
<value>com.mysql.jdbc.Driver</value> pV8tn!
</property> vdUKIP
=|_
<property name="url"> Tzjv-9^V
<value>jdbc:mysql://127.0.0.1:3306/test</value>
K5`*Y@
</property> 7uw-1F5x7
<property name="username"> [ t8]'RI%
<value>admin</value> w]h8KNt
</property> W58?t6!
=
<property name="password"> SnUR?k1
<value/> U&*%KPy`
</property> , :K{
</bean> ;m.6 ~A
P $y'``
2、项目中创建连接池(一般利用开源项目) b[:m[^
WB'1_a
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> / s Apj
<property name="driverClassName"> s 8K.A~5 w
<value>com.mysql.jdbc.Driver</value> 6/g
82kqpk
</property> 54WX#/<Yik
<property name="url"> 2)$-L'YS
<value>jdbc:mysql://127.0.0.1:3306/test</value> c]U+6JH
</property> 6)i>qz).
<property name="username"> \BfMCA/
<value>admin</value> g<^A(zM
</property> wmR~e
<property name="password"> 1VG]|6f
<value/> as/PM"
</property> I} Q+{/?/
</bean> yXEI%2~)
sieC7raO
3、应用服务器创建数据源,项目中通过jndi访问,以tomcat为例 Ax=)J{4v
G9jf]Ye;
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> T?Z&\g0yp
<property name="jndiName"> {=&({ cS
<value>java:comp/env/jdbc/TestDB</value> sL\ {.ad5
</property> I!kR:Z
</bean> U%BtBPL
<0~1
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB [%6)
y#`;[!
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常