iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis查询条件包含List的情况说明
  • 513
分享到

Mybatis查询条件包含List的情况说明

2024-04-02 19:04:59 513人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录查询条件包含List的情况在mybatis中查询的语句查询条件带List和其他类型字段需求查询条件包含List的情况 在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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Mybatis查询条件包含List的情况说明

本文链接: https://www.lsjlt.com/news/136509.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作