[JAVA] [MyBatis] List <Map <>> Pass nesting as Mapper parameter

I want to nest it in the Mapper parameter of MyBatis!

--Map is possible with key and value --List can be nested normally

environment

Development PC: Windows 10 STS: 4 mybatis:3.2.5 mybatis-spring:1.2.2 Java: 8

Official documentation

http://www.mybatis.org/mybatis-3/ja/dynamic-sql.html No particular explanation for nesting

SQL ready

--Mapper: xml

SampleMapper.xml


  <select id="selectTestMapList" resultMap="BaseResultMap">
  SELECT
    sample.*
  FROM
    sample
  WHERE
    sample.id IN
    <foreach item="internalMap" collection="nestedMapList">
        <foreach item="value" index="key" collection="internalMap"  open="(" separator="," close=")">
            #{value}
        </foreach>
    </foreach>
  </select>

java definition

--Mapper: java

SampleMapper.java


List<Sample> selectTestMapList(@Param("nestedMapList") List<Map<String, Integer>> nestedMapList);

Try to run

TestMapperExecutor.java


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/sample/spring/test-context.xml"})
public class TestMapperExecutor {

	@Autowired
	private SampleMapper sampleMapper;

	@Test
	public void test() {
		List<Map<String, Integer>> nestedMapList = new CopyOnWriteArrayList<>();
		Map<String, Integer> internalMap = new ConcurrentHashMap<>();
		internalMap.put("iKey", 1);
		nestedMapList.add(internalMap);

		List<Sample> result = null;
		try {

			result = sampleMapper.selectTestMapList(nestedMapList);
		}
		catch (Throwable t) {
			t.printStackTrace();
		}

	}

}

Thank you for your hard work!

Recommended Posts

[MyBatis] List <Map <>> Pass nesting as Mapper parameter
List, Set, Map
Array / list / map