日期:2014-05-16  浏览次数:20450 次

Spring jdbc 批量JDBC操作、ORM映射
一、Spring JDBC 概述

     Spring 提供了一个强有力的模板类JdbcTemplate简化JDBC操作,DataSource,JdbcTemplate都可以以Bean的方式定义在想xml配置文件,JdbcTemplate创建只需注入一个DataSource,应用程序Dao层只需要继承JdbcDaoSupport, 或者注入JdbcTemplate,便可以获取JdbcTemplate,JdbcTemplate是一个线程安全的类,多个Dao可以注入一个JdbcTemplate;

<!--         Oracle数据源           -->  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>  
        <property name="url" value="jdbc:oracle:thin:@oracle.devcake.co.uk:1521:INTL"/>  
        <property name="username" value="sa"/>  
        <property name="password" value=""/>  
</bean>  
  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="dataSource"/>  
 </bean>  
  
<bean id="customerDao" class="JdbcCustomerDao" >  
         <property name="dataSource" ref="dataSource"/>  
</bean>  
<!--         Oracle数据源           -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@oracle.devcake.co.uk:1521:INTL"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
 </bean>

<bean id="customerDao" class="JdbcCustomerDao" >
         <property name="dataSource" ref="dataSource"/>
</bean>

然后将jdbcTemplate对象注入自定义的Dao、或者继承JdbcDaoSupport,例如:
public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao {  
}  
  
public class JdbcCustomerDao implements CustomerDao {  
  
          private JdbcTemplate jdbcTemplate  
  
         public void setJdbcTemplate()JdbcTemplate jdbcTemplate{  
               this.jdbcTemplate=jdbcTemplate  
         }  
}  
public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao {
}

public class JdbcCustomerDao implements CustomerDao {

          private JdbcTemplate jdbcTemplate

         public void setJdbcTemplate()JdbcTemplate jdbcTemplate{
               this.jdbcTemplate=jdbcTemplate
         }
}

二、 JdbcTemplate 提供以下主要方法简化JDBC操作:
2.1、List query(String sql,Ojbect[] args,RowMapper rowMapper)

     说明:常用的查询,sql待执行的sql语句,args是sql语句的参数,rowMapper负责将每一行记录转化为java对象存放在list,并最终返回,例如:

public List<Book> queryByAuthor(String author) {  
        String sql = "select * from book where author=?";  
        Collection c = getJdoTemplate().find(sql,  
                new Object[] { author },new BookRowMapper());  
        List<Book> books = new ArrayList<Book>();  
        books.addAll(c);  
        return books;  
}  
  
class BookRowMapper implements RowMapper{  
     public Object mapRow(ResultSet res, int index) throws SQLException {  
          Book book = new Book();  
          book.setId(rs.getInt("id"));  
          //省略set  
       return book;  
    }  
}  
public List<Book> queryByAuthor(String author) {
        String sql = "select * from book where author=?";
        Collection c = getJdoTemplate().find(sql,
                new Object[] { author },new BookRowMapper());
        List<Book> books = new ArrayList<Book>();
        books.addAll(c);
        return books;
}

class BookRowMapper implements RowMapper{
     public Object mapRow(ResultSet res, int index) throws SQLException {
          Book book = new Book();
          book.setId(rs.getInt("id"));
          //省略se