Ecside整合到SpringSide

Struts基类代码

public ActionForward doQuery(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  // 当列表的分页 过滤 排序等操作是基于数据库时,必须要用到Limit对象.
  // 注意,当页面有多个ec的时候,需要使用带 tableId参数的同名方法.
  // Limit limit=RequestUtils.getLimit(request,"ecGird的Id");
  Limit limit = RequestUtils.getLimit(request);

  // 基于数据库的排序.
  // EC会取得排序的相关信息:当前是按哪个(目前只支持单列排序)column排序的,以及排序的方式desc或asc,
  // 这个信息以key-value方式存放在map里.
  // 但是至于如果处理这些信息(如组装成sql语句),则是由开发人员自己在DAO里完成的.
  Sort sort = limit.getSort();
  Map sortValueMap = sort.getSortValueMap();

  // 基于数据库过滤.
  // EC会取得过滤的相关信息:当前是对哪些column进行过滤,以及过滤的内容是什么,这个信息以key-value方式存放在map里.
  FilterSet filterSet = limit.getFilterSet();
  //Map filterPropertyMap = filterSet.getPropertyValueMap();
  T obcect=entityClass.newInstance();
  bindEntity(form, obcect);
  //filterPropertyMap=BeanUtils.describe(obcect);
  // 在本例中, sort 和 filter 相关信息将被传入 DAO,用于拼装sql语句.
  // 其实,此时的排序 过滤就和我们以前的传统的查询非常类似:从查询页面取得查询条件,传入DAO.
  // RequestUtils.getTotalRowsFromRequest(request);是一个工具类,用来从ECSIDE的列表中取得上次计算出的总行数
  // 如果您不希望每次翻页都重新计算总行数,那么建议参考下面几行代码的做法.
  int totalRows = RequestUtils.getTotalRowsFromRequest(request);
  if (totalRows < 0) {

   // 取得记录总条数时,不要忘了把filter作为参数传入,因为要取得的总行数也是要接受条件限制的.
   //totalRows = getEntityManager().getTotalEntitys(obcect,sortValueMap);
   totalRows = getEntityManager().FindTotalEntitys(obcect,sortValueMap);
  }
  // DEFAULT_PAGE_SIZE ==0 时, 每页记录数会使用 properties文件内的默认设置
  // DEFAULT_PAGE_SIZE <0 时, 每页记录数会等于全部记录数
  limit.setRowAttributes(totalRows, DEFAULT_PAGE_SIZE);
  // 取得当前要查询的页面的记录起止行号
  // offset表示数据编号的起始号,不同的数据库起始号不一样
  int offset = 0;
  int[] rowStartEnd = new int[] { limit.getRowStart() + offset,
    limit.getRowEnd() + offset };
  // rowStartEnd[0], rowStartEnd[1] 左闭 右开
  List list = (List) getEntityManager().FindEntitysForPage(rowStartEnd[0],
    rowStartEnd[1], obcect, sortValueMap);

  request.setAttribute(getEntityListName(), list);
  return mapping.findForward("eclist");

 }

Hibernate 代码

/**
  * ec分页 Query方式 带过滤条件和模糊查询
  *
  * @author DM
  */
 public List<T> FindEntitysForPage(int startRow, int countPerPage, T filter,
   Map order) {

  String sql = " from " + getEntityClass().getName() + " a where 1=1";
  List list = getQuerySql(sql, filter, order);
  sql = list.get(0).toString();
  list.remove(0);
  Object[] objects = list.toArray();
  Query qr = createQuery(sql, objects);
  qr.setFirstResult(startRow);
  qr.setMaxResults(countPerPage - startRow);
  return qr.list();
 }

 /**
  * Query方式 根据查询条件,过滤,得到符合条件的记录总数
  *
  * @param filter
  * @author By Dm
  * @return
  */
 public int FindTotalEntitys(T filter, Map order) {
  int result = 0;
  String sql = "select count(*) from " + getEntityClass().getName()+ " a where 1=1";
  List list = getQuerySql(sql, filter, order);
  sql = list.get(0).toString();
  list.remove(0);
  Object[] objects = list.toArray();
  Query qr = createQuery(sql, objects);

  Integer count = (Integer) qr.uniqueResult();
  if (null == count) {
   result = 0;
  } else {
   result = count.intValue();
  }
  return result;

 }

 // SQL语句自动拼装
 /**
  * @author DM
  */
 public List getQuerySql(String sql, T filter, Map order) {
  List objects = new ArrayList();
  objects.add(sql);
  objects=getSqlByBean(objects, filter, "");
  sql=objects.get(0).toString();
  // 排序
  if (order != null) {
   Set fieldNameSet = order.keySet();
   for (Iterator iterator = fieldNameSet.iterator(); iterator
     .hasNext();) {
    String fieldName = (String) iterator.next();
    if (StringUtils.isNotEmpty(fieldName)) {
     String orderType = order.get(fieldName).toString();
     if ("asc".equalsIgnoreCase(orderType)) {
      sql = sql + "order by " + fieldName + ".asc";
     } else {
      sql = sql + "order by " + fieldName + ".desc";
     }
    }
   }

  }
  objects.set(0, sql);
  return objects;

 }

 /**
  * 通过bean无限遍历SQL语句
  * @author DM
  */
 public List getSqlByBean(List objects, Object filter,
   String BeanName) {
  String sql=objects.get(0).toString();
  BeanUtilsBean beanunit = new BeanUtilsBean();
  PropertyDescriptor origDescriptors[] = beanunit.getPropertyUtils()
    .getPropertyDescriptors(filter);

  for (int i = 0; i < origDescriptors.length; i++) {
   String fieldName = origDescriptors[i].getName();
   Object values = null;
   try {
    values = beanunit.getPropertyUtils().getSimpleProperty(filter,
      fieldName);
    if (fieldName.equals("class")) {
     continue;
    }

    if (values != null) {
     // String类型
     if (values.getClass() == String.class) {
      if (StringUtils.isNotEmpty(values.toString())) {
       sql = sql + " and ( a." + BeanName + fieldName + " like  ?  )";
       objects.add("%" + values + "%");
       objects.set(0, sql);
      }
      // 时间类型
     } else if (values.getClass() == Date.class) {
      if(fieldName.endsWith("Start")){
      fieldName = fieldName.substring(0, fieldName
        .length() - 5);
      sql = sql + " and ( a." + BeanName + fieldName
        + " between ? and  ?  )";
      objects.add(values);
      Date endValues=(Date) beanunit.getPropertyUtils().getSimpleProperty(filter, fieldName + "End");
      if(endValues==null){
       endValues=new Date();
      }
      objects.add(endValues);
      objects.set(0, sql);
         }else if(fieldName.endsWith("End")){
       fieldName = fieldName.substring(0, fieldName
         .length() - 3);
       sql = sql + " and ( a." + BeanName + fieldName
         + " between ? and  ?  )";
       Date SrartValues=(Date) beanunit.getPropertyUtils().getSimpleProperty(filter, fieldName + "Start");
       if(SrartValues==null){
        DateConverter dc=new DateConverter();
        SrartValues=(Date) dc.convert(Date.class, "2000-01-01");
       }
       objects.add(SrartValues);
       objects.add(values);
       objects.set(0, sql);
         }
//       Long类型
      } else if (values.getClass() == java.lang.Long.class) {
      sql = sql + " and ( a." + BeanName + fieldName + " = ? ) ";
      objects.add(values);
      objects.set(0, sql);
      // Set类型
     } else if (values.getClass() == java.util.HashSet.class) {
      continue;
      // Integer类型
     }else if (values.getClass() == java.lang.Integer.class) {
//      需求自己添加
      continue;
      // Boolean类型
     }else if(values.getClass() == java.lang.Boolean.class){
      //需求自己添加
      continue;
     }
      else {
      BeanName = fieldName + "." + BeanName;
      getSqlByBean(objects, values, BeanName);
     }
    }
   } catch (IllegalAccessException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   } catch (InvocationTargetException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   } catch (NoSuchMethodException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }

  }
  
  return objects;
 }

你可能感兴趣的:(DAO,sql,Hibernate,bean,struts)