007

如果我只需要获取User 的name 怎么办? 需要在重新创建一个方法 单独获取方法吗?
那可以通过返回User 对象 这种方法 虽然说可以, 但不怎么符合设计规范 我需要什么样的数据, 你应该就返回给我什么数据, 而不是需要我在处理
而且当数据量的时候 会影响网络数据的传输性能

public String findName(int id){    
  String sql = "select name from user where id = ?";    
  Object[] args = new Object[]{id};    
  User user = (User) super.find(sql, args);     
  return user.getName(); 
}

参考下AbstractDao数据返回的 是通过 find 然后 rowMapper返回的 那么我可以在写一个rowMapper2 那怎么让父类的super.find方法中
调用 rowMapper2 呢?

protected String rowMapper2(ResultSet rs) throws SQLException {
return rs.getString("name");
}

回顾下sql 和 数据封装的处理: sql是通过参数传递进去的, 数据的封装是在子类的方法中。 那如果能把数据的分装 方法 也通过参数的传递就可以了
方法是不能通过参数传递的 这时就可以使用接口 通过传递接口的子类实现

RowMapper 接口
1 package com.jdbc.dao.impl;
2 
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 
6 public interface RowMapper {
7     public Object mapRow(ResultSet rs) throws SQLException;
8 }
MyDaoTemplate.java
 1 package com.jdbc.dao.impl;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.jdbc.base.JdbcUtil;
 9 import com.jdbc.exception.DaoException;
10 
11 public class MyDaoTemplate {
12      public Object find(String sql, Object[] args, RowMapper rowMapper){
13         Connection conn = null;
14         PreparedStatement ps = null;
15         ResultSet rs = null;
16         Object obj = null; 
17         try {
18             conn = JdbcUtil.getConnection();
19             ps = conn.prepareStatement(sql);
20             
21             for (int i = 0; i < args.length; i++) {
22                 ps.setObject(i+1, args[i]);
23             }
24             
25             rs = ps.executeQuery();
26             
27             while(rs.next()){
28                 obj = rowMapper.mapRow(rs);
29             }
30         } catch (SQLException e) {
31             throw new DaoException(e.getMessage(), e);
32         } finally {
33             JdbcUtil.free(rs, ps, conn);
34         }
35         return obj;            
36     }
37 
38     public int update(String sql, Object[] args, RowMapper rowMapper) {
39         Connection conn = null;
40         PreparedStatement ps = null;
41         ResultSet rs = null;
42         int count = 0;
43         try {
44             conn = JdbcUtil.getConnection();
45             ps = conn.prepareStatement(sql);
46             
47             for (int i = 0; i < args.length; i++) {
48                 ps.setObject(i+1, args[i]);
49             }
50             
51             count = ps.executeUpdate();
52         } catch (SQLException e) {
53             throw new DaoException(e.getMessage(), e);
54         } finally {
55             JdbcUtil.free(rs, ps, conn);
56         }
57         
58         return count;
59     }
60     
61     public int insert(String sql, Object[] args) {
62         Connection conn = null;
63         PreparedStatement ps = null;
64         ResultSet rs = null;
65         int count = 0;
66         try {
67             conn = JdbcUtil.getConnection();
68             ps = conn.prepareStatement(sql);
69             
70             for (int i = 0; i < args.length; i++) {
71                 ps.setObject(i+1, args[i]);
72             }
73             
74             count = ps.executeUpdate();
75             rs = ps.getGeneratedKeys();
76             if(rs.next()){
77                 return rs.getInt(1);
78             }
79         } catch (SQLException e) {
80             throw new DaoException(e.getMessage(), e);
81         } finally {
82             JdbcUtil.free(rs, ps, conn);
83         }
84         
85         return count;
86     }
87 }

测试代码

 1 MyDaoTemplate template = new MyDaoTemplate();
 2         String sql = "select * from User where name = ?";
 3         Object[] arg = new Object[]{"zhangsan"};
 4         
 5         User u2 = (User) template.find(sql, arg, new RowMapper(){        
 6             @Override
 7             public Object mapRow(ResultSet rs) throws SQLException {
 8                 User user = new User();
 9                 user.setId(rs.getInt("id"));
10                 user.setName(rs.getString("name"));
11                 user.setBirthday(rs.getString("birthday"));
12                 user.setMoney(rs.getDouble("money"));
13                 
14                 return user;
15             }
16             
17         });
原文地址:https://www.cnblogs.com/zi-yao/p/6196118.html