Mybatis之foreach标签
案例:通过foreach标签实现如下sql查询,并在测试类中传入参数:
select * from mybatis.blog where id in=(1 or 2 or 3)
mapper配置添加查询sql:
<select id="queryBlogForeach" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<foreach collection="idList" item="id" open="(" close=")" separator=" or ">
id=#{id}
</foreach>
</where>
</select>
测试类实现:
@Test
public void test5(){
SqlSession sqlSession = MybatisUtils.getSqlSession();//建立一个SqlSession对象
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map =new HashMap();
ArrayList<Integer> idList=new ArrayList<Integer>();
idList.add(1);
idList.add(2);
idList.add(3);
map.put("idList",idList);
List<Blog> blogs =mapper.queryBlogForeach(map);
for (Blog blog : blogs)
{
System.out.println(blog);
}
}
日志结果截图:
有不明白的童鞋可以评论提问哦,尽量当天详细回复。
创作不易,点个赞吧~~