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

相关推荐
天上掉下来个程小白9 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
计算机毕设指导612 小时前
基于Springboot学生宿舍水电信息管理系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
Struggle Sheep13 小时前
容器化部署tomcat
java·tomcat
sunnyday042618 小时前
MyBatis XML映射文件中的批量插入和更新
xml·java·mysql·mybatis
计算机毕设指导620 小时前
基于SpringBoot的城乡商城协作系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
不修×蝙蝠2 天前
Tomcat理论(Ⅰ)
java·服务器·java-ee·tomcat
~Yogi2 天前
新版Tomcat MySQL IDEA 安装配置过程遇到的问题
mysql·tomcat·intellij-idea
云中飞鸿2 天前
MFC中CString的Format、与XML中的XML_SETTEXT格式化注意
xml·c++·mfc
剑走偏锋o.O2 天前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis