国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

實現按條件查詢

來源:懂視網 責編:小采 時間:2020-11-09 15:07:43
文檔

實現按條件查詢

實現按條件查詢:第一步: 定義接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 實現接口的類: Class entityClass =
推薦度:
導讀實現按條件查詢:第一步: 定義接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 實現接口的類: Class entityClass =

第一步: 定義接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 實現接口的類: Class entityClass = TUtils.getTClass(this.getClass());public cl

第一步:

定義接口:

public interface ICommonDao {

List findCollectionByConditionNoPage(String codition,Object[] params, Map orderby);

}

第二步:
實現接口的類:

Class entityClass = TUtils.getTClass(this.getClass());

public class TUtils {

	/**泛型轉換,目的獲取子類傳遞父類的真實類型,也就是T所對應的類型*/
	public static Class getTClass(Class entity) {
	ParameterizedType type = (ParameterizedType)entity.getGenericSuperclass();
	Class entityClass = (Class) type.getActualTypeArguments()[0];
	return entityClass;
	}
} 
/**指定查詢條件查詢對應的結果,返回List(不分頁)*/
	/**
	 * FROM ElecText o WHERE 1=1 #Dao層
	AND o.textName LIKE '%張%'	#Service層
	AND o.textRemark LIKE '%張%' #Service層
	ORDER BY o.textDate ASC,o.textName DESC #Service層
	 */
	public List findCollectionByConditionNoPage(String condition,
	final Object[] params, Map orderby) {
	//定義hql語句
	String hql = "FROM "+entityClass.getSimpleName()+" o WHERE 1=1";
	//定義排序語句
	String orderbyHql = this.orderbyHql(orderby);
	//定義最終的語句
	final String finalHql = hql + condition + orderbyHql;
	//執行語句一
	//List list = this.getHibernateTemplate().find(finalHql, params);
	//執行語句二
//	SessionFactory sf = this.getHibernateTemplate().getSessionFactory();
//	Session s = sf.getCurrentSession();
//	Query query = s.createQuery(finalHql);
//	List list = query.list();
	//執行語句三
	List list = this.getHibernateTemplate().execute(new HibernateCallback() {

	public Object doInHibernate(Session session)
	throws HibernateException, SQLException {
	Query query = session.createQuery(finalHql);
	if(params!=null && params.length>0){
	for(int i=0;i orderby) {
	StringBuffer buffer = new StringBuffer("");
	if(orderby!=null && orderby.size()>0){
	buffer.append(" order by ");
	for(Map.Entry map:orderby.entrySet()){
	buffer.append(map.getKey()+" "+map.getValue()+",");
	}
	//刪除最后一個逗號
	buffer.deleteCharAt(buffer.length()-1);
	}
	return buffer.toString();
	}

下面是根據不同的業務來編寫不同的代碼。

第三步:

service接口:

public interface IElecTextService {
	public static final String SERVICE_NAME = "com.itheima.elec.service.impl.ElecTextServiceImpl";
	
	List findCollectionByConditionNoPage(ElecText elecText);
}

第四步:

service實現

/**指定查詢條件查詢對應的結果,返回List*/
	/**
	 * FROM ElecText o WHERE 1=1 #Dao層
	AND o.textName LIKE '%張%'	#Service層
	AND o.textRemark LIKE '%張%' #Service層
	ORDER BY o.textDate ASC,o.textName DESC #Service層
	 */
	public List findCollectionByConditionNoPage(ElecText elecText) {
	//查詢條件
	String condition = "";
	List paramsList = new ArrayList();
	//判斷是否添加查詢條件
	if(StringUtils.isNotBlank(elecText.getTextName())){
	condition += " and o.textName like ?";
	paramsList.add("%"+elecText.getTextName()+"%");
	}
	if(StringUtils.isNotBlank(elecText.getTextRemark())){
	condition += " and o.textRemark like ?";
	paramsList.add("%"+elecText.getTextRemark()+"%");
	}
	Object [] params = paramsList.toArray();
	//排序語句(hql語句和sql語句的排序是有順序的
	Map orderby = new LinkedHashMap();
	orderby.put("o.textDate", "asc");
	orderby.put("o.textName", "desc");
	//查詢
	List list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);
	return list;
	}


第五步:

/**模擬Action,調用Service,指定查詢條件查詢對應的結果,返回List*/
	@Test
	public void findCollectionByConditionNoPage(){
	ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
	IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);
	
	ElecText elecText = new ElecText();
//	elecText.setTextName("張");
//	elecText.setTextRemark("張");
	
	List list = elecTextService.findCollectionByConditionNoPage(elecText);
	if(list!=null && list.size()>0){
	for(ElecText elecText2:list){
	System.out.println(elecText2.getTextName()+" "+elecText2.getTextDate()+" "+elecText2.getTextRemark());
	}
	}
	}


真正的action:

package com.itheima.elec.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.elec.domain.ElecSystemDDL;
import com.itheima.elec.service.IElecSystemDDLService;


@SuppressWarnings("serial")
@Controller("elecSystemDDLAction")
@Scope(value="prototype")
public class ElecSystemDDLAction extends BaseAction{
	
	ElecSystemDDL elecSystemDDL = this.getModel();
	
	@Resource(name=IElecSystemDDLService.SERVICE_NAME)
	private IElecSystemDDLService elecSystemDDLService;
	public String home(){
	List list = elecSystemDDLService.findKeywordWithDistinct();
	request.setAttribute("list", list);
	return "home";
	}
	
	
	public String edit(){
	//獲取數據類型
	String keyword = elecSystemDDL.getKeyword();
	//1:使用數據類型作為查詢條件,查詢數據字典表,返回List
	List list = elecSystemDDLService.findSystemDDLListByKeyword(keyword);
	request.setAttribute("systemList", list);
	return "edit";
	}
	
}
總結:根據什么樣的條件查詢,是在service層完成的,把所有的條件組織好后,給dao層,dao層再拼接SQL或者hql語句,進行真正的查詢。web層的action只是傳遞參數,進行簡單的調用service層的方法。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

實現按條件查詢

實現按條件查詢:第一步: 定義接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 實現接口的類: Class entityClass =
推薦度:
標簽: 查詢 定義 條件
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

Top
主站蜘蛛池模板: 精品一区二区三区四区在线 | 最新国产精品视频免费看 | 在线视频观看国产 | 91一区二区三区四区五区 | 欧美激情视频一区 | 亚洲国产精品婷婷久久久久 | 小说区 亚洲 自拍 另类 | 午夜精品一区二区三区在线观看 | 欧美成人免费高清二区三区 | 亚洲视频免费一区 | 蜜臀91精品国产高清在线观看 | 伊人色综合一区二区三区 | 精品一区二区三区四区电影 | 99久久精品国产一区二区三区 | 亚洲码欧美码一区二区三区 | 韩国精品在线 | 亚洲欧美一区二区三区不卡 | 亚洲国产一成人久久精品 | 欧美色第一页 | 国内精品视频在线观看 | 国内精品伊人久久久久 | 国产真实乱人视频在线看 | 日韩欧美色 | 中文字幕 日韩有码 | 久久精品国产亚洲精品2020 | 亚洲欧美日韩综合网导航 | 日韩综合在线视频 | 久久国产精品一区二区 | 91中文| 禽交| 日韩亚洲第一页 | 九九久久亚洲综合久久久 | 免费观看一级成人毛片 | 国产成人99久久亚洲综合精品 | 欧美激情免费观看一区 | 日韩精品欧美高清区 | 国产精品视频一区麻豆 | 91欧美在线| 一边摸一边爽一边叫床免费视频 | 一道本一区二区三区 | 日韩欧美一卡二区 |