mybatis中xml接收参数值

你可以在 XML 映射文件中使用不同的参数占位符来接收这两个值。以下是一个示例:

xml 复制代码
<mapper namespace="com.example.mapper.UserMapper">  
    <select id="getUserByAttributes" parameterType="map" resultType="com.example.model.User">  
        SELECT * FROM user WHERE advertiser_id = #{advertiserId} AND product_platform_id = #{productPlatformId}  
    </select>  
</mapper>

在上面的示例中,我们使用了 parameterType="map",这意味着 MyBatis 将传递一个 Map 对象作为参数。在 Java 代码中,你可以创建一个 Map 对象来传递两个不同类型值的参数,如下所示:

java 复制代码
Map<String, Object> attributes = new HashMap<>();  
attributes.put("advertiserId", "someAdvertiserId");  
attributes.put("productPlatformId", "someProductPlatformId");  
  
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);  
User user = userMapper.getUserByAttributes(attributes);

在 XML 映射文件中,你可以使用 #{advertiserId} 和 #{productPlatformId} 来引用传递的 Map 对象中的属性。注意,键名(例如 advertiserId 和 productPlatformId)应该与你在 Map 中设置的键名相匹配。

相关推荐
泡海椒13 分钟前
JQuick-Excel JQuickRow 数据准备实战:JObjectConverter、JQuickRow.toRows 与 XML 导出链路
xml·windows·excel
shuoshuohaohao10 小时前
MyBatis 小知识点
java·spring·mybatis
CarIise2 天前
MyBatis动态SQL:从条件拼接到底层原理实战
mybatis
CarIise2 天前
Spring Boot图片展示与统一返回格式:从资源映射到泛型封装实战
mybatis
泡海椒2 天前
JQuick-Excel 字段映射实战:用 MAPPING 固化 Excel 表头与业务字段契约
xml·java·开发语言·excel
钝挫力PROGRAMER2 天前
开发踩坑记:MyBatis selectKey、空 SQL
数据库·sql·mybatis
自强的小白2 天前
核心功能(Service接口)
java·mybatis
自强的小白3 天前
mybatis-plus 的扩展功能
mybatis·mybatis_plus
王闯写bug3 天前
tomcat老版本下载tomcat8.5下载
java·tomcat
自强的小白3 天前
MYbatis-plus(自定义sql)
java·sql·mybatis