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:默认使用此数据类型,接收没有任何斜杠"\/"的文本 ...
随机推荐
- goland2019.2破解方法
第一步:下载 jetbrains-agent.jar 链接:https://pan.baidu.com/s/1V2qZokAeAGcbsKDaoD9eSw 提取码:nrce 第二步:将下载的jetbr ...
- JS手写call、bind、apply
call方法的实现 Function.prototype.MyCall = function(content,...args){ const self = content || window; con ...
- OpenGL学习日志(2020.4之前)
咳咳,原本这个日志是本机上随便写的一些记录,也没怎么注意可读性和格式,有用信息密度很小,所以实用价值并不大.暂时由于不可抗因素得先鸽一段落了... 后续的日志会升格为模块化的学习记录,(应该)将会有很 ...
- 【原创】Kuberneters-ConfigMap的实践
一.什么是ConfigMap ConfigMap翻译过来即为“配置字典”,在实际的生产环境中,应用程序配置经常需要且又较为复杂,参数.config文件.变量等如果直接打包到镜像中,将会降 ...
- ASP.NET Core3.1使用IdentityServer4中间件系列随笔(一):搭建认证服务器
配套源码:https://gitee.com/jardeng/IdentitySolution 1.创建ASP.NET Core Web应用程序,选择空模板. 去掉HTTPS 2.添加nuget包:I ...
- 跳转语句 break 和 continue
break跳出循环体,结束本次循环. continue结束本次循环. for(var i=0; i<5; i++){ if(i == 3) break; document.write(" ...
- MySql 错误:In aggregated query without GROUP BY, expression #1 of SELECT list contains....
前段时间做sql注入的时候 使用group_concat时,出现标题上的错误.经查阅一位大佬的博客,成功解决!故写此博文! 当mysql的sql_mode是only_full_group_by的时候 ...
- android开发之gridView的一些属性。(项目经验总结)
1.android:numColumns="auto_fit" //GridView的列数设置为自动 2.android:columnWidth="90dp &quo ...
- layui 表单验证汇总
1 表单自带校验 lay-verify:是表单验证的关键字有以下值供选择: required 必填项phone 手机号email 邮箱date 日期url 链接identity 身份证number 数 ...
- hdu6704 2019CCPC网络选拔赛1003 K-th occurrence 后缀自动机+线段树合并
解题思路: fail树上用权值线段树合并求right/endpos集合,再用倍增找到待查询串对应节点,然后权值线段树求第k大. #include<bits/stdc++.h> using ...