<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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
        主站蜘蛛池模板: 黄网站色视频免费观看45分钟| 亚洲中文字幕AV在天堂| 亚洲国产aⅴ成人精品无吗| 成人黄色免费网址| 亚洲免费视频网址| 午夜视频免费成人| 亚洲av无一区二区三区| 免费h成人黄漫画嘿咻破解版| 看Aⅴ免费毛片手机播放| 国产成人毛片亚洲精品| 中文字幕免费人成乱码中国| 亚洲av最新在线网址| 999任你躁在线精品免费不卡| 综合自拍亚洲综合图不卡区| 91频在线观看免费大全| 亚洲综合av一区二区三区不卡| 全免费a级毛片免费看不卡| 国产成人亚洲综合无| 中文字幕精品亚洲无线码一区应用| 中文字幕乱理片免费完整的| 久久亚洲AV成人无码| 在线jyzzjyzz免费视频| 黄色a三级免费看| 亚洲国产精品免费视频| 免费无码AV电影在线观看| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲欧美国产国产综合一区| 国产又粗又猛又爽又黄的免费视频| 一级毛片视频免费| 亚洲欧洲精品久久| 日韩免费福利视频| 日韩精品无码免费专区午夜| xxx毛茸茸的亚洲| 亚洲成人高清在线| 色猫咪免费人成网站在线观看 | 亚洲精品在线免费看| 国产成人免费手机在线观看视频 | 免费黄色网址入口| 国产黄色免费观看| 亚洲精品二三区伊人久久| 亚洲av午夜精品一区二区三区|