DAO

 优化JDBC代码(模板模式)
 
优化查询操作,区分开变化和不变的部分:
sql和ResultSet的处理是变化部分,创建和释放资源部分是不变部分。
l提取超类,将不变部分放入超类,变化部分留给子类实现。
l超类的主要代码:
public Object find(String sql, Object[] args) {
  …
  rs = ps.executeQuery();
  if (rs.next()) return rowMapper(rs);
  …
}
protected abstract Object rowMapper(ResultSet rs);
 

public abstract class AbstractDao {
 protected abstract Object rowMapper(ResultSet rs)throws SQLException;
 public Object find(String sql,Object[] args) {
  Object obj=null;
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  try {
//   建立连接
//   conn=JdbcUtils.getConnection();
   conn=JdbcUtilsSing.getInstance().getConnection();//使用单例
   ps=conn.prepareStatement(sql);
   for(int i=0;i<args.length;i++){
    ps.setObject(i+1, args[i]);
   }
   rs=ps.executeQuery();
   if(rs.next()){
    obj=rowMapper(rs);
   }
  } catch (Exception e) {
   //对于异常,能处理就处理,不能处理就抛出,不能隐藏
   //在不污染接口的情况下通知上一层,给上层选择处理的权力
   throw new DaoException(e.getMessage(),e);
  }finally{
//   释放资源
   JdbcUtils.free(rs, ps, conn);
  }
  return obj;
 }
 public void update(String sql,Object[] args) {
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   // 建立连接
   // conn=JdbcUtils.getConnection();
   conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
   ps = conn.prepareStatement(sql);
   for(int i=0;i<args.length;i++){
    ps.setObject(i+1, args[i]);
   }
   ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   // 释放资源
   JdbcUtils.free(rs, ps, conn);
  }
 }
}

------------------------------------------------------------------------

public class UserDaoImpl extends AbstractDao {
 public void delete(User user) {
   String sql="delete from user where id=?";
   Object[] args=new Object[]{user.getId()};
   super.update(sql, args);
 }
 public void update(User user) {
  String sql = "update user set name=?,birthday=?,money=? where id=?";
  Object[] args = new Object[] { user.getName(), user.getBirthday(),
    user.getMoney(), user.getId() };
  super.update(sql, args);
 }
 @Override
 protected Object rowMapper(ResultSet rs) throws SQLException {
  User user;
  user=new User();
  user.setId(rs.getInt("id"));
  user.setName(rs.getString("name"));
   user.setMoney(rs.getFloat("money"));
  user.setBirthday(rs.getDate("birthday"));
  return user;
 }
 public User finUser(String loginName, String password) { 
   String sql="select id,name,birthday,money from user where name=?";
   Object[] args=new Object[]{loginName};
   Object user=super.find(sql,args);
   return (User)user;
 }
}

------------------------------------------------------------------------------------------------------

策略模式(Strategy):定义一系列的算法,将每个算法封装到具有共同接口的独立类中,以便使得算法可以相互替换,而不影响客户端。

组合优化JDBC代码(策略模式)
 
优化查询操作,区分开变化和不变的部分:
sql和ResultSet的处理是变化部分,创建和释放资源部分是不变部分。
提取接口封装变化部分。
JDBC操作主要代码:
public Object find(String sql, Object[] args, RowMapper rowMapper) {
rs = ps.executeQuery();
if (rs.next())return rowMapper.mapRow(rs);
}
回调接口:
public interface RowMapper {
public Object mapRow(ResultSet rs) throws SQLException;
}
 

public interface RowMapper {
 public Object mapRow(ResultSet rs)throws SQLException;
}

-----------------------------------------------------------------------------------------

public class MyDaoTemplate {
 protected Object rowMapper(ResultSet rs) throws SQLException {
  return null;
 }
 public Object find(String sql, Object[] args, RowMapper rowMapper) {
  Object obj = null;
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   // 建立连接
   // conn=JdbcUtils.getConnection();
   conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
   ps = conn.prepareStatement(sql);
   for (int i = 0; i < args.length; i++) {
    ps.setObject(i + 1, args[i]);
   }
   rs = ps.executeQuery();
   if (rs.next()) {
    obj = rowMapper.mapRow(rs);
   }
  } catch (Exception e) {
   // 对于异常,能处理就处理,不能处理就抛出,不能隐藏
   // 在不污染接口的情况下通知上一层,给上层选择处理的权力
   throw new DaoException(e.getMessage(), e);
  } finally {
   // 释放资源
   JdbcUtils.free(rs, ps, conn);
  }
  return obj;
 }
 public void update(String sql, Object[] args) {
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   // 建立连接
   // conn=JdbcUtils.getConnection();
   conn = JdbcUtilsSing.getInstance().getConnection();// 使用单例
   ps = conn.prepareStatement(sql);
   for (int i = 0; i < args.length; i++) {
    ps.setObject(i + 1, args[i]);
   }
   ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   // 释放资源
   JdbcUtils.free(rs, ps, conn);
  }
 }
}

------------------------------------------------------------------------------------------------------

public class UserDaoImpl2{
 MyDaoTemplate template=new MyDaoTemplate();
 public User findUser(String loginName, String password) { 
   String sql="select id,name,birthday,money from user where name=?";
   Object[] args=new Object[]{loginName};
   Object user=this.template.find(sql, args, new UserRowMapper());
   return (User) user;
 }
 public String finderUserName(int id){
  String sql="select name from user when id=?";
  Object[] args=new Object[]{id};
  Object name=this.template.find(sql, args, new RowMapper(){
  public Object mapRow(ResultSet rs) throws SQLException{
   return rs.getString("name");
  }});
  return (String)name;
 }
 class UserRowMapper implements RowMapper{
  @Override
  public Object mapRow(ResultSet rs) throws SQLException {
   User user;
   user=new User();
   user.setId(rs.getInt("id"));
   user.setName(rs.getString("name"));
    user.setMoney(rs.getFloat("money"));
   user.setBirthday(rs.getDate("birthday"));
   return user;
  }
 }
}

posted @ 2011-12-17 15:47  残星  阅读(406)  评论(0编辑  收藏  举报