Chinaunix首页 | 论坛 | 博客
  • 博客访问: 587041
  • 博文数量: 68
  • 博客积分: 5070
  • 博客等级: 大校
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-11 14:20
文章分类

全部博文(68)

文章存档

2011年(3)

2010年(30)

2009年(17)

2008年(18)

我的朋友

分类: Java

2010-04-21 11:46:57

  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 findAll() {   
  58.    String sql = "SELECT * FROM items";   
  59.   return jdbcTemplate.query(sql,new ItemMapper());   
  60. }   
  61.   
  62.   
  63. public List 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. }  
阅读(14053) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

最代码2014-05-24 12:52:23

Spring jdbcTemplate代码下载:http://www.zuidaima.com/share/search.htm?key=jdbcTemplate