-
配置文件(Oracle):
<!– 批量插入临时表–>
<insert id=”insertTempPhoneBatch”parameterType=”java.util.HashMap” >
insert into ce_tempPhone_info(phone)
(
<foreach collection=”list”item=”item” separator=”union”opne=”” close=”” index=””>
select #{item,jdbcType=VARCHAR} from dual
</foreach>
)
</insert>
Insert intoce_tempPhone_info(phone) (select xxx from dual union select yyy from dual …..);
-
解析:
-
parameterType:
-
参数的类型可以是java.util.HashMap或者java.util.List
-
如果传入的类型是List的话可以两者都可以使用,List的会被封装成Map类型的;
-
-
collection:
-
如果传入的是list集合,则此处写list;
-
数组类型,此处array;
-
-
item:
-
循环的时候的变量;
-
如果传入的是list或array,则使用的时候直接使用#{item,jdbcType=VARCHAR}即可;
-
如果list中泛型是对象的话,必须使用#{item.phone,jdbcType=VARCHAR}类似形式
-
-
index:
-
索引;
-
-
open:
-
查询以什么开始;
-
如:open=”(“,则该foreach会以“(“开头;
-
-
close:
-
以什么符号结束;
-
-
separator:
-
连接符,以什么进行每次循环的连接符;
-
-
-
Java类:
Ce_sample_info info = new Ce_sample_info();
info.setProject_id(project_id);
for(int i=0;i<phones.length;i++){
info.setPhone(phones[i]);
//查询当前project_id和phone在ce_sample_info表中是否存在,不存在则插入
int count = baseDao.selectOne(“ce_sample_infoMapper.queryProjectPhoneCount”,info);
System.out.println(“==========count===========” +count);
if(count == 0) {
String id =baseDao.selectOne(“ce_sample_infoMapper.querySampleSeq”);
info.setSample_id(id);
baseDao.insert(“ce_sample_infoMapper.insertSelective”,info);
}
}