巫师的蜕变

导航

 

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;

 

posted on 2015-05-28 22:49  jacunjpg  阅读(2685)  评论(0编辑  收藏  举报