- RowMapper可以將數據中的每一行封裝成用戶定義的類,在數據庫查詢中,如果返回的類型是用戶自定義的類型則需要包裝,如果是Java自定義的類型,如:String則不需要,Spring最新的類SimpleJdbcTemplate使用更加簡單了。
- 下面這個實例說明了如何使用RowMapp,從網上下載的代碼,還行能說明問題。在使用過程中我們可以將內部類做成POJO的外部類,只要實現RowMapper接口即可。如果用戶想讓ApplicationContext進行定義還是要謹慎。畢竟實現RowMapper接口需要給一個類增加一個mapRow方法,讓類承受的功能較多,不利於分析系統
- 實現一、在內部建立內聯類實現RowMapper接口
- package hysteria.contact.dao.impl;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import java.util.List;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.RowMapper;
- import hysteria.contact.dao.ItemDAO;
- import hysteria.contact.domain.Item;
- public class ItemDAOImpl implements ItemDAO {
- private JdbcTemplate jdbcTemplate;
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
- public Item insert(Item item) {
- String sql = "INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)";
- Object[] params = new Object[] { item.getUserId(), item.getName(),
- item.getPhone(), item.getEmail() };
- int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.CHAR,
- Types.VARCHAR };
- jdbcTemplate.update(sql, params, types);
- return item;
- }
- public Item update(Item item) {
- String sql = "UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?";
- Object[] params = new Object[] { item.getName(), item.getPhone(),
- item.getEmail(), item.getId() };
- int[] types = new int[] { Types.VARCHAR, Types.CHAR, Types.VARCHAR,
- Types.VARCHAR, Types.INTEGER };
- jdbcTemplate.update(sql, params, types);
- return item;
- }
- public void delete(Item item) {
- String sql = "DELETE FROM items WHERE id = ?";
- Object[] params = new Object[] { item.getId() };
- int[] types = new int[] { Types.INTEGER };
- jdbcTemplate.update(sql, params, types);
- }
- public Item findById(int id) {
- String sql = "SELECT * FROM items WHERE id = ?";
- Object[] params = new Object[] { id };
- int[] types = new int[] { Types.INTEGER };
- List items = jdbcTemplate.query(sql, params, types, new ItemMapper());
- if (items.isEmpty()) {
- return null;
- }
- return (Item) items.get(0);
- }
- public List<Item> findAll() {
- String sql = "SELECT * FROM items";
- return jdbcTemplate.query(sql, new ItemMapper());
- }
- public List<Item> findAllByUser(int user_id) {
- 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());
- return items;
- }
- protected 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;
- }
- }
- }
spring jdbc rowmap 使用方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
從缺陷到創新:質量保障的新視角
原創
2024-06-07 23:55:01
CI+GPT雙引擎驅動,開啓AI代碼評審新紀元
京東雲開發者
2024-06-07 23:54:54
Java開發必讀,談談對Spring IOC與AOP的理解
原創
2024-06-07 22:57:21
Junit4遇上chatGPT
原創
2024-06-06 23:55:13
一文搞懂 Spring 循環依賴
原創
2024-06-06 13:11:47
營銷系統黑名單優化:位圖的應用解析
京東雲開發者
2024-06-06 11:54:12
基於阿里雲服務網格流量泳道的全鏈路流量管理(三):無侵入式的寬鬆模式泳道
原創
2024-06-05 21:13:51
iLogtail 2.0 重大升級,端上支持 SPL
原創
2024-06-05 21:13:43
一文搞懂5種內存溢出案例,內含完整源碼
原創
2024-06-05 10:56:55
高效啓動DolphinScheduler工作流:Java URL調用詳解
原創
2024-06-04 21:21:59
記一次疑似JVM內存泄漏的排查過程
原創
2024-06-04 12:09:32
?* CI+GPT雙引擎驅動,?* 開啓AI代碼評審新紀元
原創
2024-06-04 12:09:24
pfinder實現原理揭祕
原創
2024-06-04 02:39:24
pfinder實現原理揭祕
原創
2024-06-04 02:37:09