一、 传入单个参数

当传入的是单个参数时,方法中的参数名和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. Ice Cream Tower(The 2016 ACM-ICPC Asia China-Final Contest 二分&贪心)

    题目: Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists ...

  2. ubuntu wsl 子系统使用win10 系统ss代理步骤

    wind10 安装ss客户端 配置server 具体不多说 安装 ubuntu 子系统 3.安装python pip apt install python-pip 4.升级pip pip instal ...

  3. npm 使用教程

    链接----------------------------------npm官网npm淘宝镜像 安装包----------------------------------npm install -g ...

  4. Literature Review on Tidal Turbine

    source: scopus , SCIE keywords in tilte: tidal turbine Software: Bibliometrix

  5. dp专题备忘录

    hdu 1024:基础dp题 hdu 1029:主元素问题,很快的解法,计数器 hdu 1069:LIS hdu 1074:数位dp,数位dp基础 hdu 1257:简单LIS,要仔细分析为什么是求最 ...

  6. PAT 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. 虚拟机中的CentOS7如何上网?---https://blog.csdn.net/nothing2017/article/details/61420767

    虚拟机中的CentOS7如何上网?https://blog.csdn.net/nothing2017/article/details/61420767

  8. Android layer-list(3)

     Android layer-list(3) 在附录文章3.4的基础上,就Android layer-list再写一个较为复杂的应用. 先写布局文件,该布局涉及到LinearLayoutCompa ...

  9. web & Rich Text Editor

    web & Rich Text Editor 富文本编辑器 http://www.wangeditor.com/ https://github.com/wangfupeng1988/wangE ...

  10. HDU1507 Uncle Tom's Inherited Land*

    题目是跟 zoj1516是一样的,但多了匹配后的输出 详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html #include & ...