• 指定查询条件,查询对应的集合List(单表)


    TestDao.java(测试类)

    @Test
     public void findCollectionByConditionNoPage(){
      ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
      IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);
      
      //封装查询条件
      ElecText electText = new ElecText();
      electText.setTextName("李");
      electText.setTextRemark("李");
      //在service中组织查询条件,查询结果
      List<ElecText> list = elecTextService.findCollectionByConditionNoPage(electText);
      if(list!=null && list.size()>0){
       for (ElecText text : list) {
        System.out.println(text.toString());
       }
      }
     }

    ElecTextServiceImpl.java(service层实现类)

    //增删改的方法:添加:@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
     //查询的方法:不需要添加
     /**
      * SELECT * FROM elec_text o WHERE 1=1
      AND o.textName LIKE '%李%'
      AND o.textRemark LIKE '%李%'
      ORDER BY o.textDate DESC,o.textName ASC
      */
     public List<ElecText> findCollectionByConditionNoPage(ElecText electText) {
      //组织查询条件
      String condition = "";
      List<Object> paramsList = new ArrayList<Object>();//存放'?'对应的可变参量
      //名称
      String textName = electText.getTextName();
      if(StringUtils.isNotBlank(textName)){
       condition += " AND o.textName LIKE ?";
       paramsList.add("%"+textName+"%");
      }
      //备注
      String textRemark = electText.getTextRemark();
      if(StringUtils.isNotBlank(textRemark)){
       condition += " AND o.textRemark LIKE ?";
       paramsList.add("%"+textRemark+"%");
      }
      //将paramsList转换成数组
      Object [] params = paramsList.toArray();
      //排序
      Map<String, String> orderby = new LinkedHashMap<String, String>();
      orderby.put("o.textDate", "desc");
      orderby.put("o.textName", "asc");
      List<ElecText> list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);
      return list;
     }

    CommonDaoImpl.java(底层方法封装CommonDaoImpl类,Dao层)

    //指定查询条件,查询对应的集合List(单表)
     /**
      * SELECT * FROM elec_text o WHERE 1=1
      AND o.textName LIKE '%李%'
      AND o.textRemark LIKE '%李%'
      ORDER BY o.textDate DESC,o.textName ASC
      */
     public List<T> findCollectionByConditionNoPage(String condition,
       Object[] params, Map<String, String> orderby) {
      String hql = " FROM "+entityClass.getSimpleName()+" o WHERE 1=1 ";
      //ORDER BY o.textDate DESC,o.textName ASC
      String orderbyHql = orderby(orderby);
      String finalHql = hql + condition + orderbyHql;
      //执行hql语句
      List<T> list = this.getHibernateTemplate().find(finalHql,params);
      return list;
     }
     
     //解析map集合,获取orderby的排序条件
     private String orderby(Map<String, String> orderby){
      StringBuffer buffer = new StringBuffer("");
      if(orderby!=null && orderby.size()>0){
       buffer.append(" ORDER BY ");
       for(Map.Entry<String, String> map:orderby.entrySet()){
        buffer.append(map.getKey()).append(map.getValue()).append(",");
       }
       //删除最后一个逗号
       buffer.deleteCharAt(buffer.length()-1);
      }
      return buffer.toString();
     }

    Service层下orderby.put("o.textDate ", "desc");   不加空格会报错

    错误提示如下:

    Caused by: org.hibernate.QueryException: could not resolve property: textDatedesc of: cn.itcast.elec.domain.ElecText [ FROM cn.itcast.elec.domain.ElecText o WHERE 1=1  AND o.textName LIKE ? AND o.textRemark LIKE ? ORDER BY o.textDatedesc,o.textNameasc]

    出现错误的代码如下:

     //排序
      Map<String, String> orderby = new LinkedHashMap<String, String>();
      orderby.put("o.textDate", "desc");
      orderby.put("o.textName", "asc");
      List<ElecText> list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);
      return list;

  • 相关阅读:
    【Lucene4.8教程之五】Luke
    【Tika基础教程之一】Tika基础教程
    【Lucene4.8教程之四】分析
    【Lucene4.8教程之六】QueryParser与Query子类:如何生成Query对象
    【Lucene4.8教程之三】搜索
    Java路径问题最终解决方案—可定位所有资源的相对路径寻址
    java.util.logging.Logger基础教程
    【Lucene4.8教程之二】索引
    【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作
    【Heritrix基础教程之4】开始一个爬虫抓取的全流程代码分析
  • 原文地址:https://www.cnblogs.com/zjiacun/p/4537223.html
Copyright © 2020-2023  润新知