网站首页 > 博客文章 正文
1. 概述
Mybatis的sql参数传递就是将接口方法中定义的参数传输到sql中。sql有两种形式,一种是XML格式(Mapper映射器)中的sql;一种是注解方式的sql。常用参数的类型主要包括:
- 基本数据类型
- Map类型
- 自定义Bean类型
从参数传递的数量上来说,主要包括单参数和多参数形式。从传递参数的手段上来说,包括注解式和非注解式。下面主要讨论如何通过各种手段来传递单个和多个参数。
2. 单个参数的传递方式
单个参数的传递我们主要讨论单个基本数据类型参数的传递,单个参数(以下所说的单个参数都是指单个基本数据类型的参数)的传递是最简单也是最常用的。单个参数传递简单示例:
接口方法:
UserDO selectOneById(Long id);
xml配置:
<select id="selectOneById" parameterType="long" resultType="com.test.UserDO">
select * from user where user_id=#{id}
</select>
这里需要说明一下:
- 单个参数情况下,#{xx}中,“xx”的名称可以和接口方法中定义的变量名称不一致,“xx”甚至可以为任意名称。
- 如果参数使用注解的话,那么#{xx}中“xx”的名称必须和@Param("xx")中“xx”的名称一致。
- 如果使用@Select注解的话,@Select注解中的sql获取参数的方式和xml方式无异。
3. 多个参数的传递方式
多个参数的传递可以使用参数位置索引、List或者数组、Map封装和Bean封装的方式进行参数传递。
3.1 参数位置索引
有一个错误,估计很多人都见到过:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]
mybatis支持使用arg或者param加上参数位置索引来传递参数,arg的规则为:
- 每个参数使用arg开头
- 参数的位置从0开始,第一个参数为arg0,第二个参数为arg1,依次类推。
- 条件表达式中(例如if的test)直接使用arg+index
- sql语句中的使用方法和普通参数一样,#{arg+index}或者${arg+index}
一个简单的例子:
List<UserDO> selectListByCriteria1( String name,String phone);
<select id="selectListByCriteria1" resultType="com.test.User">
select id, name,phone
from user
<trim prefix="where" suffixOverrides="and">
<if test="arg0!=null and arg0!=''">
name like concat('%',#{arg0},'%')
</if>
<if test="arg1!=null and arg1!=''">
and phone=#{arg1}
</if>
</trim>
</select>
上面的例子中使用了arg+index的方式传递了参数,如果使用param,规则和arg0一样,只是index从1开始即可。
3.2 Map封装
使用Map封装方式来传递参数,我们只需要设置parameterType=“map”即可,现在将3.1中的例子换成map方式,如下:
List<UserDO> selectListByCriteria1(Map<String,String> map);
<select id="selectListByCriteria1" parameterType="map" resultType="com.test.User">
select id, name,phone
from user
<trim prefix="where" suffixOverrides="and">
<if test="name!=null and name!=''">
name like concat('%',#{name},'%')
</if>
<if test="phone!=null and phone!=''">
and phone=#{phone}
</if>
</trim>
</select>
3.3 JavaBean封装
使用JavaBean封装传递参数,需要设置parameterType为指定的JavaBean类型,将上面的例子修改为如下:
List<UserDO> selectListByCriteria1(UserDO user);
<select id="selectListByCriteria1" parameterType="map" resultType="com.test.User">
select id, name,phone
from user
<trim prefix="where" suffixOverrides="and">
<if test="name!=null and name!=''">
name like concat('%',#{name},'%')
</if>
<if test="phone!=null and phone!=''">
and phone=#{phone}
</if>
</trim>
</select>
3.4 使用注解方式
使用注解方式传递多个参数,就不需要设置parameterType的值,直接在sql语句中引用@Param中配置的参数名称,修改如下:
List<UserDO> selectListByCriteria1(@Param("name") String name,@Param("phone") String phone);
<select id="selectListByCriteria1" resultType="com.test.User">
select id, name,phone
from user
<trim prefix="where" suffixOverrides="and">
<if test="name!=null and name!=''">
name like concat('%',#{name},'%')
</if>
<if test="phone!=null and phone!=''">
and phone=#{phone}
</if>
</trim>
</select>
在xml中,不再需要设置parameterType的值,#{}中的值只需要和@Param中的值名称一样即可。注解方式是一种比较方便的传递多个参数的实现方式,但是参数个数不宜太多,如果五个以下还行,如果太多了,使用注解并不是一种最好的选择。
4. 传递数组或者可迭代集合
<foreach>标签在动态sql中使用频率是非常高的,这个时候一般会传入一个集合或者数组作为参数。我们先介绍下<foreach>的几个属性。
- item:集合元素迭代时候使用的别名,也就是代表集合中的一个成员。
- index:当集合是list或者数组的时候,index就是指的元素的序号,当集合是map时,指的是键。
- open:集合中元素拼接开始符号。
- separator:也是进行字符串拼接使用的,拼接每个元素时的分隔符。
- close:和open对应,拼接字符串的结尾符号。
- collection:循环迭代 的对象,这个属性稍微复杂以下,分为以下几种情况:如果是单参数,传入对象为List类型时,collection的值为list。如果是单参数,传入对象为数组时,collection的值为array。如果是一个map参数,需要迭代的对象是map中的一个值时,collection的值为对应的键。如果是一个JavaBean对象参数,collection的值为bean中集合的变量名称。
示例一:单参数,List类型对象
List<UserDO> selectListByCriteria1(List<String> names);
<select id="selectListByCriteria1" parameterType="list" resultType="com.test.UserDO">
select id, name, phone
from user
where name in
<foreach collection="list" separator="," index="" item="name" open="(" close=")">
#{name}
</foreach>
</select>
示例二:单参数,数组
List<UserDO> selectListByCriteria1(String[] names);
<select id="selectListByCriteria1" resultType="com.test.UserDO">
select id, name, phone
from user
where name in
<foreach collection="array" separator="," index="" item="name" open="(" close=")">
#{name}
</foreach>
</select>
示例三:map
List<UserDO> selectListByCriteria1(Map<String,Object> map);
//在调用selectListByCriteria1方法时,将参数放入list,并将list放入Map中,map的键为names
map.put("names",list);
<select id="selectListByCriteria1" parameterType="map" resultType="com.test.UserDO">
select id, name, phone
from user
where name in
<foreach collection="names" separator="," index="" item="name" open="(" close=")">
#{name}
</foreach>
</select>
示例四:JavaBean
List<UserDO> selectListByCriteria1(TestDTO test);
//TestDTO中有一个属性 nameList
private List nameList;
<select id="selectListByCriteria1" parameterType="com.test.TestDTO" resultType="com.test.UserDO">
select id, name, phone
from user
where name in
<foreach collection="nameList" separator="," index="" item="name" open="(" close=")">
#{name}
</foreach>
</select>
猜你喜欢
- 2024-12-05 路虎揽胜行驶中仪表提示只有正常高度可用
- 2024-12-05 linux系统运维之,你知道linux真实剩余内存吗?回收?
- 2024-12-05 一次线上故障:数据库连接池泄露后的思考
- 2024-12-05 Win10投屏功能无法使用解决办法(Miracast: Available, no HDCP)
- 2024-12-05 在Linux中如何禁止用户登录
- 2024-12-05 No qualifying bean of type '××Mapper' available的错误解决
- 2024-12-05 Steam显示 Steam is not available的最新解决方法
- 2024-12-05 面对性骚扰 两位中外女士的真实反应到底有何区别
- 2024-12-05 R包无法安装,这个方法实测有效
- 2024-12-05 「电脑知识」FirPE系统维护工具箱 v1.8纯净无流氓U盘装机必备软件
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)