Ibatis中传List参数
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注入)
用迭代来实现,用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参数的更多相关文章
- 火狐浏览器URL中传中文参数乱码问题
火狐浏览器:前端页面传中文 <span data-bind=" check_action:'roleMenuPriv'"> <a data-bind=" ...
- postman 模拟请求中添加 header,post请求中传json参数
1. GET 请求 2.Post 请求 (请求参数为Json,header中带有参数) 问题延伸 GET请求不能够 添加 Body 吗?[答案]
- ibatis中in语句参数传入方法
第一种:传入参数仅有数组,iterate中不能有数组的属性名 <select id="GetEmailList_Test" resultClass=" ...
- JS在路径中传中文参数
需要用 encodeURI('中文');处理一下.
- AJAX在GBK编码页面中传中文参数乱码的问题
---恢复内容开始--- 页面编码是GBK的情况下传递中文有乱码,解决方法如下: 在ajax传递前用若是Array,JSON,等其它对象,可用JSON.stringfy字符串序列化后,赋值给ajax传 ...
- js onclick函数中传字符串参数的问题
规则: 外变是“”,里面就是‘’外边是‘’,里边就是“” 示例: var a="111"; var html="<a onclick='selecthoods( ...
- 把连接中传的参数截取出来变成一个json对象
获取url function test() { var url=window.location.search; if(url.indexOf("?")!=-1) { var str ...
- swift向方法传数组参数的语法
总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...
- 使用ibatis时 sql中 in 的参数赋值
一.问题描述: 1.在使用ibatis执行下面的sql: update jc_jiesuan set doing_time = unix_timestamp(curdate()),doing_stat ...
随机推荐
- spring hadoop 访问hbase入门
1. 环境准备: Maven Eclipse Java Spring 版本 3..2.9 2. Maven pom.xml配置 <!-- Spring hadoop --> <d ...
- Hadoop_HDFS架构和HA机制
Hadoop学习笔记总结 01.HDFS架构 1. NameNode和ResourceManager NameNode负责HDFS,从节点是DataNode:ResourceManager负责MapR ...
- 二分套二分 hrbeu.acm.1211Kth Largest
Kth Largest TimeLimit: 1 Second MemoryLimit: 32 Megabyte Description There are two sequences A and ...
- python中list注意事项
今天写python出了一个小插曲,具体情况见代码: >>> a = [1,2,3] >>> import queue >>> q = queue. ...
- 使用 ssh 连接github的方法说明(gitub的官方说法)
https://help.github.com/articles/generating-an-ssh-key/
- 转 异常处理汇总 ~ 修正果带着你的Net飞奔吧!
异常处理汇总 ~ 修正果带着你的Net飞奔吧! 异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 异常处理汇总-开发工具 h ...
- javascript中的array对象属性及方法
Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, e ...
- form表单和ajax表单提交(Html.BeginForm()、Ajax.BeginForm())的差别
有如下几种区别: 1. Ajax在提交.请求.接收时,都是异步进行的,网页不需要刷新: Form提交则是新建一个页面,哪怕是提交给自己本身的页面,也是需要刷新的: 2. A在提交时,是在后台新建一个请 ...
- 微软职位内部推荐-Software Engineer II
微软近期Open的职位: Job Description Group: Search Technology Center Asia (STCA)/Search Ads Title: SDEII-Sen ...
- Linux 网络编程九(select应用--大并发处理)
//网络编程服务端 /* * 备注:因为客户端代码.辅助方法代码和epoll相同,所以select只展示服务器端代码 */ #include <stdio.h> #include < ...