Foreach is mainly used in building in conditions, it can iterate over a collection in SQL statements. The attributes of the foreach element mainly include item, index, collection, open, separator, and close. Item represents the alias when each element in the collection is iterated. Index specifies a name to represent the position to which each iteration is reached during the iteration process. Open represents what starts with the statement, separator represents what symbols are used as separators between each iteration, and close represents what ends with. The most critical and most error-prone thing when using foreach is the collection attribute. This attribute must be specified, but in different cases, the value of the attribute is different. There are three main situations:
1. If the passed in a single parameter and the parameter type is a List, the collection attribute value is list
2. If the passed in a single parameter and the parameter type is an array, the property value of the collection is array
3. If there are multiple parameters passed in, we need to encapsulate them into a map. Of course, a single parameter can also be encapsulated into a map. In fact, if you pass in parameters, you will also encapsulate them into a map in the breast. The map's key is the parameter name, so at this time the collection attribute value is the key of the passed List or array object in the map encapsulated by itself.
Let’s take a look at the example codes for the above three situations:
1. Type of single parameter List:
<select id="dynamicForeachTest" resultType="Blog">select * from t_blog where id in<foreach collection="list" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>The value of the above collection is list, and the corresponding mapper is like this
public List<Blog> dynamicForeachTest(List<Integer> ids);
Test code:
@Testpublic void dynamicForeachTest() {SqlSession session = Util.getSqlSessionFactory().openSession();BlogMapper blogMapper = session.getMapper(BlogMapper.class);List<Integer> ids = new ArrayList<Integer>();ids.add(1);ids.add(3);ids.add(6);List<Blog> blogs = blogMapper.dynamicForeachTest(ids); for (Blog blog: blogs)System.out.println(blog);session.close();}2. Type of single parameter array:
<select id="dynamicForeach2Test" resultType="Blog">select * from t_blog where id in<foreach collection="array" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>The above collection is array, corresponding Mapper code:
public List<Blog> dynamicForeach2Test(int[] ids);
Corresponding test code:
@Testpublic void dynamicForeach2Test() {SqlSession session = Util.getSqlSessionFactory().openSession();BlogMapper blogMapper = session.getMapper(BlogMapper.class);int[] ids = new int[] {1,3,6,9};List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);for (Blog blog : blogs)System.out.println(blog);session.close();}3. Encapsulate the parameters into the type of Map by yourself
<select id="dynamicForeach3Test" resultType="Blog">select * from t_blog where title like "%"#{title}"%" and id in<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>The value of the above collection is ids, which is the key of the parameter Map passed in, and the corresponding Mapper code:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
Corresponding test code:
@Testpublic void dynamicForeach3Test() {SqlSession session = Util.getSqlSessionFactory().openSession();BlogMapper blogMapper = session.getMapper(BlogMapper.class);final List<Integer> ids = new ArrayList<Integer>();ids.add(1);ids.add(2);ids.add(3);ids.add(6);ids.add(7);ids.add(9);Map<String, Object> params = new HashMap<String, Object>();params.put("ids", ids);params.put("title", "China");List<Blog> blogs = blogMapper.dynamicForeach3Test(params);for (Blog blog: blogs)System.out.println(blog);session.close();}The above is the relevant knowledge of the detailed explanation of the foreach statement of MyBatis introduced to you by the editor. I hope it will be helpful to you!