Spring中的DataSource配置

    Spring中我们使用的数据源可以是多种,用户可以选择,也可以自己定义~~ZteDataSource.java就是自己定义的数据源~~
其中有dbcp和c3p0常用的数据源...在使用的时候,应该特别注意配置中Property节点下的name的值...每种数据源的配置都是不一样的... 

   applicationContext.xml  // #######  //

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
   <property name="url" value="jdbc:oracle:thin:@10.30.2.204:1527:sec"></property>
   <property name="username" value="apps"></property>
   <property name="password" value="secapp29"></property>
 </bean>
 
  <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
   <property name="jdbcUrl" value="jdbc:oracle:thin:@10.30.2.204:1527:sec"></property>
   <property name="user" value="apps"></property>
   <property name="password" value="secapp29"></property>
 </bean>
 
  <bean id="dataSource_zte" class="com.zte.ZteDataSource">
   <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
   <property name="driverUrl" value="jdbc:oracle:thin:@10.30.2.204:1527:sec"></property>
   <property name="username" value="apps"></property>
   <property name="password" value="secapp29"></property>
 </bean>
 
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="sql-map-config.xml"></property>
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <bean id="userDao" class="example.UserDaoImpl">
  <property name="sqlMapClient" ref="sqlMapClient"></property>
 </bean>
 
</beans>


   ZteDataSource  // #######  //

package com.zte;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

public class ZteDataSource implements DataSource {
 
 private String driverClass;
 private String driverUrl;
 private String username;
 private String password;

 public ZteDataSource() {
  super();
  // TODO Auto-generated constructor stub
 }

 public Connection getConnection() throws SQLException {
  // TODO Auto-generated method stub
  Connection conn = null;
  try {
   Class.forName(driverClass);
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  conn =DriverManager.getConnection(driverUrl,username,password);
  
  return conn;
 }

 public Connection getConnection(String username, String password)
   throws SQLException {
  // TODO Auto-generated method stub
  Connection conn = null;
  try {
   Class.forName(driverClass);
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  conn =DriverManager.getConnection(driverUrl,username,password);
  
  return conn; 
 }

 public PrintWriter getLogWriter() throws SQLException {
  // TODO Auto-generated method stub
  return null;
 }

 public int getLoginTimeout() throws SQLException {
  // TODO Auto-generated method stub
  return 0;
 }

 public void setLogWriter(PrintWriter arg0) throws SQLException {
  // TODO Auto-generated method stub

 }

 public void setLoginTimeout(int arg0) throws SQLException {
  // TODO Auto-generated method stub

 }

 public String getDriverClass() {
  return driverClass;
 }

 public void setDriverClass(String driverClass) {
  this.driverClass = driverClass;
 }

 public String getDriverUrl() {
  return driverUrl;
 }

 public void setDriverUrl(String driverUrl) {
  this.driverUrl = driverUrl;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }
 

}


  测试代码片段: TestMain.java

 
   
   //数据源
   String dataSourceId="dataSource_zte"; //dataSource_zte
//   String dataSourceId="dataSource_c3p0";//c3p0DataSource
//   String dataSourceId="dataSource"; //dataSource_dbcp
   DataSource ds = (DataSource) context.getBean(dataSourceId);
   System.out.println("Connection:" + ds.getConnection());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值