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

相关推荐
captain3761 小时前
Spring Web MVC入门(1)
ide·spring·java-ee·tomcat
驭渊的小故事6 小时前
MyBatis XML 开发实战:从配置到 CRUD 全解析
xml·java·mybatis
云絮.19 小时前
Mybatis(一)
java·数据库·mybatis
芦芭荞21 小时前
QT使用xml模版生成word文档
xml·qt·word
梁云亮2 天前
在 IDEA 中运行 Tomcat 时,直接在 server.xml 中配置的 <Context> 无效
tomcat·intellij-idea·虚拟路径
她的男孩3 天前
权限通了、列表有数,一点分页就炸Forge Admin 用 COUNT(1) 扛住复杂待办
java·spring boot·mybatis
高级程序源3 天前
springboot校园外卖平台13676-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·tomcat·maven·课程设计
paopaokaka_luck3 天前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
大牧师4 天前
Mybatis 框架教程
java·数据库·mybatis·数据持久化·orm
楚识科技4 天前
XML配置OCR接入实战:自定义OCR模板从字段定义到API调用全流程
xml·java·ocr