指定查询条件,查询对应的集合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中組織查詢條件,查詢結(jié)果
??List<ElecText> list = elecTextService.findCollectionByConditionNoPage(electText);
??if(list!=null && list.size()>0){
???for (ElecText text : list) {
????System.out.println(text.toString());
???}
??}
?}
ElecTextServiceImpl.java(service層實現(xiàn)類)
//增刪改的方法:添加:@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>();//存放'?'對應(yīng)的可變參量
??//名稱
??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轉(zhuǎn)換成數(shù)組
??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層)
//指定查詢條件,查詢對應(yīng)的集合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;
??//執(zhí)行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]
出現(xiàn)錯誤的代碼如下:
?//排序
??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;
?
轉(zhuǎn)載于:https://www.cnblogs.com/zjiacun/p/4537223.html
總結(jié)
以上是生活随笔為你收集整理的指定查询条件,查询对应的集合List(单表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uplift 模型开源包(causalm
- 下一篇: guava之cache