Mybatis查詢條件包含List的情況說明
查詢條件包含List的情況
在mybatis中進行搜索時,有時候參數(shù)中包含了List,比如傳入?yún)?shù):
public class FileRequest{ //文件類型 private Integer fileType; //狀態(tài) private List<Status> statusList; } public class Status{ //注冊狀態(tài) private Integer registerStatus; //會議狀態(tài) private Integer meetingStatus }
在mybatis中查詢的語句
<select id="findList" parameterType="FileRequest" resultMap="..."> select * from tableName where 1=1 <if test="fileType != null "> and file_type = #{fileType} </if> <if test="statusList != null "> and <foreach collection="statusList" index="index" item="item" open"(" separator="or" close=")"> <if test="item.registerStatus != null "> and register_status= #{item.registerStatus} </if> <if test="item.meetingStatus != null "> and meeting_status= #{item.meetingStatus } </if> </foreach> </if> </select>
查詢條件帶List和其他類型字段
需求
Mybatis查詢條件帶List和其他類型字段(Integer,String,...).
select * from table where type=? and code in (?,?,?,?)
Mapper.java文件
List<BaseDictionary> selectByTypeAndCodes( @Param("codes") List<Integer> codes, @Param("type") Integer type); Mapper.xml.
注意其中<foreach collection="codes"中的collection的值要和你定義的List別名@Param(“codes”)一致,
而不是只有一個list參數(shù)時的<foreach collection="list"
<select id="selectByTypeAndCodes" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from base_dictionary where type = #{type} AND code in <foreach collection="codes" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> AND show_enable=1 AND obj_status=1 ORDER BY sort </select>
執(zhí)行結果:
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)==> Preparing: select id, type, name, code, sort, show_enable, obj_remark, obj_status, obj_createdate, obj_createuser, obj_modifydate, obj_modifyuser from base_dictionary where type = ? AND code in ( ? , ? , ? ) AND show_enable=1 AND obj_status=1 ORDER BY sort
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)==> Parameters: 34(Integer), 1(Integer), 2(Integer), 3(Integer)
BaseJdbcLogger.debug(BaseJdbcLogger.java:145)<== Total: 2
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring-Cloud-Function-Spel?漏洞環(huán)境搭建
這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧2022-03-03Java map.getOrDefault()方法的用法詳解
這篇文章主要介紹了Java map.getOrDefault()方法的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12SpringBoot3.x中spring.factories?SPI?服務發(fā)現(xiàn)機制的改變問題小結
spring.factories其實是SpringBoot提供的SPI機制,底層實現(xiàn)是基于SpringFactoriesLoader檢索ClassLoader中所有jar引入的META-INF/spring.factories文件,這篇文章主要介紹了SpringBoot3.x中spring.factories?SPI?服務發(fā)現(xiàn)機制的改變,需要的朋友可以參考下2023-05-05SpringBoot實現(xiàn)發(fā)送郵件、發(fā)送微信公眾號推送功能
這篇文章主要介紹了SpringBoot實現(xiàn)發(fā)送郵件、發(fā)送微信公眾號推送功能,這里對成員變量JavaMailSender使用了@Resource注解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03springboot 啟動時初始化數(shù)據(jù)庫的步驟
這篇文章主要介紹了springboot 啟動時初始化數(shù)據(jù)庫的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01Java連接MySQL數(shù)據(jù)庫并實現(xiàn)數(shù)據(jù)交互的示例
數(shù)據(jù)庫是非常重要的一種存儲格式,可以大大提高存儲效率,本文主要介紹了Java連接MySQL數(shù)據(jù)庫并實現(xiàn)數(shù)據(jù)交互的示例,具有一定的參考價值,感興趣的可以了解一下2024-03-03