zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    SQL server 统计数据库表数量和列出所有表名称
    mybatis 模糊查询 like的三种方式
    jquery 实现按回车键登录功能的写法
    js 各种事件 如:点击事件、失去焦点、键盘事件等
    ssm框架中从controller传值给jsp的方式
    [GDOI2019]小说
    洛谷5113
    2020.9.26模拟总结
    [IOI2015]分组
    9.19 总结
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/4753452.html
Copyright © 2011-2022 走看看