生活随笔
收集整理的這篇文章主要介紹了
mybatis-plus在Mapper类中使用@select标签进行多表联合动态条件查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.單表動(dòng)態(tài)條件查詢 1)單字段作為參數(shù) 直接用@param設(shè)置的值作為注入就好了
@Select ( "select * from ppms_person_message where create_time = #{testValue}" ) void test ( @Param ( "testValue" ) String testValue
) ;
2)對(duì)象作為參數(shù) 注意:在@param內(nèi)的值不為“ew”的時(shí)候會(huì)提示找不到條件參數(shù)
@Select ( "select * from ppms_person_message ${ew.customSqlSegment}" ) Page < PersonMessage > selectTestPage ( @Param ( "page" ) Page < PersonMessage > page
, @Param ( "ew" ) QueryWrapper < PersonMessage > qw
) ;
2.多表聯(lián)合動(dòng)態(tài)條件查詢 1>使用wrapper 注意:1)${ew.customSqlSegment}僅為where下的條件 2)join on后面的條件需要使用另外的param
@Select ( "select m.* from ppms_person_message m " + "left join ppms_notice n on n.id = m.notice_id and n.create_time = #{testValue}" + "${ew.customSqlSegment}" ) void test ( @Param ( "testValue" ) String testValue
, @Param ( "ew" ) QueryWrapper < PersonMessage > qw
) ;
2>使用標(biāo)簽 注意:1)動(dòng)態(tài)條件外雙引號(hào)改為單引號(hào),內(nèi)單引號(hào)改為轉(zhuǎn)義雙引號(hào)(") 2)使用遍歷條件時(shí)需要在最外成加上<script></script>標(biāo)簽
@Select ( "<script>" + " ( SELECT" + " '3' AS `type`," + " COUNT(*) AS `num`," + " SUBSTR( max( publish_time ), 6, 5 ) AS `time`" + " FROM ppms_notice" + " WHERE del_flag = '0' " + " )" + " UNION" + " ( SELECT" + " '1' AS `type`," + " COUNT(*) AS `messageNum`," + " SUBSTR( max( t.create_time ), 6, 5 ) AS `time`" + " FROM" + " `ppms_person_task` t" + " WHERE" + " t.`status` = '1'" + " AND (t.receiver_id = #{dto.receiverId}" + " <if test='dto.orgId != null and dto.orgId != \"\"'>" + " or t.receiver_id = #{dto.orgId}" + " </if>" + " <if test='null != dto.roleIds and dto.roleIds.size > 0'>" + " or t.receiver_id in" + " <foreach collection='dto.roleIds' item='item' close=')' open='(' separator=','>" + " #{item}" + " </foreach>" + " </if>" + " )" + " AND t.del_flag = '0'" + " )" + " UNION" + " ( SELECT" + " '2' AS `type`," + " COUNT(*) AS `num`," + " SUBSTR( max( m.create_time ), 6, 5 ) AS `time`" + " FROM" + " ppms_person_message m" + " WHERE" + " (m.receiver_id = #{dto.receiverId}" + " <if test='dto.orgId != null and dto.orgId != \"\"'>" + " or m.receiver_id = #{dto.orgId}" + " </if>" + " <if test='null != dto.roleIds and dto.roleIds.size > 0'>" + " or m.receiver_id in" + " <foreach collection='dto.roleIds' item='item' close=')' open='(' separator=','>" + " #{item}" + " </foreach>" + " </if>" + " )" + " AND m.`status` = '0'" + " AND m.del_flag = '0'" + " )" + "</script>" ) List < MessageRespVo > selectUnReadNumByUserId2 ( @Param ( "dto" ) TaskPageDto dto
) ;
總結(jié)
以上是生活随笔 為你收集整理的mybatis-plus在Mapper类中使用@select标签进行多表联合动态条件查询 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。