简体   繁体   中英

how can we pass more than parameter with IN clause in iBatis

I know that we can pass list/string array in clause statement, like the below code

from here

Please advise me how can we pass more than parameter with IN clause in iBatis.

This is possible.

Define a class for holding your input, which will hold your three properties that are needed for the Select, so they might be:

  • FirstName (string)
  • LastName (string)
  • UserNames (list of strings)

If you were using C#, it might look something like this:

public class UserRetrievalParams
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public IList<String> UserNames { get; set; }
}

...or something similar. Construct an instance of that object and populate the list with your usernames.

Then pass that object into Ibatis as the parameter, and you can reference the properties like this:

<select id="example" resultMap="yourMap" parameterClass="UserRetrievalParams">
    select * from YourTable where

    username in
    <iterate property="UserNames" open="(" close=")" conjunction=",">
        #UserNames[]:char#
    </iterate>

    and user_first_name=#FirstName:char#
    and user_last_name=#LastName:char#
</select>

Note the slightly counter-intuitive syntax of having to reference the UserNames list property in two places: both in the iterate property, and again between the iterate tags.

I'm using this syntax successfully with Ibatis version 1.6.2.0 on .Net 2 and above.

Hope that helps.

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