1、直接连接
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://127.0.0.1:3306/test</value>
- </property>
- <property name="username">
- <value>admin</value>
- </property>
- <property name="password">
- <value/>
- </property>
- </bean>
- 3 jndi,以tomcat为例
- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName">
- <value>java:comp/env/jdbc/TestDB</value>
- </property>
- </bean>
2、连接池
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://127.0.0.1:3306/test</value>
- </property>
- <property name="username">
- <value>admin</value>
- </property>
- <property name="password">
- <value/>
- </property>
- </bean>
注意每种容器的jndi写法不同,所以java:comp/env/jdbc/TestDB为其全称,但在配置jndi时可能会是其中一部分,如tomcat配置jdbc/TestDB
配置错误会报javax.naming.NameNotFoundException: Name jdbc is not bound in this Context异常