温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybatis查询条件包含List的示例分析

发布时间:2021-09-24 11:19:43 来源:亿速云 阅读:114 作者:小新 栏目:开发技术

小编给大家分享一下Mybatis查询条件包含List的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

查询条件包含List的情况

在mybatis中进行搜索时,有时候参数中包含了List,比如传入参数:

public class FileRequest{
    //文件类型
    private Integer fileType;
    //状态
    private List<Status> statusList;
} 
public class Status{
    //注册状态
    private Integer registerStatus;
    //会议状态
    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参数时的<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>

执行结果:

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

以上是“Mybatis查询条件包含List的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI