Search code examples
ibatismybatis

How to add multiple named parameters to the SQL statement?


I would like to write something like this in myBatis (using anotations instead of XML):

@Insert("INSERT INTO friendships (user_id, friend_id) VALUES (#{user.id}, {friend.id})")
public void insert(User user, User friend);

Is this possible? How exactly?

(Note, I would like to use Objects of type User for type-safty. I know that using int parameters and use #{1} and #{2} as placeholders would work)


Solution

  • You can use annotations to provide a namespace for multiple input parameters. These essentially give nice names to the #{1.foo} and #{2.bar} identifiers.

    @Insert("INSERT INTO friendships (user_id, friend_id) VALUES (#{user.id}, 
    #{friend.id})")
    public void insert(@Param(value="user") User user, @Param(value="friend") User friend)