簡單SQL查詢元件 整理

2021-06-29 11:54:48 字數 1863 閱讀 7761

相信大家在業務非常複雜,客戶要求非常頭疼的情況下寫的查詢sql語句也是一種非常痛苦事了,還好我跟老師做的專案中要求不是很多,差不多就5,6個樣子,但是就是這5,6個樣子也把我累得像狗。剛開始的時候想到的方案是寫if/else語句,條件少點還過得去,但是條件多了就不行。因此,這次介紹乙個簡單查詢元件的編寫(所編寫的hql語句應用於hibernate中,當然你可以改一下用到sql語句中)。

作為一條完整的hql語句,應該包括 select [condition] from classname alias where [....]

order by [....].那麼我們可以將hql語句簡單分解為三個部分fromclause,whereclause和orderclause三個組成部分。我們需要做的是就是拼接fromclause+whereclause+orderclause.好了,上**:

/**

* 簡單查詢元件

* @author administrator

*/public class queryhelper

/*** 新增where字句,注意空格!!

* @param condition

* @param paramters

*/public queryhelper addwherecondition(string condition , object...paramters)else

//新增引數

for(object obj : paramters)

return this ; }

public queryhelper addwherecondition(boolean iswhere , string condition , object...paramters)

/***新增orderby引數,注意空格

* @param orderby

* @param order true為公升序 false為降序

*/public queryhelper addorderbycondition(string orderby , boolean order)else

return this ;

} //計算記錄的條數,這是基本上是需求中要求最多的。

public string gettotalcount()

public queryhelper addorderbycondition(boolean isorder , string orderby , boolean order)

public listgetlistobject()

public string getquery()

}

以上就是分頁**了,好了,怎麼使用呢,來看下面的例子:

假設存在乙個user使用者,需要查詢乙個姓zhang的,成績(抱歉我還是學生,只能這麼幼稚啊)大於90分的,按照id公升序,然後name降序的查詢語句:

@test

public void testuser()

那麼輸出結果可以看到:

from  user   user	where  user.name like  ?  and  user.grade > ? order by  user.id  asc   , user.name  desc  

select count(*) from user user where user.name like ? and user.grade > ?

['%zhang%', 90]

呵呵,我們非常不爽的**就完全通過工具實現了。這樣就可以少點抱怨多用心學習了啊。

ps:這可能是對於單個專案定製的,你完全可以通過自己想象的空間,去建立更多優秀的**的。。。

簡單常用sql查詢

self.db executeupdate sql,record.recordid create table scene record id text primary key,record time text,location text,place text,description text,isu...

Mysql select,資料簡單查詢整理總結

一,簡單的資料查詢 select from table 查詢表單中的所有字段資料 select col1,col2,col3 from table 返回指定的字段資料 二,避免資料重複查詢 演示資料表 tablename user 使用關鍵字 distinct 原始資料 sql select dis...

簡單的sql查詢優化

1 sql中禁止用select from 的寫法 缺點 將會增加服務區io的輸出負擔 2 如果乙個sql比較複雜 則寫完的sql 可以用 在plsql 中用f5 來看起執行計畫,例如sql select from exp booking where booking id like 123123123...