How to Pass Array to String to IN Clause Using MyBatis annotation

1.1k views Asked by At

I am using below Code Snippet for IN clause using Mybatis annotation.I am not getting any exception on execution but it returns 0 rows.
Please help me on this.

@Select("SELECT A.* FROM RPRT_HDR A,SCHL_MSTR B WHERE A.SCHL_ID=B.SCHL_ID AND B.SCHOOL_NAME in (#{schools}) AND A.YR_MTH=#{ym}")
@Results(value={
@Result(property ="rprtKey",column="RPRT_KEY"),
@Result(property="reportImage",javaType = List.class,column ="rprtKey",many=@Many(select="getImages")),
@Result(property="reportQnAns",javaType = List.class,column ="rprtKey",many=@Many(select="getQuestionAnswer"))            
})        
List<ReportHeader> getReportdata(@Param("schools")String[] schools,@Param("ym")int yearmonth );
1

There are 1 answers

0
blackwizard On BEST ANSWER

The query works because Mybatis actually send the String array toString, then is is valid, but there is merely no record to match the string. Write:@Select("<script>SELECT A.* FROM RPRT_HDR A,SCHL_MSTR B WHERE A.SCHL_ID=B.SCHL_ID AND B.SCHOOL_NAME in (<foreach collection='schools' item='school' separator=', '>#{school}</foreach>) AND A.YR_MTH=#{ym}</script>")