How can I pass multiple parameters and use them?

狂风中的少年 提交于 2019-12-03 05:28:19

Do not specify parameterType but use @Param annotation on parameters in mapper:

@Mapper
public interface MyMapper {

    void update(@Param("a") A a, @Param("b") B b);

    ...
}

Then reference them in mapping:

<update id="update" > 
   UPDATE SOME WHERE x=#{a.x} AND y=#{b.y}
</update>

Use parameterType="map" and @Param annotation.

Method declared in interface:

void mapCategoryAndPage(@Param("categoryLocalId") Long categoryLocalId, @Param("pageLocalId") Long localId);

It is not required that value of @Param annotation must be equal to name of parameter

<insert id="mapCategoryAndPage" parameterType="map">
    INSERT INTO
        category_page_mapping (
            page_local_id,
            category_local_id)
    VALUES
        (#{pageLocalId},
         #{categoryLocalId});
</insert>

I would suggest reading the MyBatis documentation - it is pretty comprehensive and accurate.

Lets take an example: updating a customer's name from a com.mycompany.Customer POJO instance, which has a getFirstName() getter.

  1. Pass an instance of your Customer class as the parameter
  2. Set the parameterType= "com.mycompany.Customer". But check out the alias facility - then you can use the shorthand version parameterType="Customer"
  3. Use the standard MyBatis syntax in the SQL: ... set FIRST_NAME = #{firstName}

(Edit) If you need to pass multiple objects there are various options:

  1. Pass a map containing the multiple objects
  2. Create a utility class the aggregates your multiple classes, and pass this as the parameter.

Again, please read the manual...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!