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

相关推荐
Codeking__38 分钟前
Redis初识——什么是Redis
数据库·redis·mybatis
hqp3 小时前
SQLite 不支持 LocalDateTime
sqlite·mybatis
用户8307196840824 小时前
秒杀面试!MyBatis-Spring-Boot 初始化流程深度拆解
spring boot·mybatis
chilavert3186 小时前
技术演进中的开发沉思-304计算机原理:XML
xml·计算机原理
8***f3956 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
w***76556 小时前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
张较瘦_19 小时前
SpringBoot3 | MyBatis-Plus 搞定宠物管理:从0到1实现增删改查
mybatis·宠物
沙白猿1 天前
Redis报错:A bean with that name has already been defined in class path resource
spring boot·redis·mybatis
计算机毕设指导61 天前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven