spring jdbc rowmap 使用方法

来源:互联网 发布:淘宝店铺基本加权设置 编辑:程序博客网 时间:2024/06/04 19:54

  1. RowMapper可以将数据中的每一行封装成用户定义的类,在数据库查询中,如果返回的类型是用户自定义的类型则需要包装,如果是Java自定义的类型,如:String则不需要,Spring最新的类SimpleJdbcTemplate使用更加简单了。   
  2. 下面这个实例说明了如何使用RowMapp,从网上下载的代码,还行能说明问题。在使用过程中我们可以将内部类做成POJO的外部类,只要实现RowMapper接口即可。如果用户想让ApplicationContext进行定义还是要谨慎。毕竟实现RowMapper接口需要给一个类增加一个mapRow方法,让类承受的功能较多,不利于分析系统   
  3. 实现一、在内部建立内联类实现RowMapper接口   
  4.   
  5. package hysteria.contact.dao.impl;   
  6.   
  7. import java.sql.ResultSet;   
  8. import java.sql.SQLException;   
  9. import java.sql.Types;   
  10. import java.util.List;   
  11.   
  12. import org.springframework.jdbc.core.JdbcTemplate;   
  13. import org.springframework.jdbc.core.RowMapper;   
  14.   
  15. import hysteria.contact.dao.ItemDAO;   
  16. import hysteria.contact.domain.Item;   
  17.   
  18. public class ItemDAOImpl implements ItemDAO {   
  19.     private JdbcTemplate jdbcTemplate;   
  20.   
  21.     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {   
  22.         this.jdbcTemplate = jdbcTemplate;   
  23.     }   
  24.   
  25.     public Item insert(Item item) {   
  26.         String sql = "INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)";   
  27.         Object[] params = new Object[] { item.getUserId(), item.getName(),   
  28.                 item.getPhone(), item.getEmail() };   
  29.         int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.CHAR,   
  30.                 Types.VARCHAR };   
  31.         jdbcTemplate.update(sql, params, types);   
  32.         return item;   
  33.     }   
  34.   
  35.     public Item update(Item item) {   
  36.         String sql = "UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?";   
  37.         Object[] params = new Object[] { item.getName(), item.getPhone(),   
  38.                 item.getEmail(), item.getId() };   
  39.         int[] types = new int[] { Types.VARCHAR, Types.CHAR, Types.VARCHAR,   
  40.                 Types.VARCHAR, Types.INTEGER };   
  41.         jdbcTemplate.update(sql, params, types);   
  42.   
  43.         return item;   
  44.     }   
  45.   
  46.     public void delete(Item item) {   
  47.         String sql = "DELETE FROM items WHERE id = ?";   
  48.         Object[] params = new Object[] { item.getId() };   
  49.         int[] types = new int[] { Types.INTEGER };   
  50.         jdbcTemplate.update(sql, params, types);   
  51.     }   
  52.   
  53.     public Item findById(int id) {   
  54.         String sql = "SELECT * FROM items WHERE id = ?";   
  55.         Object[] params = new Object[] { id };   
  56.         int[] types = new int[] { Types.INTEGER };   
  57.         List items = jdbcTemplate.query(sql, params, types, new ItemMapper());   
  58.         if (items.isEmpty()) {   
  59.             return null;   
  60.         }   
  61.         return (Item) items.get(0);   
  62.     }   
  63.   
  64.     public List<Item> findAll() {   
  65.         String sql = "SELECT * FROM items";   
  66.         return jdbcTemplate.query(sql, new ItemMapper());   
  67.     }   
  68.   
  69.     public List<Item> findAllByUser(int user_id) {   
  70.         String sql = "SELECT * FROM items WHERE user_id = ?";   
  71.         Object[] params = new Object[] { user_id };   
  72.         int[] types = new int[] { Types.INTEGER };   
  73.         List items = jdbcTemplate.query(sql, params, types, new ItemMapper());   
  74.         return items;   
  75.     }   
  76.   
  77.     protected class ItemMapper implements RowMapper {   
  78.   
  79.         public Object mapRow(ResultSet rs, int rowNum) throws SQLException {   
  80.             Item item = new Item();   
  81.             item.setId(rs.getInt("id"));   
  82.             item.setUserId(rs.getInt("user_id"));   
  83.             item.setName(rs.getString("name"));   
  84.             item.setPhone(rs.getString("phone"));   
  85.             item.setEmail(rs.getString("email"));   
  86.   
  87.             return item;   
  88.         }   
  89.   
  90.     }   
  91.   
  92. }  
0 0
原创粉丝点击