• mybatis中判断传入的数组与集合是否为空+mybatis中Foreach的使用详解


    数组判空

    传过来的数组 object[] ,在mapper中判空时先判断是否为null,再判断数组长度 object.length是否大于0.

    1. <if test=" object !=null and object.length > 0">
    2. 你的逻辑sql
    3. if>

    集合判空

    比如参数为List集合,在mybatis中先判断是否为null,不为null再判断集合的长度 object.size() 是否大于0即可。

    1. <if test=" object != null and object.size() > 0">
    2. 你的逻辑sql
    3. if>

    不为空循环 使用forech 

    foreach

    如果​​​​​​​collection的类型为List

    List getUserInfo(@Param("userName") List userName);
    

    使用@Param注解自定义keyName; 

    1. <if test="userName!= null and userName.size() >0">
    2. USERNAME IN
    3. <foreach collection="userName" item="value" separator="," open="(" close=")">
    4. #{value}
    5. foreach>
    6. if>

    也可以使用默认属性值list作为keyname

    1. <select id="selectByIds" resultType="com.olive.pojo.User">
    2. select * from t_user where id in
    3. <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
    4. #{item}
    5. foreach>
    6. select>

    如果collection的属性为array

    List getUserInfo(@Param("userName") String[] userName);
    

    使用@Param注解自定义keyName; 

    1. <select id="getUserInfo" resultType="com.test.UserList">
    2. SELECT
    3. *
    4. FROM user_info
    5. where
    6. <if test="userName!= null and userName.length() >0">
    7. USERNAME IN
    8. <foreach collection="userName" item="value" separator="," open="(" close=")">
    9. #{value}
    10. foreach>
    11. if>
    12. select>

    也可以,使用默认属性值array作为keyname

    1. <select id="selectByIds" resultType="com.olive.pojo.User">
    2. select * from t_user where id in
    3. <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
    4. #{item}
    5. foreach>
    6. select>

    如果collection的属性为Map

    List getUserInfo(@Param("user") Map user);
    

    第一种:获取Map的键值对

    多字段组合条件情况下,一定要注意书写格式:括号()

    eg: SELECT * FROM user_info WHERE (USERNAME,AGE) IN (('张三','26'),('李四','58'),('王五','27'),......);
    
    1. <select id="getUserInfo" resultType="com.test.UserList">
    2. SELECT
    3. *
    4. FROM user_info
    5. where
    6. <if test="user!= null and user.size() >0">
    7. (USERNAME,AGE) IN
    8. <foreach collection="user.entrySet()" item="value" index="key" separator="," open="(" close=")">
    9. (#{key},#{value})
    10. foreach>
    11. if>
    12. select>

    第二种:参数Map类型,只需要获取key值或者value值

    key:

    1. <select id="getUserInfo" resultType="com.test.UserList">
    2. SELECT
    3. *
    4. FROM user_info
    5. where
    6. <if test="user!= null and user.size() >0">
    7. (USERNAME) IN
    8. <foreach collection="user.keys" item="key" separator="," open="(" close=")">
    9. #{key}
    10. foreach>
    11. if>
    12. select>

    value:

    1. <select id="getUserInfo" resultType="com.test.UserList">
    2. SELECT
    3. *
    4. FROM user_info
    5. where
    6. <if test="user!= null and user.size() >0">
    7. (USERNAME) IN
    8. <foreach collection="user.values" item="value" separator="," open="(" close=")">
    9. #{key}
    10. foreach>
    11. if>
    12. select>

  • 相关阅读:
    prompt learning——你需要掌握的基础知识以及离散型 prompt 的代码
    Netty-实验
    接口测试需要验证数据库么?
    UFS详细介绍---终章
    【MySQL从入门到精通】【高级篇】(十一)Hash索引、AVL树、B树与B+树对比
    C++学习笔记(二十一)
    以前端视角,漫谈「云端」
    3.获取元素
    docker部署ElasticSearch过程记录
    分布式知识整理
  • 原文地址:https://blog.csdn.net/w13966597931/article/details/126541161