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 取出入参值的更多相关文章

  1. MyBatis 从浅入深 随笔整理

    MyBatis? archetypeCatalog = internal 本文档单独出现的_parameter都标识为变量名 一.三个基本要素: 核心接口和类 MyBatis 核心配置文件 SQL映射 ...

  2. 关于用mybatis调用存储过程时的入参和出参的传递方法

    一.问题描述 a)         目前调用读的存储过程的接口定义一般是:void  ReadDatalogs(Map<String,Object> map);,入参和出参都在这个map里 ...

  3. springMVC使用map接收入参 + mybatis使用map 传入查询参数

    测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...

  4. Mybatis调用PostgreSQL存储过程实现数组入参传递

    注:本文来源于 < Mybatis调用PostgreSQL存储过程实现数组入参传递  > 前言 项目中用到了Mybatis调用PostgreSQL存储过程(自定义函数)相关操作,由于Pos ...

  5. python学习笔记(九)函数返回多个值,列表生成式,循环多个变量,入参格式声明

    一.函数返回多个值 1.函数如果返回多个值的话,它会把这几个值放到一个元组里面2.函数如果返回多个值的话,也可以用多个变量来接收 def say(): num1 = num2 = num3 = ret ...

  6. mybatis入参方式和缓冲

    1.mybatis入参方式 @Param注解参数(注解) 封装成对象入参 public int updatePassword(@Param("id")int id,@Param(& ...

  7. JMeter使用JSON Extractor插件实现将一个接口的JSON返回值作为下一个接口的入参

    ##补充## 接口响应数据,一般为JSON,HTML格式的数据. 对于HTML的响应结果提取,可以使用正则表达式,也可以通过XPath来提取:对于JSON格式的数据,可以用正则表达式,JSON Ext ...

  8. mybatis框架之多参数入参--传入Map集合

    需求:查询出指定性别和用户角色列表下的用户列表信息 实际上:mybatis在入参的时候,都是将参数封装成为map集合进行入参的,不管你是单参数入参,还是多参数入参,都是可以封装成map集合的,这是无可 ...

  9. postman 上一个接口的返回值作为下一个接口的入参

    在使用postman做接口测试的时候,在多个接口的测试中,如果需要上一个接口的返回值作为下一个接口的入参,其基本思路是: 1.获取上一个接口的返回值 2.将返回值设置成环境变量或者全局变量 3.设置下 ...

随机推荐

  1. jsp页面中 <%%> <%! %>, <%=%> <%-- --%>有什么区别

    <%%> 可添加java代码片段   <%! %>       可添加java方法   <%=%>       变量或表达式值输出到页面 <%-- --%&g ...

  2. swagger如何测试List类型参数

    使用swagger 时,往往会用到类似下面这样的注解 @ApiImplicitParam(name = "id", value = "主键", dataType ...

  3. 在ubuntu上安装运行ionic项目

    1.安装nodejs.npm curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install - ...

  4. MT【243】球内接四面体体积

    已知半径为2的球面上有$A,B,C,D$四点,若$AB=CD=2$,则四面体$ABCD$的体积最大为____ 解答:利用$V=\dfrac{1}{6}|AB||CD|d<AB,CD>sin ...

  5. Qt Creator 中文编译失败 怎么办

    在Qt Creator 中c++源码有中文字符,结果不能编译成功. 代码 QMessageBox::warning(this, "警告","用户名密码错误",Q ...

  6. 树状数组区间加法&区间求和操作

    树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...

  7. luogu3188/bzoj1190 梦幻岛宝珠 (分层背包dp)

    他都告诉你能拆了 那就拆呗.把每个重量拆成$a*2^b$的形式 然后对于每个不同的b,先分开做30个背包 再设f[i][j]表示b<=i的物品中 容量为$ j*2^i+W\&((1< ...

  8. [NOI2010]海拔(最小割)

    题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个 正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个 ...

  9. UVALive - 4222

    题目链接:https://vjudge.net/contest/244167#problem/D 题目: For a dance to be proper in the Altered Culture ...

  10. 由AC自动机引发的灵感

    一,WA自动机 #include <cstdio> using namespace std; int main() { printf("☺"); ; } 二,TLE自动 ...