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

相关推荐
gAlAxy...16 小时前
MyBatis 核心配置文件 SqlMapConfig.xml 全解析
xml·mybatis
杀死那个蝈坦17 小时前
Docker
java·docker·eclipse·tomcat·hibernate
2501_9167665419 小时前
【Mybatis】延迟加载与多级缓存
缓存·mybatis
计算机毕设指导620 小时前
基于微信小程序的篮球场馆预订系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
无名-CODING20 小时前
#Servlet与Tomcat完全指南 - 从入门到精通(含面试题)
java·servlet·tomcat
YDS8291 天前
MyBatis-Plus精讲 —— 从快速入门到项目实战
java·后端·spring·mybatis·mybatis-plus
库库林_沙琪马1 天前
7、集成MyBatis
spring boot·mybatis
2501_916766541 天前
【Mybatis】注解开发与事务
mybatis
少年攻城狮2 天前
Mybatis-Plus系列---【自定义拦截器实现sql完整拼接及耗时打印】
数据库·sql·mybatis
编程修仙2 天前
第十一篇 Spring事务
xml·java·数据库·spring