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

相关推荐
zihan03212 小时前
若依(RuoYi)框架核心升级:全面适配 SpringData JPA,替换 MyBatis 持久层方案
java·开发语言·前端框架·mybatis·若依升级springboot
莫寒清3 小时前
MyBatis 中 ${} 和 #{} 有什么区别?
java·面试·mybatis
SakitamaX5 小时前
Tomcat介绍与实验
java·tomcat
2301_780669866 小时前
MyBatis(配置,增删改查,注解与XML两种开发方式)、SpringBoot配置文件(yml简化properties)
xml·spring boot·mybatis·javaweb
71ber9 小时前
RHCSE 实战笔记:Nginx 和 Tomcat --企业高并发应用的杀手锏
linux·nginx·tomcat
blockrock9 小时前
Tomcat
java·tomcat
莫寒清10 小时前
MyBatis 的缓存机制
面试·mybatis
cyber_两只龙宝10 小时前
Tomcat--企业级web应用服务器详细介绍与整合Nginx配置流程
linux·运维·前端·nginx·云原生·tomcat·负载均衡
Hx_Ma1610 小时前
mybatis练习2
java·数据库·mybatis
小邓睡不饱耶1 天前
Spring Boot 3 + MyBatis-Plus 高性能持久层开发实战:从入门到调优
spring boot·后端·mybatis