第一步: 定義接口: public interface ICommonDaoT { ListT findCollectionByConditionNoPage(String codition,Object[] params, MapString, String orderby); } 第二步: 實現接口的類: Class entityClass = TUtils.getTClass(this.getClass());public cl
第一步:
定義接口:
public interface ICommonDao
List
}
第二步:
實現接口的類:
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 ListfindCollectionByConditionNoPage(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"; ListfindCollectionByConditionNoPage(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 ListfindCollectionByConditionNoPage(ElecText elecText) { //查詢條件 String condition = ""; 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("張"); Listlist = 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"; } }
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com