多條件搜索功能的自定義SQL--mybatis-plus

1.? DeviceMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bmc.cloud.mapper.DeviceMapper">

? ? <resultMap id="BaseResultMap" type="com.bmc.cloud.model.ExtDevice">

? ? ? ? <id column="id" property="id" jdbcType="INTEGER" />

? ? ? ? <result column="pairing_status" property="pairingStatus" jdbcType="VARCHAR"/>

? ? ? ? <result column="create_id" property="createId" jdbcType="INTEGER"/>

? ? ? ? <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>

? ? ? ? <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>

? ? </resultMap>

? ? <select id="getDeviceListByCondition" resultMap="BaseResultMap">

select c.*, d.patient_hcp_id

? ? ? ? from (

? ? ? ? ? ? SELECT a.*, b.patient_id

? ? ? ? ? ? from sys_device as a inner join sys_patient_device as b

? ? ? ? ? ? on a.sn=b.sn?and b.status = 1? // 左連查詢帶條件

? ? ? ? ? ? where a.create_id=#{createId, jdbcType=INTEGER}

????????????<if test="condition.sn != null and condition.sn != ''">

? ? ? ? ? ????? and a.sn like CONCAT('%',#{condition.sn, jdbcType=VARCHAR},'%')

? ? ? ????? </if>

? ? ? ? ????<if test="condition.model != null and condition.model != ''">

? ? ? ? ? ? ????and a.model like CONCAT('%',#{condition.model, jdbcType=VARCHAR},'%')

? ? ? ? ????</if>

? ? ? ? ? ? <if test="condition.type != null and condition.type != ''">

? ? ? ? ? ? ? ? and a.type = #{condition.type, jdbcType=VARCHAR}

? ? ? ? ? ? </if>

? ? ? ? ? ? <if test="condition.status != null and condition.status != ''">

? ? ? ? ? ? ? ? and a.status = #{condition.status, jdbcType=VARCHAR}

? ? ? ? ? ? </if>

? ? ? ? ? ? <if test="condition.pairingStatus != null and condition.pairingStatus != ''">

? ? ? ? ? ? ? ? and a.pairing_status = #{condition.pairingStatus, jdbcType=VARCHAR}

? ? ? ? ? ? </if>

? ? ? ? ? ? <if test="condition.startTime != null and condition.startTime != ''">

? ? ? ? ? ? ? ? and a.create_time >= STR_TO_DATE(#{condition.startTime, jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%s')

? ? ? ? ? ? </if>

? ? ? ? ? ? <if test="condition.endTime != null and condition.endTime != ''">

? ? ? ? ? ? ? ? and a.create_time &lt;= STR_TO_DATE(#{condition.endTime, jdbcType=VARCHAR},'%Y-%m-%d %H:%i:%s')

? ? ? ? ? ? </if>

? ? ? ? ) as c

? ? ? ? left JOIN sys_patient as d

? ? ? ? ON c.patient_id=d.id

????????order by c.create_time desc

? ? </select>

</mapper>


2. DeviceMapper.java

public interface DeviceMapper extends BaseMapper<Device> {

// 多表符合查詢列表

? ? @Select("select c.*, d.patient_hcp_id from (SELECT a.*, b.patient_id from sys_device as a LEFT JOIN sys_patient_device as b on a.create_id=#{createId} AND a.sn=b.sn) as c left JOIN sys_patient as d ON c.patient_id=d.id")

Page<ExtDevice> getPageDevicePatientVo(Integer createId, Page<ExtDevice> iPage);


// 多條件搜索功能

? ? Page<ExtDevice> getDeviceListByCondition(Integer createId, Page<ExtDevice> iPage,@Param("condition") SearchDeviceRequest request);

}


3. DeviceServiceImpl

public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

? ? @Resource

? ? DeviceMapper deviceMapper;

? ? @Override

? ? public Page<ExtDevice> searchDeviceList(Account account, SearchDeviceRequest request) {

? ? ? ? //獲取指定分頁的設(shè)備列表

? ? ? ? Long page = Long.valueOf(request.getPage());

? ? ? ? Long size = Long.valueOf(request.getSize());

? ? ? ? Page<ExtDevice> ipage = new Page<ExtDevice>(page, size);

? ? ? ? Page<ExtDevice> result = deviceMapper.getDeviceListByCondition(account.getId(), ipage, request);

? ? ? ? return result;

? ? }

}


4. DeviceService

public interface IDeviceService extends IService<Device> {

? public Page<ExtDevice> getDeviceList(Account account, Page<ExtDevice> ipage);

? public Page<ExtDevice> searchDeviceList(Account account, SearchDeviceRequest request);

}


5. DeviceController

@ApiOperation(value = "按條件獲取設(shè)備列表", notes = "post請求")

? ? @ApiImplicitParams({

? ? ? ? ? ? @ApiImplicitParam(name = "page", value = "頁碼", required = true, dataType = "java.lang.String"),

? ? ? ? ? ? @ApiImplicitParam(name = "size", value = "單頁容量", required = true, dataType = "java.lang.String"),

????????????@ApiImplicitParam(name = "startTime", value = "設(shè)備添加開始時間", required = true, dataType = ????????????"java.lang.String"),

? ? ? ? ? ? @ApiImplicitParam(name = "endTime", value = "設(shè)備添加結(jié)束時間", required = true, dataType = ????????????????"java.lang.String")

? ? })

? ? @PostMapping("/search")

? ? BaseResponse searchDeviceList(@RequestBody SearchDeviceRequest requestBody) {

? ? ? ? Page<ExtDevice> deviceList = deviceService.searchDeviceList(account, requestBody);

? ? ? ? return success(deviceList);

? ? }



6. 日期過濾簡化:入?yún)?before_date趾盐,?after_date為String

and v1.record_date BETWEEN #{before_date, jdbcType=DATE} AND #{after_date, jdbcType=DATE}

order by v1.record_date

7. 日期屬于某個集合

<select id="selectEventData" resultMap="BaseWithEventsResultMap">

? ? select

? ? v1.*,

? ? <include refid="Table_Event_Column_List"></include>

? ? from ventilator_summaries v1

? ? left join ventilator_event_pressures v2

? ? on v1.id = v2.summary_id

? ? left join ventilator_event_leaks v3

? ? on v1.id = v3.summary_id

? ? left join ventilator_event_apns v4

? ? on v1.id = v4.summary_id

? ? left join ventilator_event_hyps v5

? ? on v1.id = v5.summary_id

? ? left join ventilator_event_csas v6

? ? on v1.id = v6.summary_id

? ? where v1.serial = #{condition.serialNumber, jdbcType=VARCHAR}

? ? and v1.record_date in

? ? <foreach item = "recordDate" collection="condition.recordDateList" open="(" separator="," close=")">

? ? ? #{recordDate, jdbcType=DATE}

? ? </foreach>

? ? order by v1.record_date

? </select>

8. case when a then x when b then y end (參考: https://blog.csdn.net/qq_44858608/article/details/125735498)

select pd.p_date as pDate, pd.unp_date as unpDate, p.pa_id as paId,

(case when a.type = 2 then (select pr.`name` from prov pr where pr.ac_id = pd.op_id) when a.type = 3 then a.name END) as opName

from pa_d pd

left join pa p on p.id = pd.pa_id

left join ac a on a.id = pd.op_id

where pd.`status` = 0 and pd.sn = 'E110' ORDER BY pd.update_time DESC;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末疗韵,一起剝皮案震驚了整個濱河市熬的,隨后出現(xiàn)的幾起案子往衷,更是在濱河造成了極大的恐慌稀颁,老刑警劉巖咒劲,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件井辜,死亡現(xiàn)場離奇詭異悬襟,居然都是意外死亡布蔗,警方通過查閱死者的電腦和手機藤违,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門浪腐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人顿乒,你說我怎么就攤上這事议街。” “怎么了璧榄?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵特漩,是天一觀的道長。 經(jīng)常有香客問我骨杂,道長涂身,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任腊脱,我火速辦了婚禮访得,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘陕凹。我一直安慰自己悍抑,他們只是感情好,可當我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布杜耙。 她就那樣靜靜地躺著搜骡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪佑女。 梳的紋絲不亂的頭發(fā)上记靡,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機與錄音团驱,去河邊找鬼摸吠。 笑死,一個胖子當著我的面吹牛嚎花,可吹牛的內(nèi)容都是我干的寸痢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼紊选,長吁一口氣:“原來是場噩夢啊……” “哼啼止!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起兵罢,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤献烦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后卖词,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體巩那,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了拢操。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锦亦。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖令境,靈堂內(nèi)的尸體忽然破棺而出杠园,到底是詐尸還是另有隱情,我是刑警寧澤舔庶,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布抛蚁,位于F島的核電站,受9級特大地震影響惕橙,放射性物質(zhì)發(fā)生泄漏瞧甩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一弥鹦、第九天 我趴在偏房一處隱蔽的房頂上張望肚逸。 院中可真熱鬧,春花似錦彬坏、人聲如沸朦促。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽务冕。三九已至,卻和暖如春幻赚,著一層夾襖步出監(jiān)牢的瞬間禀忆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工落恼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留箩退,地道東北人。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓佳谦,卻偏偏與公主長得像乏德,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吠昭,可洞房花燭夜當晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容