当前位置:编程学习 > JAVA >>

Spring JdbcTemplate使用

Spring JdbcTemplate使用
JdbcTemplate主要提供以下五类方法:
execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
 
JdbcTemplate类支持的回调类:
预编译语句及存储过程创建回调:用于根据JdbcTemplate提供的连接创建相应的语句;
         PreparedStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement;
         CallableStatementCreator:通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的CallableStatement;
预编译语句设值回调:用于给预编译语句相应参数设值;
         PreparedStatementSetter:通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值;
         BatchPreparedStatementSetter:;类似于PreparedStatementSetter,但用于批处理,需要指定批处理大小;
自定义功能回调:提供给用户一个扩展点,用户可以在指定类型的扩展点执行任何数量需要的操作;
         ConnectionCallback:通过回调获取JdbcTemplate提供的Connection,用户可在该Connection执行任何数量的操作;
         StatementCallback:通过回调获取JdbcTemplate提供的Statement,用户可以在该Statement执行任何数量的操作;
         PreparedStatementCallback:通过回调获取JdbcTemplate提供的PreparedStatement,用户可以在该PreparedStatement执行任何数量的操作;
         CallableStatementCallback:通过回调获取JdbcTemplate提供的CallableStatement,用户可以在该CallableStatement执行任何数量的操作;
结果集处理回调:通过回调处理ResultSet或将ResultSet转换为需要的形式;
         RowMapper:用于将结果集每行数据转换为需要的类型,用户需实现方法mapRow(ResultSet rs, int rowNum)来完成将每行数据转换为相应的类型。
         RowCallbackHandler:用于处理ResultSet的每一行结果,用户需实现方法processRow(ResultSet rs)来完成处理,在该回调方法中无需执行rs.next(),该操作由JdbcTemplate来执行,用户只需按行获取数据然后处理即可。
         ResultSetExtractor:用于结果集数据提取,用户需实现方法extractData(ResultSet rs)来处理结果集,用户必须处理整个结果集;
 
接下来让我们看下具体示例吧,在示例中不可能介绍到JdbcTemplate全部方法及回调类的使用方法,我们只介绍代表性的,其余的使用都是类似的;
 
 
1)预编译语句及存储过程创建回调、自定义功能回调使用:
[java]
<strong>@Test 
public void testPpreparedStatement1() { 
  int count = jdbcTemplate.execute(new PreparedStatementCreator() { 
     @Override 
     public PreparedStatement createPreparedStatement(Connection conn) 
         throws SQLException { 
         return conn.prepareStatement("select count(*) from test"); 
     }}, new PreparedStatementCallback<Integer>() { 
     @Override 
     public Integer doInPreparedStatement(PreparedStatement pstmt) 
         throws SQLException, DataAccessException { 
         pstmt.execute(); 
         ResultSet rs = pstmt.getResultSet(); 
         rs.next(); 
         return rs.getInt(1); 
      }});     
   Assert.assertEquals(0, count); 

 </strong><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 14px; text-align: left; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 31px; ">首先使用PreparedStatementCreator创建一个预编译语句,其次由JdbcTemplate通过PreparedStatementCallback回调传回,由用户决定如何执行该PreparedStatement。此处我们使用的是execute方法。</p><p style="font-weight: bold; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 14px; text-align: left; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 31px; "> </p><p style="font-weight: bold; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 14px; text-align: left; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 31px; "><strong>2)预编译语句设值回调使用:</strong></p> 
[java] 
@Test   
public void testPreparedStatement2() {   
  String insertSql = "insert into test(name) values (?)";   
  int count = jdbcTemplate.update(insertSql, new PreparedStatementSetter() {   
      @Override   
      public void setValues(PreparedStatement pstmt) throws SQLException {   
          pstmt.setObject(1, "name4");   
  }});   
  Assert.assertEquals(1, count);       
  String deleteSql = "delete from test where name=?";   
  count = jdbcTemplate.update(deleteSql, new Object[] {"name4"});   
  Assert.assertEquals(1, count);   
}   

 通过JdbcTemplate的int update(String sql, PreparedStatementSetter pss)执行预编译sql,其中sql参数为“insert into test(name) values (?) ”,该sql有一个占位符需要在执行前设值,PreparedStatementSetter实现就是为了设值,使用setValues(PreparedStatement pstmt)回调方法设值相应的占位符位置的值。JdbcTemplate也提供一种更简单的方式“update(String sql, Object... args)”来实现设值,所以只要当使用该种方式不满足需求时才应使用PreparedStatementSetter。
3)结果集处理回调:
[java] 
public void testResultSet1() { 
  jdbcTemplate.update("insert into test(name) values('name5')"); 
  String listSql = "select * from test"; 
  List result = jdbcTemplate.query(listSql, new RowMapper<Map>() { 
      @Override 
      public Map mapRow(ResultSet rs, int rowNum) throws SQLException { 
          Map row = new HashMap(); 
          row.put(rs.getInt("id"), rs.getString("name")); 
          return row; 
  }}); 
  Assert.assertEquals(1, result.size()); 
  jdbcTemplate.update("delete from test where name='name5'");      

RowMapper接口提供mapRow(ResultSet rs, int rowNum)方法将结果集的每一行转换为一个Map,当然可以转换为其他类,如表的对象画形式。
[java]
@Test 
public void testResultSet2() { 
  jdbcTempla

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,