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 中设置的键名相匹配。

相关推荐
残月心殇 请珍惜枸2 小时前
LINQ之路17:LINQ to XML之X-DOM介绍
xml·solr·linq
SQL-First布道者7 小时前
⚡ Spring JDBC 完整体系 · 第 8 讲 · 查询四象限完备
java·spring boot·后端·spring·mybatis·spring jdbc
乐观的Terry1 天前
11、发布系统-用户认证与权限体系
java·spring boot·spring·spring cloud·mybatis
步行cgn1 天前
carList.forEach(System.out::println)
java·开发语言·mybatis
豆角焖肉2 天前
MyBatis多表关联查询(一对多/多对一/多对多)
mybatis·一对多·多表设计·多对多·一对一·多对一
学Linux的语莫2 天前
Redis 的两种持久化方式:RDB 和 AOF,一次讲懂
数据库·redis·mybatis
x17388062733 天前
XXE外部实体注入
xml·笔记
yio_yin3 天前
MyBatis
java·前端·mybatis
無a伟3 天前
Redis事务详解:原理、实战、坑点与实践
mybatis
豆角焖肉3 天前
MyBatis延迟加载、缓存机制与注解开发
java·spring·mybatis