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

单个参数

单个参数的传参比较简单,可以是任意形式的,比如#{a}#{b}或者#{param1}但是为了开发规范,尽量使用和入参时一样

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的几种传参方式的更多相关文章

  1. Mybatis的几种传参方式,你了解吗?

    持续原创输出,点击上方蓝字关注我 目录 前言 单个参数 多个参数 使用索引[不推荐] 使用@Param 使用Map POJO[推荐] List传参 数组传参 总结 前言 前几天恰好面试一个应届生,问了 ...

  2. PHP四种传参方式

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

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

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

  4. Vue中router两种传参方式

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

  5. axios的各种传参方式

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

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

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

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

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

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

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

  9. vue的三种传参方式

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

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

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

随机推荐

  1. .Net 7 的AOT的程序比托管代码更容易破解?

    楔子 .Net 7的一个重要功能是把托管的源码编译成Native Code,也就是二进制文件.此举看似增加了程序反编译难度,实际上是减少了程序的破解难度.本篇在不触及整个程序架构的前提下,以简单的例子 ...

  2. 青少年CTF-Web-Robots

    题目信息 题目名称:Robots 题目描述:昨天十三年社团讲课,讲了Robots.txt的作用,小刚上课没有认真听课正在着急,你能不能帮帮忙? 题目难度:一颗星 解题过程 访问题目链接 浏览器里是空白 ...

  3. GC耗时高,原因竟是服务流量小?

    原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 最近,我们系统配置了GC耗时的监控,但配置上之后,系统会偶尔出现GC耗时大于1s的报警,排查花了一些力气,故在这里 ...

  4. python进阶之路18 os、sys、json模块

    os模块与sys模块 os模块主要与操作系统打交道 sys模块主要与python解释器打交道 os模块(重要) os模块主要与代码运行所在的操作系统打交道 import os os.path.spli ...

  5. centos搭建neo4j环境(含java)2021_12

    限centos neo4j与java下载: 链接:https://pan.baidu.com/s/1ei15dROGy3OwJfbislxH7g 提取码:8B3A   下载后 1.在linux中建立文 ...

  6. ES中的内置对象--jquery如何优化代码,少用$进行查找,减少查找次数的方法

  7. 图书管理系统BMS

    图书管理系统BMS 效果图: 主要代码: 表关系的创建: from django.db import models # Create your models here. class Book(mode ...

  8. python学习day04

    1.基本数据类型之布尔值bool 1.用来判断事物的对错,是否可行,用于流程控制中 2.只有两种状态: True:对的.真的.可行的 False:错的.假的.不可行的 3.python中所有的数据都自 ...

  9. redis-07主从复制

    转 https://www.jianshu.com/p/06ab9daf921d https://www.jianshu.com/p/06ab9daf921d 1 基本说明 我们所说的主从复制,主机数 ...

  10. 从 Newtonsoft.Json 迁移到 System.Text.Json

    一.写在前面 System.Text.Json 是 .NET Core 3 及以上版本内置的 Json 序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了 ...