packagecom.example.booking.mapperimportcom.example.booking.model.PrefectureCelebritiesimportcom.example.booking.model.Personimportorg.apache.ibatis.annotations.*@MapperinterfacePersonMapper{// XMLファイル・アノテーション併用のMapper
@Select("""
<script>
SELECT
prefecture_id,
person_name,
person_age
FROM
person
WHERE
person_id IN
<foreach item="id" collection="idList" separator="," open="(" close=")">
#{id}
</foreach>
ORDER BY prefecture_id, person_id
</script>
""")@ResultMap("com.example.booking.mapper.PersonMapper.prefectureCelebrities")funselectPrefectureCelebritiesWithXml(@Param("idList")idList:List<Int>):List<PrefectureCelebrities>// アノテーションだけで実装したMapper
@Select("""
<script>
SELECT
prefecture_id
FROM
person
WHERE
person_id IN
<foreach item="id" collection="idList" separator="," open="(" close=")">
#{id}
</foreach>
GROUP BY prefecture_id
ORDER BY prefecture_id
</script>
""")@Results(Result(property="prefectureId",column="prefecture_id"),Result(property="personList",column="prefecture_id",javaType=List::class,many=Many(select="selectPerson")))funselectPrefectureCelebritiesWithMany(@Param("idList")idList:List<Int>):List<PrefectureCelebrities>// @Manyで必要になるSELECT文
@Select("""
SELECT
person_name,
person_age
FROM
person
WHERE
prefecture_id = #{prefectureId}
ORDER BY person_id
""")@Results(Result(property="name",column="person_name"),Result(property="age",column="person_age"))funselectPerson(@Param("prefectureId")prefectureId:Long):Person}
packagecom.example.booking.mapperimportcom.example.booking.config.MyBatisTestConfigimportorg.assertj.core.api.BDDAssertionsimportorg.assertj.core.api.junit.jupiter.SoftAssertionsExtensionimportorg.junit.jupiter.api.Testimportorg.junit.jupiter.api.extension.ExtendWithimportorg.mybatis.spring.annotation.MapperScanimportorg.mybatis.spring.boot.test.autoconfigure.MybatisTestimportorg.springframework.beans.factory.annotation.Autowiredimportorg.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabaseimportorg.springframework.context.annotation.Importimportorg.springframework.test.context.ContextConfigurationimportorg.springframework.test.context.jdbc.Sqlimportkotlin.test.assertNotNull@MybatisTest@MapperScan@ContextConfiguration(classes=[MyBatisTestConfig::class])@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)@ExtendWith(SoftAssertionsExtension::class)@Import(MyBatisTestConfig::class)classPersonMapperTest{@AutowiredlateinitvarpersonMapper:PersonMapper@Test@Sql(scripts=["classpath:/sql/prefecture_celebrities.sql"])funselectPrefectureCelebritiesWithXml(){validList=listOf(1,2,3,4,5)valresult=personMapper.selectPrefectureCelebritiesWithXml(idList)assertNotNull(result)// XMLで記述したresultMapによって、prefectureIdで集約される
BDDAssertions.then(result).hasSize(2)BDDAssertions.then(result[0].personList).hasSize(3)BDDAssertions.then(result[1].personList).hasSize(2)}@Test@Sql(scripts=["classpath:/sql/prefecture_celebrities.sql"])funselectPrefectureCelebritiesWithMany(){validList=listOf(1,2,3,4,5)valresult=personMapper.selectPrefectureCelebritiesWithMany(idList)assertNotNull(result)// GROUP BY を使うことで prefectureId で集約できる
BDDAssertions.then(result).hasSize(2)BDDAssertions.then(result[0].personList).hasSize(3)BDDAssertions.then(result[1].personList).hasSize(2)}}