简体   繁体   中英

how to foreach array in MyBatis

this is array:

String[] user_login_array={user_email,user_password};

this is mybatis.xml :

<select id="getUser" parameterType="String"resultType="post.User">
     select * from users where user_email=#{user_email} and user_password=#{user_password}
</select>

How to add array's parameters to this SQL?

This is something which I got first into mind. Please see the below logic, where you are actually looping over the array and using multiple AND and OR to tackle the query. This may not be the perfect query for you but hope this will give you some insights to the actual solution.

<select id="getUser" parameterType="String"resultType="post.User"> 
select * from users where    
          <foreach item="item" collection="user_login_map.entrySet()" separator="OR">
        (user_email=#{item.key} AND user_password=#{item.value})
        </foreach>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM