iBatis之Iterator的使用
2024-09-28 15:54:57
一:前言
现在这个项目使用的是iBatis,我刚刚开始的时候说是用MyBatis,因为我以前用过,觉得还是比较好用的啊,而且不像iBatis样,查什么一个字段不能多也不能少,觉得好无语啊。
二:内容
自己现在接触的iterator有如下两种用处
(一):批量循环插入
批量插入的话有可以分开说,一种是在后台代码中,批量插入,一种是在数据库中循环插入
(1):在先说后台批量插入的,直接上代码
return (Integer)this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
executor.startBatch();
int count=0;
for(User u:list){
executor.insert("batchAddUser", u);
}
count=executor.executeBatch();
return count;
}
});
insert into user(id,,user_id,user_name,start_time,end_time,) values (#id#,#userId#,#userName#,#startTime#,#endTime#)
(2):在sql中进行循环插入
<selecet id="batchInsert" parameterClass="java.util.List">
insert <iterator>
into user(id,user_id,user_name.start_time,end_time)
values(#list[].id#,#list[].useId#,#list[].userName#,#list[].startTime#,#list[].endTime#)
</iterator>
</select>
(二):在sql中我们会使用到in这种查询,in里面一般就如(1,2,3,...)这种形式所以我们可以用iterator来循环生成这种形式
<select id="get_user" parameterClass="java.util.Map"
resultMap="getUser">
<![CDATA[
select * from user o where o.user_role=2
]]>
<isNotEmpty prepend="and" property="list">
o.user_id IN
<iterate close=")" open="(" conjunction="," property="list">#list[]#</iterate>
</isNotEmpty>
</select>
这里必须加上property="pilesList",否则会报下面的错误
--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject
or property was not a Collection, Array or Iterator.
二是使用list当作参数
<select id="getUser" parameterClass="java.util.List" resultMap="get_user">
<![CDATA[
select * from user o where o.user_role=2
]]>
and o.pile_id IN
<iterate close=")" open="(" conjunction=",">#list[]#</iterate>
</select> 这里面就不用加property="list"这个属性,这里还有一点要注意的就是不能加成这样的
<select id="getUser" parameterClass="java.util.List" resultMap="get_user">
<![CDATA[
select * from user o where o.user_role=2
]]>
<isNotEmpty property="list">
and o.pile_id IN
<iterate close=")" open="(" conjunction=",">#list[]#</iterate>
</isNotEmpty>
</select>
如果加了这个判断也是会报错的,这点使得我很与郁闷啊,这个isNotEmpty判断不了list是否为空的,list的空值是[]写的,所以只能用if判断。
三:总结
今天知道了旁边的同事要走,其实挺无语的。不知道怎么的,我的感触还是很深的,每个人都有自己的路去选择,尽量保持平常的心态吧。我的心很大,所以我要坚定下去。努力加油。都冲吧。
最新文章
- hdu3068马拉车
- php环境搭建wampserver、Apache、Mysql和php
- CMAKE 配置选项
- 随机生成数字(ashx文件,调用上篇所写发送邮件代码)
- Android 使用xml序列化器生成xml文件
- Linux线程的实现 &; LinuxThread vs. NPTL &; 用户级内核级线程 &; 线程与信号处理
- Asp.Net 获取FileUpload控件的文件路径、文件名、扩展名
- IDE改为AHCI后系统无法启动的解决办法
- 用 OUTLOOK VBA 生成 自定义文件夹 邮件列表
- AssetManager asset使用
- 设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用
- (转)Spring 的 init-method 和 destory-method
- 使用express+multer实现node中的图片上传
- linux之间免密操作
- IPC$横向渗透代码实现
- 【python】——python3 与 python2 的那些不兼容
- protobuf example make backup
- session_id 生成原理
- node inspector
- JAVA-Eclipse中web-inf和meta-inf文件夹
热门文章
- 详解mysql体系结构和存储引擎
- 开源版本 hadoop-2.7.5 + apache-hive-2.1.1 + spark-2.3.0-bin-hadoop2.7整合使用
- 【实用】如何将sublime text 3 打造成实用的python IDE 环境
- 多个excel合并(excel2007)
- bug单的提交
- jmeter实例,如果有说明错误,请各位大神批评
- python终极篇 --- django---班级管理系统
- LINQ学习笔记——(3)基本查询操作符
- mysql数据备份和还原
- 通过 systemctl 设置自定义 Service