2013-04-15 59 views
-1

嗨我想通过ibatis插入对象列表,但我得到一个异常,如 org.apache.ibatis.mapping.SqlMapperException:表达式'boxlist'评估为空值。Ibatis插入操作

<insert id="insertList" parameterType="java.util.List"> 
    INSERT INTO boxtable (
    size,length) 
    VALUES 
    <foreach item="box" collection="boxList" separator=","> 
     (#{box.size},#{box.length}) 
    </foreach> 
</insert> 

吾道类就像

public void insert(List<box> boxList) 
     throws SQLException { 

    try {        
     sqlSession = sqlSessionFactory.openSession(AUTO_COMMIT); 
     int status = sqlSession.insert("insertlist", boxList); 
     logger.debug("status :: " + status); 
     sqlSession.commit(); 
    } catch (Throwable ee) { 
     logger.error("e", ee); 
    sqlSession.rollback(); 
    } finally { 
     sqlSession.close(); 
    } 

} 

谁能帮助我???

+0

错误信息很清楚我认为。你发送到表达式的值计算为'null' – NilsH

+0

没有列表不是空的..我从其他函数调用该函数并传递列表 –

回答

0

尝试在您的sql语句中使用<isPropertyAvailable>之前的<foreach>。你也可以尝试使用<iterate>但不要忘记追加[],以乌尔boxlist(如#boxlist[]#

更多关于动态SQL语句在ibatis的遵循这一link

1

如果我记得是正确的,你需要传递包含boxList属性的对象,或者您可以传递包含boxList密钥的地图。例如

public class Wrapper { 
    private List<box> boxList; 
} 


<insert id="insertList" parameterType="Wrapper"> 
    INSERT INTO boxtable (
    size,length) 
    VALUES 
    <foreach item="box" collection="boxList" separator=","> 
     (#{box.size},#{box.length}) 
    </foreach> 
</insert> 
+0

我必须传递一个包含对象的列表 –

+0

创建一个类似Wrapper并在该对象中设置框列表并传递该对象。 –

+0

你试过了吗?任何问题? –