一、 传入单个参数

当传入的是单个参数时,方法中的参数名和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. ubuntu中mysql忘记密码如何修改

    1.在终端进入安装目录下:cd /etc/mysql 2.sudo cat debian.cnf 输入密码 3用如下图用户和密码登录 4.进入mysql use mysql 5.使用describe ...

  2. Mac安装Qt出现错误Could not resolve SDK Path for 'macosx'

    Qt 5.8 + Mac 10.14  qdevice.pri文件里没有网上说的那行应该改的代码,自己写上这句话也没有解决问题 最终解决方案: 在命令行输入:sudo xcode-select -s ...

  3. String类的判断功能

    /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. * * String类的判断功能 ...

  4. 【Codeforces 922D】Robot Vacuum Cleaner

    [链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就 ...

  5. 夜话JAVA设计模式之适配器模式(adapter pattern)

    适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...

  6. T1081 线段树练习 2 codevs

    http://codevs.cn/problem/1081/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数, ...

  7. spring-cloud-starter-ribbon提供客户端的软件负载均衡算法

    Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...

  8. 21、Java并发性和多线程-Java中的锁

    以下内容转自http://ifeve.com/locks/: 锁像synchronized同步块一样,是一种线程同步机制,但比Java中的synchronized同步块更复杂.因为锁(以及其它更高级的 ...

  9. Unity3D Asset文件导出3DMax 可编辑格式

    本文章由cartzhang编写,转载请注明出处. 全部权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/60878354 作者:car ...

  10. ORA-01925:maximum of 80 enabled roles exceeded

    ORA-01925:maximum of 80 enabled roles exceeded max_enabled_roles 9i的參数,10g及以后都不用了. 指定用户session的最大ena ...