指定查詢條件,查詢對應的集合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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM