网站开发端口查询,织梦英文版网站怎么做,南开区网站建设,医院网站跳出率高一、xml文件中foreach的主要属性 foreach元素的属性主要有 collection#xff0c;item#xff0c;index#xff0c;separator#xff0c;open#xff0c;close。 collection: 表示集合#xff0c;数据源 item #xff1a;表示集合中的每一个元素 index #xff1a;用于… 一、xml文件中foreach的主要属性 foreach元素的属性主要有 collectionitemindexseparatoropenclose。 collection: 表示集合数据源 item 表示集合中的每一个元素 index 用于表示在迭代过程中每次迭代到的位置 separator 表示在迭代时数据以什么符号作为分隔符 open 表示该语句以什么开始 close 表示以什么结束 二、foreach批量查询数据 1、当查询的参数只有一个时 例如findByIds(List ids) a.如果参数类型为List在使用时collection的属性需指定为list b.如果参数类型为数组则在使用时collection属性需指定为array select idfindByIdsMap resultMapBaseResultMap Select sum(mark) from table_user where user_id in foreach itemitem indexindex collectionlist open( separator, close) #{item} /foreach /select 2、当查询的参数是一个对象时 例如findByIds(List userList) select idfindByIdsMap resultMapBaseResultMap Select sum(mark) from table_user where user_id in foreach itemitem indexindex collectionlist open( separator, close) #{item.userId} /foreach /select 三、foreach批量插入数据 实现foreach批量插入数据有两种方法一种是只发送一条 SQL插入的多条数据之间通过”,” 分隔开另一种方式是每插入一条数据就发送一条 SQL 语句多个 SQL 语句之间用“”分割。 1.一条 SQL 批量插入数据 对应的Mapper接口代码如下 /** 返回值为 Integer 类型 */ Integer addStudentByList(Param(list) ListStudent list); 1 2 对应的SQL 映射文件如下 insert idaddStudentByList parameterType INSERT INTO Student(username, age, password) VALUES foreach collectionlist itemitem separator, (#{ item.username}, #{ item.age}, #{ item.password}) /foreach /insert 2.执行多条 SQL 批量插入数据 对应的Mapper接口代码和一条 SQL 批量插入数据相同 对应的SQL 映射文件在一条 SQL 批量插入数据的基础上修改如下 insert idaddEmpsByList parameterTypecom.jas.mybatis.bean.Employee !-- 每插入一条数据就执行一次 SQL中间用;分隔开 -- foreach collectionlist itememp separator; INSERT INTO Student(username, age, password) VALUES (#{ item.username}, #{ item.age}, #{ item.password}) /foreach /insert 三、foreach批量更新数据 实现foreach批量插入数据有两种种方法第一种是一条sql语句来批量更新数据第二种是批量更新的sql。 一条 SQL 批量更新数据 一条sql语句来批量更新所有数据下面直接看一下在mybatis中通常是怎么写的去掉mybatis语法就是原生的sql语句了所有就没单独说sql是怎么写的 对应的SQL 映射文件如下 update idupdateBatch parameterTypejava.util.List update Student set username foreach collectionlist itemitem indexindex separator opencase ID closeend when #{ item.id} then #{ item.username} /foreach where id in foreach collectionlist indexindex itemitem separator, open( close) #{ item.id,jdbcTypeBIGINT} /foreach /update ------------------------------------分隔符-------------------------------- update idupdateBatch parameterTypejava.util.List foreach collectionlist itemitem indexindex open close separator; update Student set username${ item.username} /set where id ${ item.id} /foreach /update 四、foreach批量删除数据 所对应的SQL映射文件如下 delete iddelAssetstype parameterTypejava.util.List DELETE FROM WHERE id in foreach collectionlist itemitem open( close) separator, #{ item} /foreach /delete 对应的Mapper接口代码如下 public void deleteAssetstype(ListInteger ids);