1.單表動態條件查詢 1)單字段作為參數 直接用@param設置的值作為注入就好了
@Select ( "select * from ppms_person_message where create_time = #{testValue}" ) void test ( @Param ( "testValue" ) String testValue
) ;
2)對象作為參數 注意:在@param內的值不為“ew”的時候會提示找不到條件參數
@Select ( "select * from ppms_person_message ${ew.customSqlSegment}" ) Page < PersonMessage > selectTestPage ( @Param ( "page" ) Page < PersonMessage > page
, @Param ( "ew" ) QueryWrapper < PersonMessage > qw
) ;
2.多表聯合動態條件查詢 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>使用標簽 注意:1)動態條件外雙引號改為單引號,內單引號改為轉義雙引號(") 2)使用遍歷條件時需要在最外成加上<script></script>標簽
@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
) ;
總結
以上是生活随笔 為你收集整理的mybatis-plus在Mapper类中使用@select标签进行多表联合动态条件查询 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。