日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 指定查询条件,查询对应的集合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;

?

轉載于:https://www.cnblogs.com/zjiacun/p/4537223.html

總結

以上是生活随笔為你收集整理的指定查询条件,查询对应的集合List(单表)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。