持续原创输出,点击上方蓝字关注我

目录

  • 前言
  • 单个参数
  • 多个参数

    • 使用索引【不推荐】
    • 使用@Param
    • 使用Map
    • POJO【推荐】
  • List传参
  • 数组传参
  • 总结

前言

  • 前几天恰好面试一个应届生,问了一个很简单的问题:你了解过Mybatis中有几种传参方式吗?
  • 没想到其他问题回答的很好,唯独这个问题一知半解,勉强回答了其中两种方式。
  • 于是这篇文章就来说一说Mybatis传参的几种常见方式,给正在面试或者准备面试的朋友巩固一下。

单个参数

  • 单个参数的传参比较简单,可以是任意形式的,比如#{a}#{b}或者#{param1}但是为了开发规范,尽量使用和入参时一样
  • Mapper如下:
UserInfo selectByUserId(String userId);
  • XML如下:
<select id="selectByUserId" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=1
</select>

多个参数

  • 多个参数的情况下有很多种传参的方式,下面一一介绍。

使用索引【不推荐】

  • 多个参数可以使用类似于索引的方式传值,比如#{param1}对应第一个参数,#{param2}对应第二个参数.......
  • Mapper方法如下:
UserInfo selectByUserIdAndStatus(String userId,Integer status);
  • XML如下:
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{param1} and status=#{param2}
</select>
  • 注意:由于开发规范,此种方式不推荐开发中使用。

使用@Param

  • @Param这个注解用于指定key,一旦指定了key,在SQL中即可对应的key入参。
  • Mapper方法如下:
UserInfo selectByUserIdAndStatus(@Param("userId") String userId,@Param("status") Integer status);
  • XML如下:
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>

使用Map

  • Mybatis底层就是将入参转换成Map,入参传Map当然也行,此时#{key}中的key就对应Map中的key
  • Mapper中的方法如下:
UserInfo selectByUserIdAndStatusMap(Map<String,Object> map);
  • XML如下:
<select id="selectByUserIdAndStatusMap" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
  • 测试如下:
@Test
void contextLoads() {
Map<String,Object> map=new HashMap<>();
map.put("userId","1222");
map.put("status",1);
UserInfo userInfo = userMapper.selectByUserIdAndStatusMap(map);
System.out.println(userInfo);
}

POJO【推荐】

  • 多个参数可以使用实体类封装,此时对应的key就是属性名称,注意一定要有get方法。
  • Mapper方法如下:
UserInfo selectByEntity(UserInfoReq userInfoReq);
  • XML如下:
<select id="selectByEntity" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
  • 实体类如下:
@Data
public class UserInfoReq {
private String userId;
private Integer status;
}

List传参

  • List传参也是比较常见的,通常是SQL中的in
  • Mapper方法如下:
List<UserInfo> selectList( List<String> userIds);
  • XML如下:
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="list" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>

数组传参

  • 这种方式类似List传参,依旧使用foreach语法。
  • Mapper方法如下:
List<UserInfo> selectList( String[] userIds);
  • XML如下:
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="array" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>

总结

  • 以上几种传参的方式在面试或者工作中都会用到,不了解的朋友可以收藏下。
  • Mybatis专题文章写到这里也算是到了尾声,后期准备再写写Mybatis的面经,如果觉得作者写的不错,欢迎关注分享。

Mybatis的几种传参方式,你了解吗?的更多相关文章

  1. PHP四种传参方式

    test1界面: <html> <head> <title>testPHP</title> <meta http-equiv = "co ...

  2. python 计算机发展史,线程Process使用 for循环创建 2种传参方式 jion方法 __main__的解释

    ########################总结################## #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬 ...

  3. Vue中router两种传参方式

    Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...

  4. axios的各种传参方式

    axios的各种传参方式 1. params方式 axios({ url: '/users', method: 'get', params: { id: '11111', name: '22222' ...

  5. vue param和query两种传参方式

    1.传参方式 query传参方式 this.$router.push({ path: "/home", query: {code:"123"} }) param ...

  6. 浅谈C++三种传参方式

    浅谈C++三种传参方式 C++给函数传参中,主要有三种方式:分别是值传递.指针传递和引用传递. 下面通过讲解和实例来说明三种方式的区别. 值传递 我们都知道,在函数定义括号中的参数是形参,是给函数内专 ...

  7. vector作为参数的三种传参方式

    c++中常用的vector容器作为参数时,有三种传参方式,分别如下(为说明问题,用二维vector): function1(std::vector<std::vector<int> ...

  8. vue的三种传参方式

    <template> <div> <router-link :to="{'name':'x',params:{'type':'users'}}"> ...

  9. 四:flask-URL两种传参方式(路径传参和get传参)

    新建一个视图 第一种:路径传参:url/参数:<参数名>,然后再视图函数中接收参数 也可以指定数据类型 string:默认使用此数据类型,接收没有任何斜杠"\/"的文本 ...

随机推荐

  1. gpio模拟mdc/mdio通信

    本文主要是学习gpio模拟mdc/mdio通信. 运行环境是在ATMEL的sama5d35MCU,两个GPIO引脚模拟MDC/MDIO通信,读取百兆phy的寄存器的值. #include<lin ...

  2. hook框架-frida使用-APP在模拟器无法打开,用钩子去除限制

    app拿soul为例子 一.环境配置 #模拟器的frida服务为86 #frida-server-12.9.8-android-x86 adb push frida-server-12.9.8-and ...

  3. JavaScript学习系列博客_29_JavaScript arguments

    arguments (封装实参的对象) 在调用函数时,浏览器每次都会传递进两个隐含的参数:1.函数的上下文对象 this2.封装实参的对象 arguments- arguments是一个类数组对象,它 ...

  4. 详解Python中的__init__和__new__

    转载:https://my.oschina.net/liuyuantao/blog/747164 1.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ ...

  5. 如何下载gitbub中的单个文件

    1.进入Github文件夹,打开对应文件: 2.右键单击Raw,然后目标另存为即可.

  6. python字典的概念与基本操作

    字典是非常常用的一种数据结构,它与json格式的数据非常相似,核心就是以键值对的形式存储数据,关于Python中的字典做如下四点说明: 1.构造字典对象需要用大括号表示 {},每个字典元素都是以键值对 ...

  7. ElementUI 不维护了?供我们选择的 Vue 组件库还有很多!

    前文回顾:Vue+Spring Boot 前后端分离的商城项目开源啦! Vue 组件千千万,只要不行咱就换. ElementUI 近况 根据我最近的观察,得知一些关于 ElementUI 维护人员都退 ...

  8. 【独家】React Native 版本升级指南

    前言 React Native 作为一款跨端框架,有一个最让人头疼的问题,那就是版本更新.尤其是遇到大版本更新,JavaScript.iOS 和 Android 三端的配置构建文件都有非常大的变动,有 ...

  9. 想学习SEO可以看哪些书籍

    http://www.wocaoseo.com/thread-28-1-1.html 除了一些常见的比如入门推荐<走进搜索引擎>和进阶推荐<这就是搜索引擎--核心技术详解>之外 ...

  10. WebApis中DOM执行机制的认识

    1.1. 节点操作 1.1.1 删除节点 node.removeChild() 方法从 node节点中删除一个子节点,返回删除的节点. <button>删除</button> ...