几个经常用到的通用Dorado基础类

 

关键字: dorado
1.Dorado中虽然提供了ViewModel的接口, 但是这是一个非常"肥"的接口, 对于客户程序来说很多东东都是用不着的, 于是将经常用到的方法进行了一下封装, 创建了一个更小的接口以便能将viewmodel的功能传递到其他地方使用, 当然出于某些需要, 又加了几个经常需要的方法.
Java代码 复制代码
  1. /**  
  2.  * 将ViewModel中的getDateset()等方法抽取出来,以便在utils类的静态方法中调用ViewModel的getDataset()等方法  
  3.  *   
  4.  * @author Macro Chen  
  5.  * @since May 6, 2008  
  6.  */  
  7. public interface IViewModelProvider extends IProvider {   
  8.     public ViewDataset getDataset(String datasetId);   
  9.   
  10.     public Control getControl(String controlId) throws Exception;   
  11.   
  12.     public int getState();   
  13.   
  14.     public boolean isViewState();   
  15.   
  16.     public boolean isViewState(int state);   
  17.   
  18.     public MetaData properties();   
  19. }  


2.下面的两个类一般联合起来和DBStatement的query(), queryForList()方法一起使用, 用来将通过jbbc取得的VariantSet类型的记录集转换成JavaBean类型的记录集
Java代码 复制代码
  1. /**  
  2.  *   
  3.  * 封装创建DBStatement,给statement的参数赋值和关闭DBStatement操作 具体是查询还是更新操作由子类实现  
  4.  *   
  5.  * @author Macro Chen  
  6.  * @Dec 20, 2006  
  7.  *   
  8.  */  
  9. public abstract class SqlExecutor {   
  10.   
  11.     /**  
  12.      * 公共操作(获取Statement和关闭之)  
  13.      *   
  14.      * @param sql  
  15.      * @return  
  16.      * @throws Exception  
  17.      */  
  18.     public Object execute(String sql) throws Exception {   
  19.         DBStatement stmt = DoradoUtils.getStatement(sql);   
  20.         try {   
  21.             ParameterSet p = stmt.parameters();   
  22.             return extract(stmt, p);   
  23.         } finally {   
  24.             stmt.close();   
  25.         }   
  26.     }   
  27.   
  28.     /**  
  29.      * 子类需要实现的CURD操作  
  30.      *   
  31.      * @param stmt  
  32.      * @param p  
  33.      * @return  
  34.      * @throws Exception  
  35.      */  
  36.     public abstract Object extract(DBStatement stmt, ParameterSet p) throws Exception;   
  37. }  


Java代码 复制代码
  1. /**  
  2.  * 用来将VariantSet转换成JavaBean的接口  
  3.  *   
  4.  * @author Macro Chen  
  5.  * @Dec 18, 2006  
  6.  *  
  7.  */  
  8. public interface RowConverter {   
  9.     public Object convert(VariantSet vs) throws SQLException;   
  10. }  


3.DBStatement是Dorado中最常用的一个类, StatementDelegate这个封装类貌似用处不大, 本来主要是为了用方法的连写,后来发现经过格式化的连写代码比较难看.
Java代码 复制代码
  1. /**  
  2.  * 对DBStatement做进一步的代理, 让设置参数的查询和更新更方便, 支持方法的连写  
  3.  *   
  4.  * @author Macro Chen  
  5.  * @since Jun 19, 2008  
  6.  */  
  7. public class StatementDelegate extends DBStatement {   
  8.     public DBStatement getDBStatement() {   
  9.         return this;   
  10.     }   
  11.   
  12.     public StatementDelegate addParameter(String name, Long value) {   
  13.         if (value == null) {   
  14.             this.parameters().setValue(name, value);   
  15.         } else {   
  16.             this.parameters().setLong(name, value);   
  17.         }   
  18.         return this;   
  19.     }   
  20.   
  21.     public StatementDelegate addParameter(String name, Double value) {   
  22.         this.parameters().setDouble(name, value);   
  23.         return this;   
  24.     }   
  25.   
  26.     public StatementDelegate addParameter(String name, String value) {   
  27.         this.parameters().setString(name, value);   
  28.         return this;   
  29.     }   
  30.   
  31.     public StatementDelegate addParameter(String name, Integer value) {   
  32.         if (value == null) {   
  33.             this.parameters().setValue(name, value);   
  34.         } else {   
  35.             this.parameters().setInt(name, value);   
  36.         }   
  37.         return this;   
  38.     }   
  39.   
  40.     public StatementDelegate setDateTime(String fieldName) {   
  41.         DoradoUtils.setDateTime(parameters(), fieldName);   
  42.         return this;   
  43.     }   
  44.   
  45.     public StatementDelegate setPrimaryKey(long primaryKey) {   
  46.         addParameter("id", primaryKey);   
  47.         return this;   
  48.     }   
  49.   
  50.     public StatementDelegate setPrimaryKey() {   
  51.         long primaryKey = IdentifierUtils.getPrimaryKey();   
  52.   
  53.         addParameter("id", primaryKey);   
  54.         return this;   
  55.     }   
  56. }  


4.这个是我比较常用到的一个类, 用了一个回调的模式, 用来处理在一个sql操作中调用其他的sql操作.后来发现它还有一个妙用, 就是封装一个不完全的查询, 丢到另一个查询中使用, 并注入剩下的查询条件
Java代码 复制代码
  1. /**  
  2.  * 为了处理在调用setSql()的时候关闭正在使用的DBStatement, 将创建DBStatement延迟到使用的时候  
  3.  *   
  4.  * @author Macro Chen  
  5.  * @since Jun 11, 2008  
  6.  */  
  7. public abstract class DBStatementCallback {   
  8.   
  9.     public DBStatement getDBStatement() throws Exception {   
  10.         DBStatement stmt = DoradoUtils.getDBStatement(getSql());   
  11.         setParameters(stmt.parameters());   
  12.         return stmt;   
  13.     }   
  14.   
  15.     protected abstract String getSql();   
  16.   
  17.     protected abstract void setParameters(ParameterSet parameters);   
  18. }  


Java代码 复制代码
  1. /**  
  2.  * 针对只设置sql, 不设置parameter的情况  
  3.  * @author Macro Chen  
  4.  * @since Jun 11, 2008  
  5.  */  
  6. public abstract class SimpleDBStatementCallback extends DBStatementCallback {   
  7.     @Override  
  8.     protected void setParameters(ParameterSet parameters) {   
  9.     }   
  10.   
  11. }  
原文地址:https://www.cnblogs.com/linsond/p/1674406.html