Spring JdbcTemplate 查询方法中的RowMapper实现汇总

sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类.

 我们在数据库查询中,如果返回的类型是用户自定义的类型(其实我们在数据库查询中大部分返回的都是自定义的类)则需要包装,如果是Java自定义的类型,如:String则不需要.

  如果sping与hibernate 相结合了,基本上是用不到,大多数都是在spring单独使用时用到.

  可以通过建立内部类实现RowMapper接口,RowMapper中有一个mapRow方法,所以实现RowMapper接口一定要实现mapRow方法,而对自定义类的包装就在mapRow方法中实现.

这里只是一个简单的例子:

public class TestDao {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
   this.jt = jt;
}
public List<TNpc> getAll(){
    String sql = "select * from t_npc";
   //使用
   List list = jt.query(sql, new NpcRowMapper());
   return list;
}
/**
* 定义内部类实现RowMapper接口
*/
public class NpcRowMapper implements RowMapper{
      //实现mapRow方法
     public Object mapRow(ResultSet rs, int num) throws SQLException {
        //对类进行封装
      TNpc npc = new TNpc();
      npc.setId(rs.getLong("id"));
      npc.setName(rs.getString("name"));
      return npc;
   }  
   }
}

Java代码  收藏代码
  1. 实现一、在内部建立内联类实现RowMapper接口  
  2. package hysteria.contact.dao.impl;  
  3.   
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Types;  
  7. import java.util.List;  
  8.   
  9. import org.springframework.jdbc.core.JdbcTemplate;  
  10. import org.springframework.jdbc.core.RowMapper;  
  11.   
  12. import hysteria.contact.dao.ItemDAO;  
  13. import hysteria.contact.domain.Item;  
  14.   
  15. public class ItemDAOImpl implements ItemDAO {  
  16.  private JdbcTemplate jdbcTemplate;  
  17.   
  18.  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
  19.   this.jdbcTemplate = jdbcTemplate;  
  20.  }  
  21.   
  22.  public Item insert(Item item) {  
  23.   String sql = "INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)";  
  24.   Object[] params = new Object[]{item.getUserId(),item.getName(),item.getPhone(),item.getEmail()};  
  25.   int[] types = new int[]{Types.INTEGER,Types.VARCHAR,Types.CHAR,Types.VARCHAR};  
  26.   jdbcTemplate.update(sql,params,types);  
  27.   return item;  
  28.  }  
  29.   
  30.  public Item update(Item item) {  
  31.   String sql = "UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?";  
  32.   Object[] params = new Object[] {item.getName(),item.getPhone(),item.getEmail(),item.getId()};  
  33.   int[] types = new int[] {Types.VARCHAR,Types.CHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER};  
  34.   jdbcTemplate.update(sql,params,types);  
  35.   
  36.   return item;  
  37.  }  
  38.   
  39.  public void delete(Item item) {  
  40.   String sql = "DELETE FROM items WHERE id = ?";  
  41.   Object[] params = new Object[] {item.getId()};  
  42.   int[] types = new int[]{Types.INTEGER};  
  43.   jdbcTemplate.update(sql,params,types);  
  44.  }  
  45.   
  46.  public Item findById(int id) {  
  47.   String sql = "SELECT * FROM items WHERE id = ?";  
  48.   Object[] params = new Object[] {id};  
  49.   int[] types = new int[] {Types.INTEGER};  
  50.   List items = jdbcTemplate.query(sql,params,types,new ItemMapper());  
  51.   if(items.isEmpty()){  
  52.    return null;  
  53.   }  
  54.   return (Item)items.get(0);  
  55.  }  
  56.   
  57.  public List<Item> findAll() {  
  58.   String sql = "SELECT * FROM items";  
  59.   return jdbcTemplate.query(sql,new ItemMapper());  
  60.  }  
  61.   
  62.   
  63.  public List<Item> findAllByUser(int user_id) {  
  64.   String sql = "SELECT * FROM items WHERE user_id = ?";  
  65.   Object[] params = new Object[]{user_id};  
  66.   int[] types = new int[]{Types.INTEGER};  
  67.   List items = jdbcTemplate.query(sql,params,types,new ItemMapper());  
  68.   return items;  
  69.  }  
  70.   
  71.  protected class ItemMapper implements RowMapper {  
  72.   
  73.   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {  
  74.    Item item = new Item();  
  75.    item.setId(rs.getInt("id"));  
  76.    item.setUserId(rs.getInt("user_id"));  
  77.    item.setName(rs.getString("name"));  
  78.    item.setPhone(rs.getString("phone"));  
  79.    item.setEmail(rs.getString("email"));  
  80.   
  81.    return item;  
  82.   }  
  83.   
  84.  }  
  85.   
  86.   
  87. }  
原文地址:https://www.cnblogs.com/wnlja/p/3910072.html