Mybatis传参传入map可以使用以下两种方式:

  1. 使用@Param注解

在Mapper方法的参数中使用@Param注解指定map的key值作为参数名,例如:

@Mapper
public interface UserMapper {
    List<User> getUserList(@Param("params") Map<String, Object> params);
}

在Mapper.xml中可以通过#{params.key}来获取对应的value值,例如:

<select id="getUserList" resultType="com.example.User">
    SELECT * FROM user
    <where>
        <if test="params.name != null">
            AND name = #{params.name}
        </if>
        <if test="params.age != null">
            AND age = #{params.age}
        </if>
    </where>
</select>
  1. 直接使用Map作为参数

在Mapper方法的参数中直接使用Map作为参数,例如:

@Mapper
public interface UserMapper {
    List<User> getUserList(Map<String, Object> params);
}

在Mapper.xml中可以通过#{key}来获取对应的value值,例如:

<select id="getUserList" resultType="com.example.User">
    SELECT * FROM user
    <where>
        <if test="name != null">
            AND name = #{name}
        </if>
        <if test="age != null">
            AND age = #{age}
        </if>
    </where>
</select>
Mybatis传参传入map怎么做

原文地址: https://www.cveoy.top/t/topic/ba8C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录