zoukankan      html  css  js  c++  java
  • 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} 可惜在我这里经常报错。。。。。

  • 相关阅读:
    Oracle11以后的行列转换
    stream重复Key的处理
    EasyUI笔记(一)Base基础
    jQuery笔记(六)jQuery之Ajax
    【jQuery实例】Ajax登录页面
    jQuery笔记(五)jQuery表单验证
    jQuery笔记(四)jQuery中的动画
    jQuery笔记(三)jQuery中的事件
    jQuery笔记(二)jQuery中DOM操作
    jQuery笔记(一)jQuery选择器
  • 原文地址:https://www.cnblogs.com/luckyQi/p/7745031.html
Copyright © 2011-2022 走看看