Mybatis的几种传参方式,你了解吗?
持续原创输出,点击上方蓝字关注我

目录
前言 单个参数 多个参数 使用索引【不推荐】 使用@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的几种传参方式,你了解吗?的更多相关文章
- PHP四种传参方式
test1界面: <html> <head> <title>testPHP</title> <meta http-equiv = "co ...
- python 计算机发展史,线程Process使用 for循环创建 2种传参方式 jion方法 __main__的解释
########################总结################## #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬 ...
- Vue中router两种传参方式
Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...
- axios的各种传参方式
axios的各种传参方式 1. params方式 axios({ url: '/users', method: 'get', params: { id: '11111', name: '22222' ...
- vue param和query两种传参方式
1.传参方式 query传参方式 this.$router.push({ path: "/home", query: {code:"123"} }) param ...
- 浅谈C++三种传参方式
浅谈C++三种传参方式 C++给函数传参中,主要有三种方式:分别是值传递.指针传递和引用传递. 下面通过讲解和实例来说明三种方式的区别. 值传递 我们都知道,在函数定义括号中的参数是形参,是给函数内专 ...
- vector作为参数的三种传参方式
c++中常用的vector容器作为参数时,有三种传参方式,分别如下(为说明问题,用二维vector): function1(std::vector<std::vector<int> ...
- vue的三种传参方式
<template> <div> <router-link :to="{'name':'x',params:{'type':'users'}}"> ...
- 四:flask-URL两种传参方式(路径传参和get传参)
新建一个视图 第一种:路径传参:url/参数:<参数名>,然后再视图函数中接收参数 也可以指定数据类型 string:默认使用此数据类型,接收没有任何斜杠"\/"的文本 ...
随机推荐
- mysql高级内容学习总结
创建索引 create [unique] index indexname on tablename(columnname(length)) alter tablename add [unique] i ...
- 2020重新出发,MySql基础,MySql视图&索引&存储过程&触发器
@ 目录 视图是什么 视图的优点 1) 定制用户数据,聚焦特定的数据 2) 简化数据操作 3) 提高数据的安全性 4) 共享所需数据 5) 更改数据格式 6) 重用 SQL 语句 MySQL创建视图 ...
- Istio安全-授权(实操三)
Istio安全-授权 目录 Istio安全-授权 授权HTTP流量 为使用HTTP流量的负载配置访问控制 卸载 授权TCP流量 部署 配置TCP负载的访问控制 卸载 使用JWT进行授权 部署 使用有效 ...
- Android Studio 如何导出和导入自己的常用设置,避免重复制造轮子。加快开发速度
Android Studio 如何导出和导入自己的常用设置,避免重复制造轮子.加快开发速度 作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 在使用 A ...
- C#还原对图像做的修改
在C#程序中对图像进行处理,有的时候需要将处理后的图像还原,便于观察两者之间的区别,避免重新运行程序造成的麻烦.我是将之前写的Tab页中打开的图像进行还原,将原始图像数据保存在数据流中,然后从数据流中 ...
- iOS开发知识梳理博文集
前言 做iOS开发有3年了,从当初的小白到现在,断断续续看过很多资料,之前也写过一些博文来记录,但是感觉知识点都比较凌乱.所以最近准备抽时间把iOS开发的相关知识进行一个梳理,主要分为OC基础.UI控 ...
- Burst
Unity Burst 用户指南 https://blog.csdn.net/alph258/article/details/83997917 Burst https://unity3d.com/cn ...
- gson 处理null
1.定义null处理类 class StringConverter : JsonSerializer<String?>, JsonDeserializer<String?> { ...
- Fragment 1.2.0 更新记录
1.官方地址 https://developer.android.com/jetpack/androidx/releases/fragment 2.引入方法 dependencies { def fr ...
- [WUST-CTF]Web WriteUp
周末放假忙里偷闲打了两场比赛,其中一场就是武汉科技大学的WUST-CTF新生赛,虽说是新生赛,题目质量还是相当不错的.最后有幸拿了总排第5,记录一下Web的题解. checkin 进入题目询问题目作者 ...