Ibatis中用list传参数的方式。

Java代码  select count(id) from `user` where id in #[]# and status=1 。

 <select id="getcount" parameterClass="java.util.ArrayList" resultClass="int">
select count(id) from `user` where id in
<iterate open="(" close=")" conjunction="," >
#[]#
</iterate>
and status=1
</select>

程序调用的方式

java代码

 public Integer getcount(List<Integer> friendsIds) throws SQLException {
Integer count(Integer)client.queryForObject("User.getcount", friendsIds);
return count;
}

还可以在程序把list拼成String,用string当参数传给Ibatis查询,但是要注意在Ibatis的xml中要用 $parameter$来取参数,以保证Ibatis不改变参数的性质,如果用#parameter#取参数,此种传参的办法就不行了 。
select count(id) from `user` where id in ($parameter$)(注意:容易导致sql注入)

ibatis 数组参数
 

用迭代来实现,用parameterClass 来接收然后通过<iterate>遍历整个集合

Iterate的属性:
prepend - 可被覆盖的SQL语句组成部分,添加在语句的前面(可选)
property - 类型为java.util.List的用于遍历的元素(必选)
open - 整个遍历内容体开始的字符串,用于定义括号(可选)
close -整个遍历内容体结束的字符串,用于定义括号(可选)
conjunction - 每次遍历内容之间的字符串,用于定义AND或OR(可选)
<iterate> 遍历类型为java.util.List的元素。

例子:

user.xml

 <select id="getUser" parameterClass="java.util.Map" resultClass="userModel">

 <![CDATA[ select * from userinfo WHERE (userid in

 ]]>

   <iterate property="personList" open="(" close=")" conjunction=",">

     #personList[].userId#

 <!--$personList[].userId$-->

    </iterate>

 <![CDATA[

 )

 ]]>

 </select>

注意:使用<iterate>时,在List元素名后面包括方括号[]非常重要,方括号[]将对象标记为List,以防解析器简单地将 List输出成String。

(#) 使用的是PreparedStatement 机制,生成的SQL字符串中含有很多?,这些会被动态的添加参数进去查询

($) 中的变量好比字符串直接替换。

Dao.java

 public UserModel getUser(UserModel userModel) throws SQLException {                      

 Map<String, Object> map = new HashMap<String, Object>();

 List<UserModel> list = new ArrayList<UserModel>();

 UserModel userModel1 = new UserModel();

 userModel1.setUserId("1");

 list.add(userModel1);

 UserModel userModel2 = new UserModel();

 userModel2.setUserId("lsw");                                 

 list.add(userModel2);    

 map.put("personList", list);

 List sqlUserModelList = getSqlMapClientTemplate().queryForList("getUser", map);

 UserModel sqlUserModel = new UserModel();                     

 return sqlUserModel;    }

Ibatis中传List参数的更多相关文章

  1. 火狐浏览器URL中传中文参数乱码问题

    火狐浏览器:前端页面传中文 <span data-bind=" check_action:'roleMenuPriv'"> <a data-bind=" ...

  2. postman 模拟请求中添加 header,post请求中传json参数

    1. GET 请求 2.Post 请求 (请求参数为Json,header中带有参数) 问题延伸 GET请求不能够 添加 Body 吗?[答案]

  3. ibatis中in语句参数传入方法

    第一种:传入参数仅有数组,iterate中不能有数组的属性名       <select id="GetEmailList_Test"  resultClass=" ...

  4. JS在路径中传中文参数

    需要用 encodeURI('中文');处理一下.

  5. AJAX在GBK编码页面中传中文参数乱码的问题

    ---恢复内容开始--- 页面编码是GBK的情况下传递中文有乱码,解决方法如下: 在ajax传递前用若是Array,JSON,等其它对象,可用JSON.stringfy字符串序列化后,赋值给ajax传 ...

  6. js onclick函数中传字符串参数的问题

    规则: 外变是“”,里面就是‘’外边是‘’,里边就是“”   示例: var a="111"; var html="<a onclick='selecthoods( ...

  7. 把连接中传的参数截取出来变成一个json对象

    获取url function test() { var url=window.location.search; if(url.indexOf("?")!=-1) { var str ...

  8. swift向方法传数组参数的语法

    总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...

  9. 使用ibatis时 sql中 in 的参数赋值

    一.问题描述: 1.在使用ibatis执行下面的sql: update jc_jiesuan set doing_time = unix_timestamp(curdate()),doing_stat ...

随机推荐

  1. [转]通过AngularJS directive对bootstrap日期控件的的简单包装

    本文转自:http://www.cnblogs.com/Benoly/p/4109460.html 最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的 ...

  2. Linux下的C之2048

    #include <stdio.h> #include <stdlib.h> #include <curses.h> #include <time.h> ...

  3. MongoDB学习(二)Windows环境安装MongoDB

    MongoDB的官方下载站是http://www.mongodb.org/downloads,可以去上面下载最新的程序下来. 在下载页面可以看到,对操作系统支持很全面,OS X.Linux.Windo ...

  4. java 21 - 11 IO流的标准输入流和标准输出流

    标准输入输出流 System类中的两个成员变量: public static final InputStream in "标准"输入流. public static final P ...

  5. 实例化Model的三种方式

  6. C语言 数组类型与数组指针类型

    //数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...

  7. Linux考试易忘命令

    这是为了考试而做的笔记 mv命令可以移动可以改名 mv a /test //移动a到test文件夹下 mv a b //把a改名为b 软链接和硬链接的创建 ln -s profile a //prof ...

  8. Boost_udp错误

      注意一点:当我们不同PC机间进行通信的时候,IP和端口号是不一样的.之前遇到的问题是,boost_system_error,这是因为我们在写程序的时候,发送和接收绑定了同一个端口,导致程序出错. ...

  9. C# log4net 不输出日志

    一个新项目,直接用了一些之前的代码,突然跟踪不到日志了.检查发现了原因,特在此记录. log4net的配置文件log4net_config.xml <?xml version="1.0 ...

  10. 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)

    http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...