(转载)Spring数据源的灵活配置巧应用

 
环境:
Java SE 1.5
Spring-2.5.1
mysql-connector-java-5.1.5.zip
Mysql 5.x
 
为了从数据库中取得数据,我们首先需要获取一个数据库连接。 Spring通过DataSource对象来完成这个工作。 DataSource是JDBC规范的一部分, 它被视为一个通用的数据库连接工厂。通过使用DataSource, Container或Framework可以将连接池以及事务管理的细节从应用代码中分离出来。 作为一个开发人员,在开发和测试产品的过程中,你可能需要知道连接数据库的细节。 但在产品实施时,你不需要知道这些细节。通常数据库管理员会帮你设置好数据源。
 
在使用Spring JDBC时,你既可以通过JNDI获得数据源,也可以自行配置数据源( 使用Spring提供的DataSource实现类)。使用后者可以更方便的脱离Web容器来进行单元测试。 这里我们将使用DriverManagerDataSource,不过DataSource有多种实现, 后面我们会讲到。使用DriverManagerDataSource和你以前获取一个JDBC连接 的做法没什么两样。你首先必须指定JDBC驱动程序的全限定名,这样DriverManager 才能加载JDBC驱动类,接着你必须提供一个url(因JDBC驱动而异,为了保证设置正确请参考相关JDBC驱动的文档), 最后你必须提供一个用户连接数据库的用户名和密码。
 
以上两段文字摘自Spring开发文档原文,下面我写的一个小的应用,用来测试Srping JDBC支持的效果。
 
下面这个测试要实现一个目标:通过获取Spring的数据源来查询MySQL数据库testdb的一个表t_user数据。

一、创建项目,加载程序用到的工具包和驱动。并在源代码目录中添加Spring的配置文件ApplicationContext.xml,配置内容如下:
<? xml version ="1.0" encoding ="gb2312" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

< beans >
    <!-- 数据库的数据源定义-->
     < bean id ="rptds" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method ="close" >
         < property name ="driverClassName" >
             < value >com.mysql.jdbc.Driver </ value >
         </ property >
         < property name ="url" >
             < value >jdbc:mysql://localhost:3306/testdb </ value >
         </ property >
         < property name ="username" >
             < value >root </ value >
         </ property >
         < property name ="password" >
             < value >leizhimin </ value >
         </ property >
     </ bean >
</ beans >
 
二、然后创建数据库环境:
 
create database if not exists testdb;

DROP TABLE IF EXISTS t_user;

CREATE TABLE t_user (
  id int(11) NOT NULL auto_increment,
  firstname varchar(50) default NULL,
  lastname varchar(50) default NULL,
  zipcode varchar(10) default NULL,
  tel varchar(20) default NULL,
  address varchar(200) default NULL,
  PRIMARY KEY  (id)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

insert  into t_user(id,firstname,lastname,zipcode,tel,address) values  
(1,'haha','hehe','234444','13577788999','阿斯地方'),
(2,'lei','aaa','450000','13939012107','郑州市经三路');
 
三、Spring Bean容器环境获取工具类
 
package com.lavasoft.dbtest;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by IntelliJ IDEA.
* File: ContextHelper.java
* User: leizhimin
* Date: 2008-2-21 14:20:46
* Spring Bean容器环境获取工具类
*/

public final class ContextHelper {
     private static ClassPathXmlApplicationContext _ctx;

     static {
        _ctx = new ClassPathXmlApplicationContext( "ApplicationContext.xml");
    }

     private ContextHelper() {
    }

     public static ClassPathXmlApplicationContext getContext() {
         return _ctx;
    }
}
 
三、写获取数据库工具类
 
package com.lavasoft.dbtest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;

/**
* Created by IntelliJ IDEA.
* File: DBUtil.java
* User: leizhimin
* Date: 2008-2-21 14:26:30
* 数据库工具
*/

public final class DBUtil {
     private static final Log log = LogFactory.getLog(DBUtil. class);

     /**
     * 获取系统的数据源
     *
     * @return DataSource
     */

     public static DataSource getDataSource() {
        DataSource dataSource = null;
         try {
            dataSource = (DataSource) ContextHelper.getContext().getBean( "rptds");
        } catch (Exception e) {
            log.error( "获取数据源出错,请检查Spring数据源配置!");
        }
         return dataSource;
    }

     /**
     * 获取数据库连接
     *
     * @return Connection
     */

     public static Connection makeConnection() {
        Connection conn = null;
         try {
            conn = getDataSource().getConnection();
        } catch (SQLException e) {
            log.error( "通过数据源获取数据库连接发生异常!");
            e.printStackTrace();
        }
         return conn;
    }

     /**
     * 执行没有参数的SQL过程
     *
     * @param procedureName 存储过程名字
     * @return boolean      返回存储过程执行的结果,true表示执行成功,false表示执行失败.
     */

     public static boolean executeBSDProcedure(String procedureName) {
         boolean flag = false;
        String sqlStr = "{call " + procedureName + "()}";
        CallableStatement cs;
        Connection conn = makeConnection();
         try {
            cs = (CallableStatement) conn.prepareStatement(sqlStr);
            cs.executeUpdate(sqlStr);
            flag = true;
        } catch (SQLException e) {
            log.error( "调用存储过程" + sqlStr + "失败!");
            e.printStackTrace();
        }
         return flag;
    }
}
 
四、写测试类
 
package com.lavasoft.dbtest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Created by IntelliJ IDEA.
* File: Test.java
* User: leizhimin
* Date: 2008-2-21 14:41:49
* Spring 数据源应用测试
*/

public class Test {
     private static final Log log = LogFactory.getLog(Test. class);

     public static void main(String args[]) {
        Test.test();
    }

     public static void test() {
        String testSql = "select * from t_user";
        Connection conn = DBUtil.makeConnection();
        Statement stmt = null;
         try {
            stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
            ResultSet rs = stmt.executeQuery(testSql);
             while (rs.next()) {
                String firstName = rs.getString( "firstname");
                String lastName = rs.getString( "lastname");
                System.out.println(firstName + " " + lastName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
             if (stmt != null) {
                 try {
                    stmt.close();
                } catch (SQLException e) {
                    log.info( "关闭Statement对象出现异常!");
                    e.printStackTrace();
                }
            }
             if (conn != null) {
                 try {
                    conn.close();
                } catch (SQLException e) {
                    log.error( "关闭数据库连接失败!");
                    e.printStackTrace();
                }
            }
        }
    }
}
 
测试运行结果:
 
haha hehe
lei aaa

Process finished with exit code 0
 
五、数据源置换
Spring实现的DriverManagerDataSource并没有提供连接池的功能,只是用来作简单的单机连接测试,并不适合使用于真正的项目当中,可以考虑用比较成熟的数据连接池来取代。Apache DBCP连接池是不错,如要要替换,则需要加载DBCP相关的工具包。
<? xml version ="1.0" encoding ="gb2312" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

< beans >
    <!-- 数据库的数据源定义-->
     < bean id ="rptds" 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://localhost:3306/testdb </ value >
         </ property >
         < property name ="username" >
             < value >root </ value >
         </ property >
         < property name ="password" >
             < value >leizhimin </ value >
         </ property >
     </ bean >
</ beans >
 
现在所使用的是org.apache.commons.dbcp.BasicDataSource作为注入的DataSource实例,为了使用DBCP 的功能,您需要在Classpath路径中设定commons-dbcp.jar、commons-pool.jar与commons- collections.jar。注意到在dataSource上设定了"destroy-method"属性,如此可以确保BeanFactory在关闭时也一并关闭BasicDataSource。
六、使用JNDI数据源
 
如果您的Servlet容器提供了JNDI(Java Naming and Directory Interface)的DataSource,您也可以简单的换上这个DataSource:
<? xml version ="1.0" encoding ="gb2312" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

< beans >
    <!-- 数据库的数据源定义-->
     < bean id ="dataSource"
           class ="org.springframework.indi.JndiObjectFactoryBean" >
         < property name ="jndiName" >
             < value >jdbc/testds </ value >
         </ property >
     </ bean >
</ beans >
 
为了使用org.springframework.indi.JndiObjectFactoryBean,您需要spring-context.jar,"jndiName"实际上要根据您所设定的JNDI查询名称。
 
参考资料:
Spring 2.0开发文档
Spring 技术手册
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值