【Mybatis】mybatis中xml配置文件使用where in 语句

最近写钉钉小程序,获取前台传过来的数组,然后用 where in 语句来查询指定的数据,返回是一个 list 集合

问题一:

我返回的 resultType是一个List,那么我在 resultType 中怎么写?


一开始写法

<select id="findUserIdByCondition" resultType="java.util.List"> </select>

我返回的是一个list,但是却报错了,报错信息如下

### Cause: java.lang.UnsupportedOperationException

解决:将 resultType中的"java.util.List"改为java.lang.String

问题二:怎么遍历list数组,将list数组中的字段放入 in()中

方法一:

可以将 list 数组转为 String 类型,放入in()中,但是数组转为字符串后,两边会有 [] ,那么怎么去除呢

可以用下面的方法

StringUtils.strip(list.toString(), "[]");

StringUtils工具类是在 apache.commons-lang 包下,如果没有,那么可以在 maven 中导入坐标
坐标如下:

 <dependency>
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
       <version>2.6</version>
       <scope>provided</scope>
 </dependency>

用此方法可以消除数组两边的 []
然后写xml 动态sql
dao层的接口方法

List<String> findUserIdByCondition(String deptList,String groupList,String sexList,String locationList);

mappel.xml中

 <select id="findUserIdByCondition" resultType="java.lang.String">
            SELECT userid FROM v_user

            <!--List deptList,List groupList,List sexList,List locationList-->
            <where>
                <if test="deptList != null">
                    d_id IN (${deptList})
                </if>
                <if test="groupList != null">
                    AND u_group IN (${groupList})

                </if>
                <if test="sexList != null">
                    AND u_sex IN (${sexList})
                </if>
                <if test="locationList != null">
                    AND u_location IN ("${locationList}")
                </if>

            </where>

        </select>

这里会发现,我用的是${}而没用#{}

Mybatis 在 处 理 #{} 时 , #{} 传入参数是以字符串传 入 , 会将SQL 中的 #{} 替 换 为 ? 号 , 调 用 PreparedStatement 的 set 方法来赋值。

Mybatis 在 处理时,是 原值传入 ,就 是把{} 替换 成变量的 值,相当于 JDBC 中的 Statement 编译变量替换 后 ; #{} 对应的变量自动加上单引号 ‘’ ; 变量替换后, ${} 对应 的变量不会加上单引号 ‘’

这样可以将所要查询的数组字段放入 in

方法二:

这个就是官方文档中所说的方法,官方文档链接
利用<foreach></foreach>标签来遍历数组中的元素,在放入in()

dao层的接口方法

List<String> findUserIdByCondition(List<String> deptList,List<String> groupList,List<String> sexList,List<String> locationList);

这里我们会发现我传进去的参数是数组List的形式,而不是String类型

<select id="findUserIdByCondition" resultType="java.lang.String">
        SELECT userid FROM v_user

        <!--List deptList,List groupList,List sexList,List locationList-->
        <where>
            <if test="deptList != null">
                d_id IN
                <foreach item="item" index="index" collection="deptList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="groupList != null">
                AND u_group IN
                <foreach item="item" index="index" collection="groupList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="sexList != null">
                AND u_sex IN
                <foreach item="item" index="index" collection="sexList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>

            </if>
            <if test="locationList != null">
                AND u_location IN
                <foreach item="item" index="index" collection="locationList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

        </where>

    </select>

这样也可以将集合中的list值放入 in()中,不过自我感觉性能上应该没第一种好,因为还用到了for循环遍历(自我认为…)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Black_Customer/article/details/108042033