• ibatis传入list对象


    在使用ibatis的时候经常需要传入list对象,sql语句如下。

    <select id="GET-PERSONS" parameterClass="java.util.ArrayList" resultClass="pojo.Person">
        <![CDATA[
                select * from  person where id in
        ]]>
        <iterate  open="(" close=")" conjunction=",">  
                #list[]#
        </iterate>
    </select>

    这个是简单的sql语句,对于list中是别的对象的,比如List<Person>这个参数传进来时需要这样使用

    <select id="GET-PERSONS" parameterClass="java.util.List" resultClass="pojo.Person">
        <![CDATA[
                select * from  person where id in
        ]]>
        <iterate open="(" close=")" conjunction=",">  
            <![CDATA[ 
                #list[].id#
            ]]>
        </iterate>
    </select>

    注意:上面select语句入参用的是parameterClass是java.util.ArrayList类,而不是一个map,这时iterator语句就不需要有property="ids"属性,即

    <select id="GET-PERSONSS" parameterClass="java.util.ArrayList" resultClass="pojo.Person">
        <![CDATA[
                select * from  person where id in
        ]]>
        <iterate property="ids" open="(" close=")" conjunction=",">  
                #ids[]#
        </iterate>
    </select>

    如果有这个属性的话ibatis会从参数中去找属性为ids这个对象,而参数是一个list没有这个属性,因此就会报错:

    Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
    --- The error occurred in person-sqlmap.xml.
    --- The error occurred while preparing the mapped statement for execution.
    --- Check the person.GET-PERSONS.
    --- Check the parameter map.
    --- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal list fr
    om JavaBean. Cause java.lang.StringIndexOutOfBoundsException: String index out o
    f range: -1

    如果parameter使用map,那么需要在java代码中将这个list封装进map。

  • 相关阅读:
    带你了解 MySQL Binlog 不为人知的秘密
    算法的空间复杂度
    算法的时间复杂度
    xargs--冬天里的一丝暖意
    "三剑客"之awk心中无剑
    hadoop之yarn(优化篇)
    对hadoop RPC的理解
    mysql优化篇(基于索引)
    [python学习手册-笔记]003.数值类型
    [python学习手册-笔记]001.python前言
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/4753452.html
Copyright © 2020-2023  润新知