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

相关推荐
进击的CJR1 小时前
redis cluster 部署
java·redis·mybatis
vx1_Biye_Design3 小时前
基于web的物流管理系统的设计与实现-计算机毕业设计源码44333
java·前端·spring boot·spring·eclipse·tomcat·maven
sunddy_x8 小时前
Spring事务
java·spring·mybatis
阿杰 AJie9 小时前
MyBatis-Plus 的内置方法
java·数据库·mybatis
橘橙黄又青9 小时前
MyBatis篇
数据库·oracle·mybatis
NotStrandedYet9 小时前
《国产系统运维笔记》第2期:在 openEuler 24.03 LTS 上在线部署 Tomcat 9 全记录
java·tomcat·信创·国产化·openeuler·信创运维·国产化运维
一直都在5729 小时前
Spring3整合MyBatis实现增删改查操作
前端·vue.js·mybatis
迪霸戈9 小时前
MyBatis动态SQL避坑:为什么List用`[0]`而不是`get(0)`
sql·list·mybatis
不凡而大米、10 小时前
报错:传入的请求具有过多的参数。该服务器支持最多2100个参数
java·开发语言·mybatis
BD_Marathon10 小时前
MyBatis的一级缓存
spring·缓存·mybatis