spring 如何传递多个参数并使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24968088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:50:46  来源:igfitidea点击:

How can I pass multiple parameters and use them?

springmybatisibatis

提问by Jin Kwon

Hi I'm new to myBatis.

嗨,我是 myBatis 的新手。

I'm using MyBatis and Spring with mybatis-spring.

我将 MyBatis 和 Spring 与 mybatis-spring 一起使用。

How can I pass two different types of objects as parameters, and how can I use their properties in query?

如何将两种不同类型的对象作为参数传递,以及如何在查询中使用它们的属性?

<update id="update" parameterType="A, B"> <!-- @@? -->
  UPDATE SOME WHERE x=A.x AND y=B.y <!-- @@? -->
</update>

回答by Roman Konoval

Do not specify parameterTypebut use @Paramannotation on parameters in mapper:

不指定parameterType而是@Param在映射器中的参数上使用注释:

@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>

回答by Tural

Use parameterType="map" and @Param annotation.

使用 parameterType="map" 和 @Param 注释。

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

不需要@Param注解的值必须等于参数名称

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

回答by kiwiron

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

我建议阅读 MyBatis 文档——它非常全面和准确。

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

让我们举一个例子:从 com.mycompany.Customer POJO 实例更新客户的姓名,该实例具有 getFirstName() 获取器。

  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}
  1. 将 Customer 类的实例作为参数传递
  2. 设置参数类型=“com.mycompany.Customer”。但是请查看别名工具 - 然后您可以使用速记版本parameterType="Customer"
  3. 在 SQL 中使用标准的 MyBatis 语法:... 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.
  1. 传递包含多个对象的地图
  2. 创建一个实用程序类来聚合您的多个类,并将其作为参数传递。

Again, please read the manual...

再次,请阅读手册...