Mybatis 注解开发传入List 两种方式接收方式 在IN场景中

1.在@Select中

@Select("<script>"
              + "SELECT IDFA FROM t_xxx WHERE IDFA IN "
              + "<foreach item=‘item‘ index=‘index‘ collection=‘strList‘ open=‘(‘ separator=‘,‘ close=‘)‘>"
                  + "#{item}"
              + "</foreach>"
          + "</script>")
    @Results(value = { @Result(column = "user_name", property = "username") })
    public List<String> getXxxList(@Param("strList") List<String> strList);

说明:上述方式其实是一种注解完全代替xml的方法。

其中的foreach的collection直接写成@param中的值即可。

摘自:https://www.cnblogs.com/java-zhao/p/5489269.html

2.将入参进行封装,成(**,**,**,**,)

@Select("select access_pat_id from emr3.gp_doc_patients where access_pat_id in (#{apis}) and admission_time = (select max(admission_time) from emr3.gp_doc_patients where access_pat_id in (#{apis}))")
String getMaxTime(@Param("apis")String apis);
public static String indexForm(List<String> s){
String content="";
int i=0;
for (String ss:s){
i++;
if (i==s.size()){
content+=ss;
}else {
content+=ss+",";
}
}
return content;
}
该方法返回值就是入参apis

相关推荐