好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis foreach list特殊处理方式

foreach list特殊处理

最近做一个功能,sql要用到 IN 条件,通过list传入IN 的值,如:

?

1

SELECT * FROM table1 WHERE id in (1,2,3)

对应的mybatis写法为:

?

1

2

3

4

5

6

7

    < select id= "queryByIds" resultMap= "resultMap" parameterType= "list" >

        SELECT * FROM table1

        WHERE id

        <foreach collection= "list" item= "rid" open = "in(" separator= "," close = ")" >

            #{rid}

        </foreach>

    </ select >

期望结果是按list的值进行查询。

可是,当list为空的时候呢?

sql应该是这样的SELECT * FROM table1 WHERE id in ()。直接在mysql中运行肯定是语法错误的。

无论如何是不会查到数据的,但mybatis又是什么策略呢?直接把这一条where条件给我忽略了…导致查全表数据。

这也不好说mybatis做的好不好,算不算bug了。

既然知道套路了,就得有策略来应对了。于是多写了两个if。

?

1

2

3

4

5

6

7

8

9

10

11

12

    < select id= "queryByIds" resultMap= "resultMap" parameterType= "list" >

        SELECT * FROM table1

        WHERE id

        <if test= "list != null and list.size() > 0" >

            <foreach collection= "list" item= "rid" open = "in(" separator= "," close = ")" >

                #{rid}

            </foreach>

        </if>

        <if test= "list == null or list.size() == 0" >

            = -1

        </if>

    </ select >

不算是这么上策,但也能解决问题,当list为空时,给id赋值一个-1,保证查不到数据,sql也没有语法错误。 

foreach用法 List和Array,对象

向sql传递数组或List,mybatis使用foreach解析

foreach元素的属性主要有item,index,collection,open,separator,close。

item :集合中元素迭代时的别名,该参数为必选。 index :在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选 open :foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选 separator :元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用[,[隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 close :foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。

collection:foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的

主要有一下3种情况:

1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3. 如果使用Map封装了,collection的属性值为对应的Key

1、array数组的类型

UserMapper.java

?

1

List<User> findUser_array( int [] ids) throws Exception;

UserMapper.xml

?

1

2

3

4

5

6

7

8

9

<!--单参数array数组的类型-->

   < select id = "findUser_array" resultType = "com.xiaomin.page.pojo.User" > 

    select * from sys_user 

        < where > 

            < foreach collection = "array" item = "item" open = "and id in(" separator = "," close = ")" > 

                  #{item} 

            </ foreach > 

        </ where > 

   </ select >

Controller.java

?

1

2

3

4

5

6

7

@RequestMapping ( "/findUser_array" ) 

   @ResponseBody

   public List<User> foreach_test() throws Exception{ 

    int [] ids= new int []{ 1 , 2 , 3 }; 

    List<User> list = userMapper.findUser_array(ids); 

    return list; 

}

示例:

2、list的类型

UserMapper.java

?

1

List<User> findUser_list(List<Integer>list) throws Exception;

UserMapper.xml

?

1

2

3

4

5

6

7

8

9

<!--list类型-->

    < select id = "findUser_list" resultType = "com.xiaomin.page.pojo.User" > 

    select * from sys_user 

    < where > 

        < foreach collection = "list" item = "user_id" open = "and id in(" separator = "," close = ")" > 

            #{user_id} 

        </ foreach > 

    </ where > 

   </ select >

Controller.java

?

1

2

3

4

5

6

7

8

9

10

11

/**

  * list类型

  * @return

  * @throws Exception

  */

   @RequestMapping ( "/findUser_list" ) 

   @ResponseBody

   public List<User> findUser_list() throws Exception{ 

    List<User> list = userMapper.findUser_list(Arrays.asList( new Integer[]{ 1 , 2 , 3 , 6 })); 

    return list; 

}

测试截图: 

3、对象类型

UserMapper.java

?

1

List<User> findUser_obj(UserQueryVo vo) throws Exception;

UserMapper.xml

?

1

2

3

4

5

6

7

8

9

10

11

<!--对象类型-->

   < select id = "findUser_obj" parameterType = "com.xiaomin.page.pojo.vo.UserQueryVo" resultType = "com.xiaomin.page.pojo.User" > 

    select * from sys_user 

        < where > 

            < if test = "ids !=null" > 

                < foreach collection = "ids" item = "user_id" open = "and id in (" separator = "," close = ")" > 

                    #{user_id} 

                </ foreach > 

            </ if > 

        </ where > 

   </ select >

Controller.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

  * 对象类型

  * @return

  * @throws Exception

  */

   @RequestMapping ( "/findUser_obj" ) 

   @ResponseBody

   public List<User> findUser_obj() throws Exception{

 

    List<Integer> ids = new ArrayList<>(); 

    ids.add( 1 ); 

    ids.add( 2 ); 

    ids.add( 3 );

 

    UserQueryVo queryVo = new UserQueryVo(); 

    queryVo.setIds(ids); 

    List<User> list = userMapper.findUser_obj( null ); 

    return list; 

}

测试截图:

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

原文链接:https://blog.csdn.net/xplan5/article/details/118186996

查看更多关于mybatis foreach list特殊处理方式的详细内容...

  阅读:9次