第一步: 定义接口: 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 List<T> findCollectionByConditionNoPage(String condition, final Object[] params, Map<String, String> orderby) { //定义hql语句 String hql = "FROM "+entityClass.getSimpleName()+" o WHERE 1=1"; //定义排序语句 String orderbyHql = this.orderbyHql(orderby); //定义最终的语句 final String finalHql = hql + condition + orderbyHql; //执行语句一 //List<T> list = this.getHibernateTemplate().find(finalHql, params); //执行语句二 // SessionFactory sf = this.getHibernateTemplate().getSessionFactory(); // Session s = sf.getCurrentSession(); // Query query = s.createQuery(finalHql); // List<T> list = query.list(); //执行语句三 List<T> 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<params.length;i++){ query.setParameter(i, params[i]); } } return query.list(); } }); return list; } /**组织排序的语句*/ /*ORDER BY o.textDate ASC,o.textName DESC*/ private String orderbyHql(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()+" "+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<ElecText> 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<ElecText> findCollectionByConditionNoPage(ElecText elecText) { //查询条件 String condition = ""; List<Object> paramsList = new ArrayList<Object>(); //判断是否添加查询条件 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<String, String> orderby = new LinkedHashMap<String, String>(); orderby.put("o.textDate", "asc"); orderby.put("o.textName", "desc"); //查询 List<ElecText> 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<ElecText> 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 elecSystemDDL = this.getModel(); @Resource(name=IElecSystemDDLService.SERVICE_NAME) private IElecSystemDDLService elecSystemDDLService; public String home(){ List<ElecSystemDDL> list = elecSystemDDLService.findKeywordWithDistinct(); request.setAttribute("list", list); return "home"; } public String edit(){ //获取数据类型 String keyword = elecSystemDDL.getKeyword(); //1:使用数据类型作为查询条件,查询数据字典表,返回List<ElecSystemDDL> List<ElecSystemDDL> list = elecSystemDDLService.findSystemDDLListByKeyword(keyword); request.setAttribute("systemList", list); return "edit"; } }总结:根据什么样的条件查询,是在service层完成的,把所有的条件组织好后,给dao层,dao层再拼接SQL或者hql语句,进行真正的查询。web层的action只是传递参数,进行简单的调用service层的方法。

如何有效監控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。 1.使用mysqladmin查看連接數。 2.用SHOWGLOBALSTATUS查看查詢數。 3.PMM提供詳細性能數據和圖形化界面。 4.MySQLEnterpriseMonitor提供豐富的監控功能和報警機制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显著差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高級安全性和良好集成性的企業級應用場景下,應選擇SQLServer而不是MySQL。 1)SQLServer提供企業級功能,如高可用性和高級安全性。 2)它與微軟生態系統如VisualStudio和PowerBI緊密集成。 3)SQLServer在性能優化方面表現出色,支持內存優化表和列存儲索引。

mySqlManagesCharacterSetsetSandCollationsyutusututf-8asthEdeFault,允許ConfigurationAtdataBase,table和columnlevels,AndrequiringCarefullageLignmentToavoidMismatches.1)setDefeaultCharactersetTercharactersetEtCollacterSeteTandColletationForAdataBase.2)conformentcollecharactersettersetertersetcollatertersetcollationcollation

MySQL觸發器是與表相關聯的自動執行的存儲過程,用於在特定數據操作時執行一系列操作。 1)觸發器定義與作用:用於數據校驗、日誌記錄等。 2)工作原理:分為BEFORE和AFTER,支持行級觸發。 3)使用示例:可用於記錄薪資變更或更新庫存。 4)調試技巧:使用SHOWTRIGGERS和SHOWCREATETRIGGER命令。 5)性能優化:避免複雜操作,使用索引,管理事務。

在MySQL中創建和管理用戶賬戶的步驟如下:1.創建用戶:使用CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';2.分配權限:使用GRANTSELECT,INSERT,UPDATEONmydatabase.TO'newuser'@'localhost';3.修正權限錯誤:使用REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost';然後重新分配權限;4.優化權限:使用SHOWGRA

MySQL適合快速開發和中小型應用,Oracle適合大型企業和高可用性需求。 1)MySQL開源、易用,適用於Web應用和中小型企業。 2)Oracle功能強大,適合大型企業和政府機構。 3)MySQL支持多種存儲引擎,Oracle提供豐富的企業級功能。

MySQL相比其他關係型數據庫的劣勢包括:1.性能問題:在處理大規模數據時可能遇到瓶頸,PostgreSQL在復雜查詢和大數據處理上表現更優。 2.擴展性:水平擴展能力不如GoogleSpanner和AmazonAurora。 3.功能限制:在高級功能上不如PostgreSQL和Oracle,某些功能需要更多自定義代碼和維護。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Linux新版
SublimeText3 Linux最新版