使用Spring 征服数据库--Spring JdbcTemplete

Spring 提供了一组数据库访问框架,集成了多种数据库访问技术不管是直接使用JDBC还是使用 IBATIS 或 Hibernate这样的ORM框架实现数据持久化,Spring都能帮助我们简化开发代码 处理那些重复的单调的事情(不变部分),让得我们可以只关注与核心的数据持久化的代码实现(可变部分)。下面我们先来介绍Spring对JDBC的支持。

Spring将数据访问过程中固定的和可变的划分成两个类:模板和回调。模板管理数据访问过程中固定的部分,回调管理数据访问过程中变化的部分。如下图所示:

Spring 为JDBC提供了3个模板类供使用:

 
模板类 说明
JdbcTemplate 最基本的SpringJdbc模板,提供了最简单的数据库访问功能和索引参数查询功能。
NamedParamaterJdbcTemplete 使用该模板进行查询时可以将查询值以命名参数的形式绑定到查询SQL中,而不是使用简单的索引参数查询。
SimpleJdbcTemplete 该模板使用java5的一些特性,入自动装箱、泛型以及可变参数列表来简化模板的使用。在Spring2.5以后也具备了和NamedParamaterJdbcTemplete同样的功能。所以 最佳实践就是使用SimpleJdbcTemplete
<!-- DBCP 数据库连接池 -->
    <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="maxActive" value="10"></property>
        <property name="initialSize" value="5"></property>
    </bean>
<!-- C3P0 数据库连接池 -->
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3"></property>
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="30"></property>
        <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="1000"></property>
        <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
        <property name="autoCommitOnClose" value="false"></property>
        <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
            获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
        <property name="breakAfterAcquireFailure" value="false"></property>
        <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 
            0 -->
        <!-- <property name="checkoutTimeout" value="100" ></property> -->
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60"></property>
        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="8"></property>
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="60"></property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="15"></property>
        <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
            如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements" value="100"></property>
        <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
        <property name="maxStatementsPerConnection" value="10"></property>
        <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过多线程实现多个操作同时被执行。Default: 
            3 -->
        <property name="numHelperThreads" value="3"></property>
    </bean>
<!-- spring 自带的数据源对象 每次返回一个新的连接 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

以上数据源推荐 C3P0 或DBCP

<bean id="namedParamaterJdbcTemplete"
        class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <!-- <constructor-arg name="dataSource" ref="dataSource"></constructor-arg> -->
        <!-- <constructor-arg name="dataSource" ref="dbcpDataSource"></constructor-arg> -->
        <constructor-arg name="dataSource" ref="c3p0DataSource"></constructor-arg>
    </bean>
package com.sane.spring.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.BeforeClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;


/**
 * Unit test for simple App.
 */
public class AppTest
{
   private static JdbcTemplate jdbcTemplate;
   private static NamedParameterJdbcTemplate namedJdbcTemplate;
   @BeforeClass
   public static void setUpClass(){
       ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("Beans.xml");
       jdbcTemplate=(JdbcTemplate)applicationContext.getBean("jdbcTemplete");
       namedJdbcTemplate=(NamedParameterJdbcTemplate)applicationContext.getBean("namedParamaterJdbcTemplete");
   }
//   @org.junit.Test
   public void query(){
       String sql="select id,username,fullname,email from test.spitter";
       jdbcTemplate.query(sql, new RowCallbackHandler(){
        public void processRow(ResultSet rs) throws SQLException {
            System.out.println(rs.getInt("id"));
            System.out.println(rs.getString("username"));
            System.out.println(rs.getString("fullname"));
            System.out.println(rs.getString("email"));
        }
           
           
       });
   }
   
   @org.junit.Test
   public void query2(){
       String sql="select id,password,username,fullname,email from test.spitter";
      Spiltter spiltter= namedJdbcTemplate.query(sql, new ResultSetExtractor<Spiltter>() {
        public Spiltter extractData(ResultSet rs) throws SQLException, DataAccessException {
            Spiltter spiltter=new Spiltter();
            if (rs.next()) {
                spiltter.setUserName(rs.getString("username"));
                spiltter.setFullName(rs.getString("fullname"));
                spiltter.setId(rs.getInt("id"));
                spiltter.setPassword(rs.getString("password"));
                spiltter.setEmail(rs.getString("email"));
            }
            
            return spiltter;
        }
    });
      System.err.println(spiltter.toString());
   }
}

 

posted @ 2016-10-14 00:10  sanemu  阅读(253)  评论(0)    收藏  举报