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

相关推荐
计算机毕设指导610 分钟前
基于微信小程序的扶贫助农系统【源码文末联系】
java·spring boot·后端·微信小程序·小程序·tomcat·maven
雨中飘荡的记忆38 分钟前
MyBatis设计模式之装饰器、模版方法、策略模式
mybatis
hssfscv3 小时前
Javaweb学习笔记——JDBC和Mybatis
笔记·学习·mybatis
麦兜*4 小时前
Spring Boot 启动过程全解析:从main方法到Tomcat启动的魔法之旅
java·spring boot·后端·spring·tomcat·firefox
cike_y4 小时前
Spring整合Mybatis:dao层
java·开发语言·数据库·spring·mybatis
雨中飘荡的记忆4 小时前
MyBatis设计模式之构建者、工厂、代理模式
mybatis
oMcLin5 小时前
如何在 CentOS 7 上通过调优 Apache Tomcat 配置,提高高并发电商网站的性能?
centos·tomcat·apache
while(1){yan}14 小时前
Mybatis基础(详解)
spring boot·spring·java-ee·mybatis
多多*17 小时前
2026年1月3日八股记录
java·开发语言·windows·tcp/ip·mybatis
草原印象20 小时前
Spring、SpringMVC、Mybatis框架整合实战视频课程
java·spring·mybatis