Java 分頁與多條件查詢

2021-09-24 19:47:02 字數 4510 閱讀 8952

與分頁一樣需要兩條查詢語句分別查詢總行數與分頁後的資料,區別是查詢分頁的時候結尾的)去掉如下:

private string selectsuppliercount=

"select count(*) count from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid = tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid = tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid=tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid"

;private string selectsupplierpags=

"select top(?) * from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid=tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid = tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid=tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid where supplierid not in (select top(?) supplierid from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid = tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid = tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid=tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid"

;

在傳遞過來的引數中所有的引數都為空時執行分頁時直接把)新增回去執行即可,而在多條件中則執行查詢總行數與分頁中都需要拼接字串然後查詢,**如下:

public returnlistjson

selectsupplier

(string name,string englishname,string suppliername,string supplierenglishname,string mainproducts,

int limit,

int page)

else}if

(supplierenglishname!=null&&!""

.equals

(supplierenglishname)

)else}if

(mainproducts!=null&&!""

.equals

(mainproducts)

)else

}int count=0;

try ps = conn.

preparestatement

(selectsupplierpags+

")")

; ps.

setint(1

, limit)

; ps.

setint(2

,(page -1)

* limit);}

else

ps = conn.

preparestatement

(selectsupplierpags+

" where "

+str+

") and "

+str)

; ps.

setint(1

, limit)

; ps.

setint(2

,(page -1)

* limit);}

rs = ps.

executequery()

; list

listselectsuppliervo =

newarraylist

();while

(rs.

next()

) listjson =

newreturnlistjson

(0,""

, count,listselectsuppliervo);}

catch

(sqlexception e)

finally

return listjson;

}

在此方法中宣告的str拼接的字串中當傳遞過來的條件不為空時開始拼接,有條件分頁沒條件分頁的區分條件為str是非為""。拼接後的**如下:

查詢總數**:

select

count(*

) count from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid=tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid=tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid=tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid where suppliername like

'%東莞%'

and supplierenglishname like

'%dongguan%'

分頁**:

select

top(?)

*from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid=tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid=tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid=tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid where supplierid notin(

select

top(?) supplierid from pw_supplier tbsupplier join sys_datasources tbdatasources on tbsupplier.datasourcesid=tbdatasources.datasourcesid join sys_paymentmethod tbpaymentmethod on tbsupplier.paymentmethodid=tbpaymentmethod.paymentmethodid join sys_sourcechannel tbsourcechannel on tbsupplier.sourcechannelid =tbsourcechannel.sourcechannelid join sys_user tbuser on tbsupplier.userid=tbuser.userid where suppliername like

'%東莞%'

and supplierenglishname like

'%dongguan%'

)and suppliername like

'%東莞%'

and supplierenglishname like

'%dongguan%'

效果圖如下:

多條件查詢分頁

分頁實現後,通過多條件查詢後,內容如果需要分頁,容易出現切換頁面內容跳轉到查詢前的內容。解決方法可以通過將查詢後的內容建表,然後在進行分頁。require once wenjian dbda.class.php require once wenjian page.class.php obj sele...

MybatisPlus多條件查詢並分頁

輸入任意字段進行查詢,如果欄位為空,則忽略該欄位,並分頁輸出。public result findmulti string name,string string position,string phone,integer department id,integer now page,integer ...

JPA多條件查詢 分頁 區域性重新整理

使用多條件查詢 實體類 標明為實體類 entity 命名為一張表,如果db中沒有則新增一張表,若有則更新字段,name可以設定表名,若不設定,則與類同名 table name t collection public class collect public long getid public voi...