spring-rowmapper

  1. •RowMapper能夠將數據中的每一行封裝成用戶定義的類,在數據庫查詢中,若是返回的類型是用戶自定義的類型則須要包裝,若是是Java自定義的類型,如:String則不須要,Spring最新的類SimpleJdbcTemplate使用更加簡單了。    
  2. •下面這個實例說明了如何使用RowMapp,從網上下載的代碼,還行能說明問題。在使用過程當中咱們能夠將內部類作成POJO的外部類,只要實現RowMapper接口便可。若是用戶想讓ApplicationContext進行定義仍是要謹慎。畢竟實現RowMapper接口須要給一個類增長一個mapRow方法,讓類承受的功能較多,不利於分析系統    
  3. •實現1、在內部創建內聯類實現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. •}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息