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

spring JdbcTemplate小结

?

?

Spring

?

?

提供了JdbcTemplate 来封装数据库jdbc操作细节:
包括: 数据库连接[打开/关闭]异常转义SQL执行查询结果的转换

使用模板方式封装 jdbc数据库操作-固定流程的动作,提供丰富callback回调接口功能,方便用户自定义加工细节,更好模块化jdbc操作,简化传统的JDBC操作的复杂和繁琐过程。

1) 使用JdbcTemplate 更新(insert /update /delete)

?

int k = jdbcTemplate.update("UPDATE tblname SET prop1=?,prop2=?...", new Object[]{...});

?

jdbcTemplate.update("INSERT INTO tblname VALUES(?,?,..)", new Object[]{...},
	 new int[]{Types.VARCHAR,Types.NUMERIC}); 

?

jdbcTemplate.update("INSERT INTO tblname VALUES(?,?,..)",                     
        new PreparedStatementSetter(){                          
               public void setValues(PreparedStatement ps) throws SQLException{      
                    ps.setLong(1, user.getId(1)); 
                    ps.setString(2, user.getName(2));   
                    ps.setDate(3, new java.sql.Date(new Date().getTime());  
                    ps.setTimestamp(4, new Timestamp(new Date().getTime());
               }                     
        }
);


2) 使用JdbcTemplate 查询 (select)

final User user = new User(); 
jdbcTemplate.query("SELECT id,name,.. FROM tblname WHERE id=1", 
       new RowCallbackHandler(){ 
              public void processRow(ResultSet rs) throws SQLException{ 
                    user.setId(rs.getLong(1)); 
                    user.setName(rs.getString(2)); 
              } 
      }
);  

?

List uGroup = jdbcTemplate.query("SELECT id,name,.. FROM tblname WHERE igroup=1", 
     new RowMapper(){ 
            public Object mapRow(ResultSet rs,int no) throws SQLException{ 
                     User user = new User(); 
                     user.setId(rs.getLong(1)); 
                     user.setName(rs.getString(2)); 
                     return user ; 
            }
     } 
}; 


3)使用JdbcTemplate 便捷方法

List uNames = jdbcTemplate.queryForList("SELECT name FROM tblname WHERE id>?", 
	new Integer []{5}, String.class); 

?

List<Map> uMapList = (List<Map>) jdbcTemplate.queryForList( "SELECT id, name FROM tblname WHERE id>?",
			 new Integer []{5}); 
for(Map<String,Object> uMap :uMapList){ 
      Integer id = uMap.get("id"); 
      String name = uMap.get("name"); 
}; 

?

String user = jdbcTemplate.queryForObject("SELECT name FROM tblname WHERE id=?",
	 new Integer []{5}, String.class ); 

?

int uNum = jdbcTemplate.queryForInt("SELECT count(*) FROM tblname WHERE id>?", 
	new Integer []{5}); 



4)使用jdbc 操作类

a)扩展 MappingSqlQuery类

class JdbcQueryObject extends MappingSqlQuery { // extends SqlQuery
      public JdbcQueryObject (DataSource ds,String sql){
            this.setDataSource( ds );
            this.setSql( sql );
            this.declareParameter(new Sqlparameter("propName", 
                Types.VARCHAR);// propName 提示作用
        this.compile();
      }
      public Object mapRow(ResultSet rs,int p) throws SQLException{
                 // ...
     }
}
JdbcQueryObject queryObj = new JdbcQueryObject( ds,
      "SELECT .. FROM tblName WHERE param=?");
List list = queryObj.execute(new Object[]{...});

?

b)使用 SqlFunction 类 查询单条结果