一、 传入单个参数

当传入的是单个参数时,方法中的参数名和sql语句中参数名一致即可

List<User> getUser(int id);

<select id="getUser" parameterType="java.lang.Integer"  resultType="com.lee.test.pojo.User">
select * from t_user where id = #{id}
</select>

二、传入的是多个参数

1、当传入的是多个参数时,可以通过#{argindex}来对应参数,索引从0开始,其中sql语句中的参数顺序与方法中的参数顺序一致

List<User> getUser(int id, String name);

<select id="getUser"  resultType="com.lee.test.pojo.User">
select * from t_user where id = #{arg0} and name =#{arg1}
</select>

2、使用@Param注解来指定对应的参数,其中@param中的参数与sql语句中的参数名一致

List<User> getUser(@param("id")int id, @param("name")String name);

<select id="getUser"  resultType="com.lee.test.pojo.User">
select * from t_user where id = #{id} and name =#{name}
</select>

3、使用map封装多个参数,其中map中的key和sql语句中的参数名一致

List<User> getUser(HashMap map);

<select id="getUser" parameterType="hashMap" resultType="com.lee.test.pojo.User">
select * from t_user where id = #{id} and name =#{name}
</select>

三、传入的参数是一个实体类

当传入的是一个实体类,mybatis会根据实体类中字段自动与sql中参数关联

List<User> getUser(User user);

<select id="getUser" parameterType="com.lee.test.pojo.User" resultType="com.lee.test.pojo.User">
select * from t_user where id = #{id} and name =#{name}
</select>

四、当传入的参数包含了数组

如果传入的参数中包括了数组,可以使用mybatis中动态sql的foreach标签

标签属性说明:

(1)collection :collection属性对应有三种,list,array和map

(2)item : 在迭代每一个元素时起的别名,与#{}参数名一致

(3)index :这个属性用来指定用来访问迭代集合下标的名称。如:index="myIndex",则#{myIndex}用来访问当前迭代的下标,下标从0开始。

(4)open :拼接字符串的前缀,如"("

(5)close :拼接字符串的后缀,如")"

(6)separator :分隔符,表示迭代时每个元素之间的分隔符号,如","

1、如果传入的是一个list

List<User> getUser(List<Integer> ids);

<select id="getUser" resultType="com.lee.test.pojo.User">
select * from t_user where id in
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

如果传入的list ids为1,2,3

这句sql语句相当于 select * from t_user where id in (1,2,3)

2、如果传入的是一个array

List<User> getUser(int[] ids);

<select id="getUser" resultType="com.lee.test.pojo.User">
select * from t_user where id in
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

如果传入的int[] ids为1,2,3

这句sql语句相当于 select * from t_user where id in (1,2,3)

3、如果传入的是一个map

在开发过程中会遇到既要传入一个String,又要传入一个list,这时我们就可以把把这个String和list封装成一个map

//定义一个list
List ids = new ArrayList();
ids.add(1);
ids.add(2);
ids.add(3);
//将list和string放到map
HashMap map = new HashMap();
map.put("name", name);
map.put("ids", ids); //mapper.java
List<User> getUser(HashMap map); //mapper.xml
<select id="getUser" parameterType="java.util.HashMap" resultType="com.lee.test.pojo.User">
select * from t_user where name = #{name} and id in
<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

这里collection的值是map中list的key值,如果直接写list或者array就会报错

mybatis传参问题总结的更多相关文章

  1. 问题小记(MyBatis传参出现的小问题)

    问题一:在MyBatis中注解@Param和ParameterType不能一起用,会报错Parameter 'XXX' not found. Available parameters are [1, ...

  2. Mybatis传参方式

    Mybatis传多个参数(三种解决方案) 据我目前接触到的传多个参数的方案有三种. 第一种方案  DAO层的函数方法 ? 1 Public User selectUser(String name,St ...

  3. mybatis传参的几种方式

    1,@Param @参考文章 @Select("select s_id id,s_name name,class_id classid from student where  s_name= ...

  4. Java Mybatis 传参方式

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  5. mybatis 传参是 list<string> 的注意事项

    <!--付款 批量 修改账单状态--><update id="editbillpayALL" parameterType="java.util.List ...

  6. mybatis传参总结

    注:文章引用部分 mybatis传递参数总结文章内容 一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <sele ...

  7. php sql 类似 mybatis 传参

    PHP sql 处理上,没有类似于 java mybatis 的工具,导致进行一些sql 处理时,会有诸多不便, 楼主抽时间写了一个 php 类似 mybatis 的sql 工具,省去了拼装sql 的 ...

  8. Mybatis传参- 被逗号分割的字符串

    String ids = "1,2,3,4,5,6",如ids作为参数传递,查询list返回.mybatis用foreach处理并返回. SELECT * FROM yp_popu ...

  9. mybatis 传参为 Integer 时 ,Mapper 文件 中判断 条件 问题。

    <if test="valiStatus==null || valiStatus=='' || valiStatus==4 "> b.work_permit_card_ ...

随机推荐

  1. man中文手册安装

    转载自 https://www.cnblogs.com/fyc119/p/7116295.html man中文手册安装 下载源码 wget https://src.fedoraproject.org/ ...

  2. Java 中 break和 continue 的使用方法及区别

    break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后结束switch,不执行下面的语句. eg: publi ...

  3. python爬虫26 | 把数据爬取下来之后就存储到你的MySQL数据库。

    小帅b说过 在这几篇中会着重说说将爬取下来的数据进行存储 上次我们说了一种 csv 的存储方式 这次主要来说说怎么将爬取下来的数据保存到 MySQL 数据库 接下来就是 学习python的正确姿势 真 ...

  4. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  5. 使用 XMLHttpRequest实现Ajax

    [XMLHttpRequest的概述] 1.XMLHttpRequest最早是在IE5中以ActiveX组件的形式实现的.非W3C标准 2.创建XMLHttpRequest对象(由于非标准所以实现方法 ...

  6. Docker定制镜像

    定制镜像 除了使用定制好的镜像外,我们也可以通过定制实现符合自己环境的镜像. 在docker里面通过build方法来生成镜像,在生成镜像之前,我们需要一个Dockerfile脚本,脚本中包含的是一条一 ...

  7. 【Codeforces 242C】King's Path

    [链接] 我是链接,点我呀:) [题意] 让你找到(x0,y0)到(x1,y1)的一条最短路 走过的点必须在所给的n个横向路径上 [题解] 因为n条横向路径上的点最多不会超过10的5次方个,所以我们可 ...

  8. 九度oj 题目1053:互换最大最小数

    题目1053:互换最大最小数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7538 解决:3049 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后 ...

  9. empty array & Array.from

    empty array bug const duplicationArray = (arr = [], times = 2, debug = false) => { let result = [ ...

  10. Power of Matrix 等比数列求和 矩阵版!

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...