Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2545615
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: Java

2018-04-17 13:31:20


点击(此处)折叠或打开

  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;

  10. /**
  11.  * object: QueryCountDate
  12.  * author: 程晓鹏
  13.  * description: 查询日统计数据
  14.  * date: 2018-04-17 09:31
  15.  */
  16. public class QueryCountDate<MODEL extends ICountDate> implements IQueryCountDate<MODEL> {

  17.     /**
  18.      * 默认构造函数
  19.      */
  20.     public QueryCountDate(){

  21.     }

  22.     @Override
  23.     public List<MODEL> queryCountDate(Class<? extends ICountDate> cls, String startDate, String endDate) {
  24.         List<MODEL> result = new ArrayList<MODEL>();
  25.         BatchData sqlData = this.getQuerySql(cls,startDate,endDate);
  26.         DBHelper db = new DbcpHelper("bd");
  27.         ResultSet rs = db.executeQuery(sqlData.sql,sqlData.param); //执行查询
  28.         try {
  29.             while (rs.next()) {
  30.                 MODEL model = this.getInstance(cls); //创建一个对象
  31.                 for(Map.Entry<String, String> entry: model.getColumn2Filed().entrySet()) {
  32.                     String column = entry.getKey(); //bd_count_day表中count_attribute中的值
  33.                     String fieldName = entry.getValue(); //类的成员变量名
  34.                     String dbvalue = rs.getString(column); //数据库中的值
  35.                     try {
  36.                         Field field = model.getClass().getDeclaredField(fieldName);
  37.                         field.setAccessible(true);
  38.                         field.set(model, dbvalue); //给成员变量,进行赋值
  39.                     } catch (NoSuchFieldException e) {
  40.                         e.printStackTrace();
  41.                     }catch (IllegalAccessException e) {
  42.                         e.printStackTrace();
  43.                     }
  44.                 }
  45.                 String countDate = rs.getString("count_date"); //获取统计日期数据
  46.                 try {
  47.                     Method method = model.getClass().getMethod("setCountDate",String.class);
  48.                     method.invoke(model, countDate); //调用设置统计日期方法
  49.                 } catch (NoSuchMethodException e) {
  50.                     e.printStackTrace();
  51.                 }catch (IllegalAccessException e) {
  52.                     e.printStackTrace();
  53.                 } catch (InvocationTargetException e) {
  54.                     e.printStackTrace();
  55.                 }
  56.                 result.add(model);
  57.             }
  58.             rs.close();
  59.         }catch (SQLException e) {
  60.             e.printStackTrace();
  61.         }finally{
  62.             db.Close();
  63.         }


  64.         return result;
  65.     }

  66.     public List<MODEL> queryCountDate2(Class<? extends ICountDate> cls, String startDate, String endDate) {
  67.         String[] columnArray = this.getCloumnArray(cls);
  68.         Map<String,Map<String,String>> data = this.queryCountDate(columnArray,startDate,endDate);
  69.         return this.formatQueryResult(cls,data);
  70.     }

  71.     public Map<String,Map<String,String>> queryCountDate(String[] columns, String startDate, String endDate) {
  72.         Map<String,Map<String,String>> result = new HashMap<String, Map<String, String>>();
  73.         BatchData sqlData = this.getQuerySql(columns,startDate,endDate);
  74.         DBHelper db = new DbcpHelper("bd");
  75.         ResultSet rs = db.executeQuery(sqlData.sql,sqlData.param); //执行查询
  76.         try {
  77.             while (rs.next()) {
  78.                 Map<String,String> item = new HashMap<String, String>(); //创建一个子map
  79.                 for(int i=0;i<columns.length;i++) {
  80.                     String column = columns[i]; //bd_count_day表中count_attribute中的值
  81.                     String dbvalue = rs.getString(column); //数据库中的值
  82.                     item.put(column,dbvalue);
  83.                 }
  84.                 String countDate = rs.getString("count_date"); //获取统计日期数据
  85.                 result.put(countDate,item);
  86.             }
  87.             rs.close();
  88.         }catch (SQLException e) {
  89.             e.printStackTrace();
  90.         }finally{
  91.             db.Close();
  92.         }


  93.         return result;
  94.     }

  95.     private BatchData getQuerySql(Class<? extends ICountDate> cls, String startDate, String endDate){
  96.         String[] array = this.getCloumnArray(cls);
  97.         return this.getQuerySql(array, startDate,endDate);
  98.     }

  99.     public String[] getCloumnArray(Class<? extends ICountDate> cls){
  100.         String[] result = null;
  101.         MODEL model = this.getInstance(cls);
  102.         result = new String[model.getColumn2Filed().size()];
  103.         int i=0;
  104.         for(Map.Entry<String, String> entry: model.getColumn2Filed().entrySet()) {
  105.             result[i++] = entry.getKey();
  106.         }

  107.         return result;
  108.     }

  109.     private BatchData getQuerySql(String[] columns, String startDate, String endDate){
  110.         BatchData batchData = new BatchData();
  111.         StringBuilder sb = new StringBuilder();
  112.         String[] param = new String[columns.length+2];
  113.         sb.append("select distinct ");
  114.         int i=0;
  115.         for(; i<columns.length;i++) {
  116.             String column = columns[i]; //bd_count_day表中count_attribute中的值
  117.             sb.append(" IFNULL((select tb"+String.valueOf(i+1)+".attribute_value from bd_count_day tb"+String.valueOf(i+1)+" ");
  118.             sb.append(" where tb"+String.valueOf(i+1)+".count_date = tb0.count_date");
  119.             sb.append(" and tb"+String.valueOf(i+1)+".customer_id = tb0.customer_id ");
  120.             sb.append(" and tb"+String.valueOf(i+1)+".count_attribute = ?), '') "+ column +", ");
  121.             param[i] = column;
  122.         }
  123.         sb.append("tb0.count_date count_date ");
  124.         sb.append("from bd_count_day tb0 ");
  125.         sb.append("where tb0.count_date >= ? and tb0.count_date <= ?");
  126.         param[i++] = startDate;
  127.         param[i++] = endDate;
  128.         batchData.sql = sb.toString();
  129.         batchData.param = param;
  130.         return batchData;
  131.     }

  132.     /**
  133.      * 获取类的对象实例
  134.      * @param cls
  135.      * @return
  136.      */
  137.     private MODEL getInstance(Class<? extends ICountDate> cls){
  138.         MODEL result = null;
  139.         try {
  140.             result = (MODEL)cls.newInstance();
  141.         } catch (InstantiationException e) {
  142.             e.printStackTrace();
  143.         } catch (IllegalAccessException e) {
  144.             e.printStackTrace();
  145.         }
  146.         return result;
  147.     }

  148.     /**
  149.      * 格式化查询结果
  150.      * @param cls 实现ICountDate接口的类名
  151.      * @param data 要格式化的数据
  152.      * @return MODEL泛型对象集合
  153.      */
  154.     public List<MODEL> formatQueryResult(Class<? extends ICountDate> cls, Map<String,Map<String,String>> data){
  155.         List<MODEL> result = null;
  156.         if(data != null && data.size()>0){
  157.             result = new ArrayList<MODEL>();
  158.             for(Map.Entry<String, Map<String,String>> entry: data.entrySet()) {
  159.                 MODEL model = this.getInstance(cls);
  160.                 this.setCountDate(model,entry.getKey()); //设置日期
  161.                 for(Map.Entry<String,String> mapItem: entry.getValue().entrySet()){
  162.                     this.setAttribute(model,mapItem.getKey(),mapItem.getValue());
  163.                 }
  164.                 result.add(model);
  165.             }
  166.         }

  167.         return result;
  168.     }

  169.     private void setAttribute(MODEL model, String attrCode, String attrValue){
  170.         try {
  171.             String fieldName = model.getColumn2Filed().get(attrCode);
  172.             Field field = model.getClass().getDeclaredField(fieldName);
  173.             field.setAccessible(true);
  174.             field.set(model, attrValue); //给成员变量,进行赋值
  175.         } catch (NoSuchFieldException e) {
  176.             e.printStackTrace();
  177.         }catch (IllegalAccessException e) {
  178.             e.printStackTrace();
  179.         }
  180.     }

  181.     private void setCountDate(MODEL model, String countDate){
  182.         try {
  183.             Method method = model.getClass().getMethod("setCountDate",String.class);
  184.             method.invoke(model, countDate); //调用设置统计日期方法
  185.         } catch (NoSuchMethodException e) {
  186.             e.printStackTrace();
  187.         }catch (IllegalAccessException e) {
  188.             e.printStackTrace();
  189.         } catch (InvocationTargetException e) {
  190.             e.printStackTrace();
  191.         }
  192.     }
  193. }

阅读(4021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~