org.springframework.jdbc.core.JdbcTemplate。我向从数据库中查找数据。请问他里面的方法怎么使用,特别是
query(String sql, RowMapper rowMapper) 中的RowMapper的用法

解决方案 »

  1.   

    RowMapper可以将数据中的每一行封装成用户定义的类,在数据库查询中,如果返回的类型是用户自定义的类型则需要包装。public class ItemMapper implements RowMapper {  
      
      public Object mapRow(ResultSet rs, int rowNum) throws SQLException {  
       Item item = new Item();  
       item.setId(rs.getInt("id"));  
       item.setUserId(rs.getInt("user_id"));  
       item.setName(rs.getString("name"));  
       item.setPhone(rs.getString("phone"));  
       item.setEmail(rs.getString("email"));  
      
       return item;  
      }  
      

  2.   

    要是在页面上显示,list<item>就是一个对象集合了是吗,或者是说query(String sql, RowMapper rowMapper) 返回的结果是list<item>是吗?
      

  3.   

      String sql = "SELECT * FROM items WHERE user_id = ?";  
      Object[] params = new Object[]{user_id};  
      int[] types = new int[]{Types.INTEGER};  
      List items = jdbcTemplate.query(sql,params,types,new ItemMapper());  你说的很对!
      

  4.   


    去网上找找吧 很多的! 在就是spring的官方文档 写的很好!