mybatis传入list、array等数据集合的处理

先上代码
//在dao类里面
int deleteBatchQuestion(@Param(value = "questionID") List<Integer> questionID); 
//在xml里面
<delete id="deleteBatchQuestion" parameterType="java.util.List">
    DELETE from single_question where
        Single_QuestionID
        <foreach collection="questionID" item="Single_QuestionID"
                index="index" open="in(" close=")" separator=",">
                #{questionID[${index}]}
            </foreach>
    </delete>

 

这里用到了mybatis的foreach元素,该元素下有 collection、item、index、open、close、separator六个属性。其中collection中要写方法声明里@Param中注解的名字(网上很多版本都说可以直接用list表示同时去掉注解,但我没有成功)。其他六种属性看名字就很好理解了。整条语句会在拼接成 DELETE from single_question where Single_QuestionID IN
(1,2,3...) 类似与这样的语句。其实在Spring接管mybatis之后,每一条sql命令执行前都会对sql进行拼接,然后再去执行。可以使用IDEA自带的反编译工具就可以直接调整sql执行的整个过程(可能过程比较无聊,但谁让咱做程序员来,多看源码还是很有帮助滴。。。。)


这里再说一下和不一样的地方就是
#{questionID[${index}]}
这个地方要写成这样,网上很多人这么写 {questionID} 可惜在我这里经常报错。。。。。

 

posted on 2017-10-27 20:17  zhd_七  阅读(1199)  评论(0编辑  收藏  举报