Spring JDBCTemplate Query方法查询_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Spring JDBCTemplate Query方法查询

Spring JDBCTemplate Query方法查询

 2014/7/14 12:33:24  wbj0110  程序员俱乐部  我要评论(0)
  • 摘要:在内部建立内联类实现RowMapper接口packagehysteria.contact.dao.impl;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Types;importjava.util.List;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowMapper
  • 标签:
在内部建立内联类实现RowMapper接口
    class="highlighter-j">
  1. package?hysteria.contact.dao.impl;
  2. import?java.sql.ResultSet;
  3. import?java.sql.SQLException;
  4. import?java.sql.Types;
  5. import?java.util.List;
  6. import?org.springframework.jdbc.core.JdbcTemplate;
  7. import?org.springframework.jdbc.core.RowMapper;
  8. import?hysteria.contact.dao.ItemDAO;
  9. import?hysteria.contact.domain.Item;
  10. public?class?ItemDAOImpl?implements?ItemDAO?{
  11. ?private?JdbcTemplate?jdbcTemplate;
  12. ?public?void?setJdbcTemplate(JdbcTemplate?jdbcTemplate)?{
  13. ??this.jdbcTemplate?=?jdbcTemplate;
  14. ?}
  15. ?public?Item?insert(Item?item)?{
  16. ??String?sql?=?"INSERT?INTO?items(user_id,name,phone,email)?VALUES(?,?,?,?)";
  17. ??Object[]?params?=?new?Object[]{item.getUserId(),item.getName(),item.getPhone(),item.getEmail()};
  18. ??int[]?types?=?new?int[]{Types.INTEGER,Types.VARCHAR,Types.CHAR,Types.VARCHAR};
  19. ??jdbcTemplate.update(sql,params,types);
  20. ??return?item;
  21. ?}
  22. ?public?Item?update(Item?item)?{
  23. ??String?sql?=?"UPDATE?items?SET?name?=??,?phone?=??,?email?=???WHERE?id?=??";
  24. ??Object[]?params?=?new?Object[]?{item.getName(),item.getPhone(),item.getEmail(),item.getId()};
  25. ??int[]?types?=?new?int[]?{Types.VARCHAR,Types.CHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER};
  26. ??jdbcTemplate.update(sql,params,types);
  27. ??return?item;
  28. ?}
  29. ?public?void?delete(Item?item)?{
  30. ??String?sql?=?"DELETE?FROM?items?WHERE?id?=??";
  31. ??Object[]?params?=?new?Object[]?{item.getId()};
  32. ??int[]?types?=?new?int[]{Types.INTEGER};
  33. ??jdbcTemplate.update(sql,params,types);
  34. ?}
  35. ?public?Item?findById(int?id)?{
  36. ??String?sql?=?"SELECT?*?FROM?items?WHERE?id?=??";
  37. ??Object[]?params?=?new?Object[]?{id};
  38. ??int[]?types?=?new?int[]?{Types.INTEGER};
  39. ??List?items?=?jdbcTemplate.query(sql,params,types,new?ItemMapper());
  40. ??if(items.isEmpty()){
  41. ???return?null;
  42. ??}
  43. ??return?(Item)items.get(0);
  44. ?}
  45. ?public?List<Item>?findAll()?{
  46. ??String?sql?=?"SELECT?*?FROM?items";
  47. ??return?jdbcTemplate.query(sql,new?ItemMapper());
  48. ?}
  49. ?public?List<Item>?findAllByUser(int?user_id)?{
  50. ??String?sql?=?"SELECT?*?FROM?items?WHERE?user_id?=??";
  51. ??Object[]?params?=?new?Object[]{user_id};
  52. ??int[]?types?=?new?int[]{Types.INTEGER};
  53. ??List?items?=?jdbcTemplate.query(sql,params,types,new?ItemMapper());
  54. ??return?items;
  55. ?}
  56. ?protected?class?ItemMapper?implements?RowMapper?{
  57. ??public?Object?mapRow(ResultSet?rs,?int?rowNum)?throws?SQLException?{
  58. ???Item?item?=?new?Item();
  59. ???item.setId(rs.getInt("id"));
  60. ???item.setUserId(rs.getInt("user_id"));
  61. ???item.setName(rs.getString("name"));
  62. ???item.setPhone(rs.getString("phone"));
  63. ???item.setEmail(rs.getString("email"));
  64. ???return?item;
  65. ??}
  66. ?}
  67. }
上一篇: 关于Java数组的12个最佳方法 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名