当前位置: 代码网 > it编程>编程语言>Java > Mybatis的七种传参方式示例全解析

Mybatis的七种传参方式示例全解析

2026年01月25日 Java 我要评论
mybatis 的 7 种传参方式详解1. 顺序传递参数通过方法参数的排列顺序进行传参,适用于多个简单类型参数。需注意在 xml 映射文件中必须按照参数顺序使用 #{arg0}、#{arg1} 或 #

mybatis 的 7 种传参方式详解

1. 顺序传递参数

通过方法参数的排列顺序进行传参,适用于多个简单类型参数。需注意在 xml 映射文件中必须按照参数顺序使用 #{arg0}#{arg1}#{param1}#{param2} 等形式引用。

controller 层示例:

@apioperation(value = "多个参数查询_匿名顺序传参")
@getmapping("findbyparams")
public resultmsg findbyparams(short gender, string age) {
    list result = employeemapper.selectbygenderandage(gender, age);
    return resultmsg.getmsg(result);
}

mapper 接口:

list<employee> selectbygenderandage(short gender, string age);

xml 映射配置(关键点:使用 param1、param2 按顺序引用):

<select id="selectbygenderandage" resultmap="baseresultmap">
    select * from employee where gender = #{param1} and age = #{param2}
</select>

⚠️ 注意:参数索引从 param1 开始,param1 对应第一个参数,param2 对应第二个。

2. 使用 @param 注解命名传参

为解决顺序传参可读性差的问题,可在 mapper 接口中使用 @param 注解为每个参数指定名称,使 xml 中可通过语义化名称访问。

mapper 接口修改:

list<employee> selectbygenderandage(@param("gender") short gender, @param("age") string age);

xml 映射文件(推荐写法,清晰直观):

<select id="selectbygenderandage" resultmap="baseresultmap">
    select * from employee where gender = #{gender} and age = #{age}
</select>
</select>

✅ 优势:代码可读性强,避免因参数顺序错误导致的 sql 异常。

3. 使用 map 传递多个参数

将多个参数封装成 map<string, object>,以键值对形式传递,适合参数较多或动态条件场景。

controller 层封装 map:

@apioperation(value = "多个参数查询_通过map传递多个参数")
@getmapping("findbymapparams")
public resultmsg findbymapparams(short gender, string age) {
    map<string, object> params = new hashmap<>();
    params.put("gender", gender);
    params.put("age", age);
    list result = employeemapper.selectbymapparams(params);
    return resultmsg.getmsg(result);
}

xml 配置说明:

<select id="selectbymapparams" resultmap="baseresultmap" parametertype="map">
    select * from employee where gender = #{gender} and age = #{age}
</select>

✅ 说明:parametertype="map" 可省略,mybatis 默认支持 map 传参;通过 #{key} 直接取值。

4. 使用 java bean 对象传参

当参数结构固定且与实体类一致时,推荐使用 java bean,controller 层通过 @requestbody 接收 json 数据并直接传递给 mapper。

controller 层接收对象:

@apioperation(value = "多个参数查询_通过java bean传递多个参数")
@postmapping("findbybeans")
public resultmsg findbybeans(@requestbody employee employee) {
    list result = employeemapper.selectbybeans(employee);
    return resultmsg.getmsg(result);
}

xml 映射配置:

<select id="selectbybeans" resultmap="baseresultmap" parametertype="com.wg.demo.po.employee">
    select * from employee where gender = #{gender} and age = #{age}
</select>

✅ 优势:结构清晰,自动映射属性,适合复杂对象查询。

5. 使用 json 对象直接传参

利用 jsonobject 接收任意结构的 json 数据,无需定义实体类,灵活性高,适用于动态查询场景。

controller 层处理 json:

@apioperation(value = "多个参数查询_通过json传递多个参数")
@postmapping("findbyjsonobject")
public resultmsg findbyjsonobject(@requestbody jsonobject params) {
    list result = employeemapper.findbyjsonobject(params);
    return resultmsg.getmsg(result);
}

xml 配置与 java bean 类似:

<select id="findbyjsonobject" resultmap="baseresultmap" parametertype="com.alibaba.fastjson.jsonobject">
    select * from employee where gender = #{gender} and age = #{age}
</select>
</select>

✅ 适用场景:快速开发、原型验证、字段不固定的接口。

6. 集合类型传参(list、set、array)

当需要根据集合进行 in 查询时,可直接传递集合对象,配合 <foreach> 标签遍历处理。

controller 层接收集合:

@apioperation(value = "多个参数查询_通过list、set、array传递多个参数")
@postmapping("findbylist")
public resultmsg findbylist(@requestbody list<string> list) {
    list result = employeemapper.findbylist(list);
    return resultmsg.getmsg(result);
}

xml 使用 <foreach> 构建 in 条件:

<select id="findbylist" resultmap="baseresultmap">
    select * from employee where age in
    <foreach collection="list" item="age" open="(" separator="," close=")">
        #{age}
    </foreach>
</select>

🔍 说明:

  • collection="list":默认参数名;
  • item:循环变量名;
  • open / close:包裹符号;
  • separator:分隔符。

7. 对象中包含集合属性的传参

用于处理嵌套结构数据,如部门对象包含员工列表,可在对象内部使用集合,并在 xml 中通过 ognl 表达式访问集合属性。

controller 层接收复合对象:

@apioperation(value = "多个参数查询_对象+集合参数")
@postmapping("findbydepartment")
public resultmsg findbydepartment(@requestbody department department) {
    list result = employeemapper.findbydepartment(department);
    return resultmsg.getmsg(result);
}

mapper 接口需配合 @param 指定别名:

list<employee> findbydepartment(@param("department") department department);

xml 中通过 ognl 访问对象内集合:

<select id="findbydepartment" resultmap="baseresultmap" parametertype="com.wg.demo.po.department">
    select * from employee 
    where dept_id = #{department.id} 
      and age in 
      <foreach collection="department.employees" item="employee" open="(" separator="," close=")">
        #{employee.age}
      </foreach>
</select>
</select>

✅ 技巧:使用 @param 可提升 ognl 表达式的可读性和稳定性。

📌 总结建议:

  • 参数少且简单 → 使用 @param
  • 参数多或结构固定 → 使用 java bean;
  • 动态字段或快速开发 → 使用 mapjsonobject
  • 涉及 in 查询 → 必须使用 <foreach> 配合集合;
  • 复杂嵌套结构 → 结合 @param 与 ognl 表达式访问对象属性。

合理选择传参方式,可显著提升代码可维护性与 sql 安全性。

到此这篇关于mybatis的七种传参方式示例全解析的文章就介绍到这了,更多相关mybatis传参方式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com