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

相关推荐
loading小马4 小时前
Mybatis-Plus超级实用的多种功能用法
java·spring boot·后端·maven·mybatis
计算机学姐4 小时前
基于SpringBoot的自习室座位预定系统【预约选座+日期时间段+协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·信息可视化·tomcat
学海无涯书山有路6 小时前
Android LiveData + MVVM 新手入门教程(基于 XML+Java)
android·xml·java
程序员乐只6 小时前
基于Python+Django+SSM热门旅游景点推荐系统(源码+LW+调试文档+讲解等)/热门旅游地推荐平台/旅游景点推荐软件/热门景点推荐系统/旅游推荐系统/旅游景点热门推荐
spring boot·spring·tomcat·hibernate·java-zookeeper·guava·java-consul
高山上有一只小老虎7 小时前
mybatisplus分页查询版本 3.5.8 以下和版本 3.5.9及以上的区别
java·spring boot·mybatis
人道领域7 小时前
javaWeb从入门到进阶(MyBatis拓展)
java·tomcat·mybatis
J2虾虾16 小时前
SpringBoot和mybatis Plus不兼容报错的问题
java·spring boot·mybatis
这周也會开心1 天前
SSM 配置 index 页面的实现方式
java·tomcat·springmvc
方方洛1 天前
技术实践总结:schema-bridgion:json、xml、yaml、toml文件相互转换
xml·前端·typescript·node.js·json
pp起床1 天前
【苍穹外卖】Day03 菜品管理
java·数据库·mybatis