foreach用法
SQL语句中有时会使用in关键字,如id in {1,2,3},我们可以使用${ids}方式直接获取值,但是这种方法不能防止SQL注入,想避免SQL注入的话就需要使用#{id}的方式,这时我们就可以配合使用foreach标签了。foreach可以对数组、Map或实现了Iterable接口(List、Set)的对象进行遍历。数组在处理时会转换为List对象,因此foreach遍历的对象可以分为两大类:Iterable类型和Map类型。
foreach实现in集合
foreach实现in集合或数组是最简单和最常用的一种情况。假设,我们需要根据传入的用户id集合来查询所有符合条件的用户。在接口类中写一个方法,selectByIdList(List
//foreach实现in集合
List selectByIdList(List idList);
foreach中包含以下属性:
collection属性的设置:
当参数类型为集合的时候,默认会转换为Map类型,并添加一个key为collection的值,如果参数类型是List集合,那么就继续添加一个key为list的值,这样当collection = list时就能得到这个集合,并对它进行循环操作。当参数类型为数组的时候,也会转换成Map类型,默认的key值为array。如selectByIdList(List
当有多个参数的时候,就要使用@Param注解给每个参数指定一个名字,否则在SQL中使用参数时就会不方便,因此将collection设置为@Param注解指定的名字即可。
使用Map和使用@Param注解方式类似,将collection指定为对应Map中的key即可。如果要循环所传入的Map,推荐使用@Param注解指定名字,此时可将collection设置为指定的名字,如果不想指定名字,就是用默认值_parameter。
这种情况下指定为对象的属性名即可。当使用对象内多层嵌套的对象时,使用属性.属性(集合和数组可使用下标取值)的方法指定深层的属性值。
测试方法:
@Test
public void testSelectByIdList() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List idList = new ArrayList();
idList.add(1001L);
idList.add(1002L);
idList.add(1003L);
idList.add(1004L);
List userList = userMapper.selectByIdList(idList);
for (User user : userList) {
System.out.println(user);
}
} finally {
sqlSession.close();
}
}
foreach实现批量插入
定义一个方法:insertList()
//foreach实现批量插入
int insertList(List userList);
insert into t_user(user_name,user_password,user_email,user_info,head_img,create_time)
values
(#{user.userName},#{user.userPassword},#{user.userEmail},#{user.userInfo},
#{user.headImg,jdbcType=BLOB},#{user.createTime,jdbcType=TIMESTAMP})
@Test
public void testInsertList() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List userList = new ArrayList();
for(int i = 1; i <= 3; i++) {
User user = new User();
user.setUserName("study" + i);
user.setUserPassword("!@#$%^");
user.setUserEmail("study@study");
user.setUserInfo("okokok");
user.setHeadImg(new byte[]{1});
user.setCreateTime(new Date());
userList.add(user);
}
int res = userMapper.insertList(userList);
if (res > 0) {
System.out.println(res);
System.out.println("插入成功!");
}
} finally {
sqlSession.commit();
sqlSession.close();
}
}
foreach实现动态update
当参数是Map,foreach标签的index属性值对应的不是索引值,而是Map中的key,利用这个key可以实现动态更新。
//foreach实现动态更新
int updateByMap(Map map);
update t_user set
${key} = #{val}
where id = #{id}
key作为列名,对应的值作为该列的值,通过foreach将需要更新的字段拼接在SQL语句中。
@Test
public void testUpdateByMap() {
SqlSession sqlSession = getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
//查询条件,也是更新条件,必须保证值存在
map.put("id",1007L);
//要更新的其他字段
map.put("user_password","testst");
map.put("user_email","testst@study");
int res = userMapper.updateByMap(map);
if(res > 0) {
System.out.println(res);
System.out.println("更新成功!");
}
} finally {
sqlSession.commit();
sqlSession.close();
}
}
至此,foreach的用法就到此为止了。