好得很程序员自学网

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

mybatis 查询sql中in条件用法详解(foreach)

foreach属性主要有item,index,collection,open,separator,close

1、item表示集合中每一个元素进行迭代时的别名,

2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,

3、open表示该语句以什么开始,

4、separator表示在每次进行迭代之间以什么符号作为分隔符,

5、close表示以什么结束,

6、collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,

主要有一下3种情况:

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

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

c、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

< select id= "findBy" resultMap= "RfCustomerMemMap" parameterType= "java.util.Map" >

   SELECT

   <include refid= "Column" />

   FROM rfl_customer_mem a LEFT JOIN rfl_loan b ON a.member_no = b.loan_member_no

   WHERE a.member_no = #{memberNo} AND b.status IN

   <foreach collection= "status" index = "index" item= "item" open = "(" separator= "," close = ")" >

    #{item}

   </foreach>

   <if test= "name != null and name != ''" >

    AND name = #{ name }

   </if>

   <if test= "idNumber != null and idNumber != ''" >

    AND id_number = #{idNumber}

   </if>

   <if test= "mobileNo != null and mobileNo != ''" >

    AND mobile_no = #{mobileNo}

   </if>

   <if test= "loanNo != null and loanNo != ''" >

    AND loan_no = #{loanNo}

   </if>

   order by a.id DESC

   <if test= "offset > -1 and rows > -1" >

    limit #{offset},#{limit}

   </if>

  </ select >

java调用查询sql代码

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

public List<LoanMerchantMemEntity> findMerchantMemBy(String merchantName, String merchantNo, String socialCreditCode, String loanNo, int offset, int limit) {

   List<LoanMerchantMemEntity> list = new ArrayList<LoanMerchantMemEntity>();

   Map<String, Object> filter = new HashMap<String, Object>();

   filter.put( "merchantName" , merchantName);

   filter.put( "socialCreditCode" , socialCreditCode);

   filter.put( "status" , statsList());

   filter.put( "loanNo" , loanNo);

   filter.put( "offset" , offset);

   filter.put( "limit" , limit);

   filter.put( "merchantNo" , merchantNo);

 

   try {

    List<LoanMerchantMemEntity> row = loanMerchantMemDao.findBy(filter);

   } catch (Exception e) {

    LOGGER.error(filter, "查询企业会员信息异常" , e);

   }

   return list;

  }

 

  static List<String> statsList(){

   List<String> statusList = new ArrayList<String>();

   statusList.add( "SUCCESS" );

   statusList.add( "DUE" );

   statusList.add( "OVER" );

   return statusList;

  }

其中,map中key为status值类型为list,这种使用场景为第三种,即collection为map中的key值

补充:当传入一个String数组后,在sql中使用foreach语句实现IN查询

当我们从前台传递过来的是一个数组是,后台我们要进行处理, 因为在数据库中表的字段类型有可能是num 或者varchar;

我这里传过来的是Map 当然也可以使用request.getparameter("name") 这个name为jsp或者htm页面中的id所对应的name,

下面代码中也是的:

?

1

2

3

String name =(String) params.get( "name" );

String[] hiddens = name .split( "," );

params.put( "name" , hiddens);

当我们经过这一部分的处理后,数据就存入到map中了,传入参数后进行查询

AND 条件 in

?

1

2

3

<foreach collection= "name" index = "index" item= "item" open = "(" separator= "," close = ")" >

    #{item}

   </foreach>

在sql上面,我们进行查询的时候就OK啦!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/aiyawalie/article/details/52954138

查看更多关于mybatis 查询sql中in条件用法详解(foreach)的详细内容...

  阅读:15次