MyBatis-parameterType 取出入参值
SQL 映射文件的几种入参情况
一、单个基本类型参数
public MyUser selectMyUser(Integer id);
<!-- #{参数名或任意名}:取出参数值 -->
<select id="selectMyUser" resultType="myUser" parameterType="integer">
select * from myuser where id = #{id}
</select>
二、多个基本类型参数
多个参数会被封装成 一个Map,Key 为 param1...paramN,或者参数的索引 (0开始):arg0...argN,Value 为传入的参数值
public MyUser selectMyUserIdAndAge(Integer id,Integer age);
<!-- #{arg0}/#{param1}:取出参数值 -->
<select id="selectMyUserIdAndAge" resultType="myUser">
select * from myuser where id = #{param1} and age = #{arg1}
</select>
使用 @Param 注解
public MyUser selectMyUserIdAndAge(@Param("id") Integer id, @Param("age") Integer age);
<!-- #{@Param标识值}:取出参数值 -->
<select id="selectMyUserIdAndAge" resultType="myUser">
select * from myuser where id = #{id} and age = #{age}
</select>
封装成 Map 对象
public MyUser selectMyUserIdAndAgeOfMap(Map<String,Object> map);
<!-- #{key}:取出map中对应的值 -->
<select id="selectMyUserIdAndAgeOfMap" resultType="myUser">
select * from myuser where id = #{id} and age = #{age}
</select>
封装成一个类,例直接使用 POJO 对象
public Integer updateMyUser(MyUser user);
<!-- #{属性名}:取出传入pojo的属性值 -->
<update id="updateMyUser" parameterType="myUser">
update myuser set name=#{name},age=#{age} where id=#{id}
</update>
三、Collection 集合类型
public MyUser selectMyUserByList(List<Integer> ids);
<!-- #{collection[0]}:取出参数值,若为 List 还可使用 #{list[0]} -->
<select id="selectMyUserByList" resultType="myUser">
select * from myuser where id = #{list[0]}
</select>
四、Array 数组类型
public MyUser selectMyUserByArray(Integer[] integers);
<!-- #{array[0]}:取出参数值 -->
<select id="selectMyUserByArray" resultType="myUser">
select * from myuser where id = #{array[0]}
</select>
五、多种参数类型
public MyUser selectMyUserIdAndAge(Integer id, @Param("user") MyUser user);
<select id="selectMyUserIdAndAge" resultType="myUser">
select * from myuser where id = #{arg0} and age = #{user.age}
</select>
附上 MyBatis 封装集合的方法
org.apache.ibatis.session.defaults.DefaultSqlSession
public class DefaultSqlSession implements SqlSession { private Object wrapCollection(final Object object) {
if (object instanceof Collection) {
StrictMap<Object> map = new StrictMap<>();
map.put("collection", object);
if (object instanceof List) {
map.put("list", object);
}
return map;
} else if (object != null && object.getClass().isArray()) {
StrictMap<Object> map = new StrictMap<>();
map.put("array", object);
return map;
}
return object;
}
如果传入的是 Collection(List、Set)集合类型或者是 Array 数组类型,会把传入的 Collection 集合或者 Array 数组封装在 Map 中。
MyBatis-parameterType 取出入参值的更多相关文章
- MyBatis 从浅入深 随笔整理
MyBatis? archetypeCatalog = internal 本文档单独出现的_parameter都标识为变量名 一.三个基本要素: 核心接口和类 MyBatis 核心配置文件 SQL映射 ...
- 关于用mybatis调用存储过程时的入参和出参的传递方法
一.问题描述 a) 目前调用读的存储过程的接口定义一般是:void ReadDatalogs(Map<String,Object> map);,入参和出参都在这个map里 ...
- springMVC使用map接收入参 + mybatis使用map 传入查询参数
测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...
- Mybatis调用PostgreSQL存储过程实现数组入参传递
注:本文来源于 < Mybatis调用PostgreSQL存储过程实现数组入参传递 > 前言 项目中用到了Mybatis调用PostgreSQL存储过程(自定义函数)相关操作,由于Pos ...
- python学习笔记(九)函数返回多个值,列表生成式,循环多个变量,入参格式声明
一.函数返回多个值 1.函数如果返回多个值的话,它会把这几个值放到一个元组里面2.函数如果返回多个值的话,也可以用多个变量来接收 def say(): num1 = num2 = num3 = ret ...
- mybatis入参方式和缓冲
1.mybatis入参方式 @Param注解参数(注解) 封装成对象入参 public int updatePassword(@Param("id")int id,@Param(& ...
- JMeter使用JSON Extractor插件实现将一个接口的JSON返回值作为下一个接口的入参
##补充## 接口响应数据,一般为JSON,HTML格式的数据. 对于HTML的响应结果提取,可以使用正则表达式,也可以通过XPath来提取:对于JSON格式的数据,可以用正则表达式,JSON Ext ...
- mybatis框架之多参数入参--传入Map集合
需求:查询出指定性别和用户角色列表下的用户列表信息 实际上:mybatis在入参的时候,都是将参数封装成为map集合进行入参的,不管你是单参数入参,还是多参数入参,都是可以封装成map集合的,这是无可 ...
- postman 上一个接口的返回值作为下一个接口的入参
在使用postman做接口测试的时候,在多个接口的测试中,如果需要上一个接口的返回值作为下一个接口的入参,其基本思路是: 1.获取上一个接口的返回值 2.将返回值设置成环境变量或者全局变量 3.设置下 ...
随机推荐
- 用牛顿-拉弗森法定义平方根函数(Newton-Raphson method Square Root Python)
牛顿法(Newton’s method)又称为牛顿-拉弗森法(Newton-Raphson method),是一种近似求解实数方程式的方法.(注:Joseph Raphson在1690年出版的< ...
- 自学Linux命令行与Shell脚本之路
自学Linux命令行与Shell脚本之路[第一回]:初识Linux 1.1 自学Linux Shell1.1-Linux初识 1.2 自学Linux Shell1.2-Linux目录结构 1.3 ...
- Android多种方法显示当前日期和时间
文章选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文探讨Android显示当前日期和时间的方法. ...
- Hadoop集群的构建和安装
1.安装Java $ yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel 上述命令默认安装位置/usr/lib/jvm/java-1. ...
- selenium js
这几天的任务量比较大,还有一个挺棘手的网站cfda,不巧的是数据量还挺大,40W关于企业信息.上来就是debugger pause,调试中断,开始还是挺懵逼的,但这个还算简单毕竟google,百度,就 ...
- nodejs的某些api~(六)HTTPS
node的HTTPS模块接口与HTTP其实差不多,就是多了一个认证证书,私钥的配置等等,API都相似的. 在客户端服务器通信的方法中,只有HTTPS是最安全的,它的原理是客户端和服务器发送自己的公钥, ...
- Linux下设置VSCode为默认的文本编辑器
解决方法 执行一下命令 xdg xdg-mime default code.desktop text/plain Debian alternatives system sudo update-alte ...
- malloc() 和 calloc()有啥区别
calloc()在动态分配完内存后,自动初始化该内存空间为零(会将所分配的内存空间中的每一位都初始化为零). 而malloc()不初始化,里边数据是随机的垃圾数据. calloc(size_t n, ...
- bouncing-balls
效果如下: 代码目录如下: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charse ...
- Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:
Administrator@DESKTOP-EHCTIOR MINGW64 /d/react-native-eyepetizer (master) $ react-native run-android ...