比如我們有一個(gè)訂單記錄管理界面
條件可以通過(guò)訂單號(hào)、商品名稱、創(chuàng)建日期范圍、價(jià)格范圍。。。來(lái)進(jìn)行篩選查詢。首先我們先確定數(shù)據(jù)庫(kù)訂單表(我這里就不做連表了,都放在一個(gè)表中)模擬一個(gè)訂單表
order表
訂單號(hào) ?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-639288.html ? |
商品名稱 ? ? |
創(chuàng)建日期 ? ? |
價(jià)格 ? ? |
地址 ? ? |
用戶 ? ? |
121 ? |
飛機(jī) ? |
2023-03-23 ? |
199 ? |
bj ? |
xxx ? |
212 ? |
大炮 ? |
2023-04-23 ? |
29 ? |
bj ? |
xxx ? |
就比如我們有一個(gè)這樣的訂單表,
?
前端的話說(shuō)一下思路,就不做演示了,我們將查詢的每個(gè)字段當(dāng)作參數(shù)傳遞給后端(我們可以通過(guò)order實(shí)體類來(lái)接受,此時(shí)要注意前端字段命名問(wèn)題了,但是時(shí)間和價(jià)格都是一個(gè)范圍,包括開始時(shí)間-結(jié)束時(shí)間,我們實(shí)體類中沒(méi)有這個(gè)字段,此時(shí)我們就需要添加一
個(gè)DTO來(lái)繼承那個(gè)實(shí)體類,在DTO中添加這幾個(gè)實(shí)體類中沒(méi)有的但是我們查詢過(guò)程中會(huì)用到的字段,通過(guò)DTO來(lái)接受參數(shù),注意:前端通過(guò)JSON傳入后端,
后端通過(guò)@RequestBody接收)
如果不懂怎么寫的可以去看下我的這個(gè)文章前后端交互問(wèn)題
如果沒(méi)什么特殊的業(yè)務(wù)操作我們的業(yè)務(wù)層和控制層基本
不用寫代碼(記得時(shí)間要格式化);
實(shí)現(xiàn)條件查詢做重要的的部分來(lái)了也就是我們的sql語(yǔ)句
這里我們使用mybatis的 .xml配置文件來(lái)寫sql
這里sql中就用到動(dòng)態(tài)拼接的方法,也就是<where> <if> ....
本文實(shí)現(xiàn)功能只用到了這兩個(gè),其他你們可以去了解
//這里的resultMap="BaseResultMap"可以改成resultType="DTO類的全路徑"
?
<select id="selectOrder" resultMap="BaseResultMap">
select
? ? ? ? ? ? ? ? tipo.id, tipo.prizes_name,tipo.Receive_address_id, tipo.create_time,
? ? ? ? ? ? ? ? tipo.Audit_time,
? ? ? ? ? ? ? ? tipo.integral, tipo.user_name
? ? ? ? ? ? ? ? from
? ? ? ? ? ? ? ? t_order tipo
? ? ? ? <where>
? ? ? ? ? ? '1'='1'
? ? ? ? ? ? <if test="prizesName != null and prizesName != ''">
? ? ? ? ? ? ? ? and tip.prizes_name = #{prizesName}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="startIntegral != null and startIntegral != ''">
? ? ? ? ? ? ? ? <if test="endIntegral != null and endIntegral != ''">
? ? ? ? ? ? ? ? ? ? and tip.prizes_integral between #{startIntegral} and #{endIntegral}
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="changeStartTime != null and changeStartTime != ''">
? ? ? ? ? ? ? ? <if test="changeEndTime != null and changeEndTime != ''">
? ? ? ? ? ? ? ? ? ? and tipo.create_time between #{changeStartTime} and #{changeEndTimestartTime}
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </if>
? ? ? ? </where>
? ? ? ? order by tipo.create_time
? ? </select>
再比如:
<select id="selectOrder" resultMap="BaseResultMap">
select
? ? ? ? ? ? ? ? id, prizes_name,Receive_address_id, create_time,
? ? ? ? ? ? ? ? Audit_time,
? ? ? ? ? ? ? ? integral, user_name
? ? ? ? ? ? ? ? from
? ? ? ? ? ? ? ? t_order
? ? ? ? <where>
? ? ? ? ? ? '1'='1'
? ? ? ? ? ? <if test="prizesName != null and prizesName != ''">
? ? ? ? ? ? ? ? and prizes_name = #{prizesName}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="startIntegral != null and startIntegral != ''">
? ? ? ? ? ? ? ? <if test="endIntegral != null and endIntegral != ''">
? ? ? ? ? ? ? ? ? ? and prizes_integral between #{startIntegral} and #{endIntegral}
? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </if>
//foreach的使用
<if test="ids!= null and ids.length!=0">
? ? ? ? ? ? id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</if>
? ? ? ? </where>
? ? ? ? order by create_time
? ? </select>
上面我們就通過(guò)動(dòng)態(tài)拼接實(shí)現(xiàn)了按多條件查詢的例子啦~,上面這種也可以不通過(guò)sql,直接在前端使用filter進(jìn)行實(shí)現(xiàn)(可以提高用戶體驗(yàn),秒速查詢)可以去看這個(gè)vue實(shí)現(xiàn)過(guò)濾器,但是這種情況
不能用于數(shù)據(jù)變化頻率高的情況,因?yàn)樵谇岸撕Y選不會(huì)重新查詢數(shù)據(jù)庫(kù),也不能實(shí)時(shí)更新文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-639288.html
?
到了這里,關(guān)于一個(gè)可以通過(guò)多個(gè)條件篩選的系統(tǒng)界面是如何實(shí)現(xiàn)的(springboot+mybatis)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!