我有一个对象列表,我想迭代并访问ibatis sql中的特定字段。在Ibatis中迭代对象列表
Ex。
public Class Student
{
String id;
String name;
}
我将通过为参数Student对象(名单(学生))
的名单,做迭代访问ID为每个对象的bean。我该怎么做呢?
我有一个对象列表,我想迭代并访问ibatis sql中的特定字段。在Ibatis中迭代对象列表
Ex。
public Class Student
{
String id;
String name;
}
我将通过为参数Student对象(名单(学生))
的名单,做迭代访问ID为每个对象的bean。我该怎么做呢?
The foreach -tag是你在找什么。示例:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
有关更多信息,请参见user guide“动态sql”一章。
顺便说一句,iBatis不再被开发和冻结,现在被称为“MyBatis”,整个开发团队从Apache移动到new MyBatis home。
一个简单的例子。
<select id="selectFewStudents" resultMap="MyMap" parameterClass="list">
select * from student_table where student_id in
<iterate open="(" close=")" conjunction=",">
#[]#
</iterate>
</select>
有关更多信息,请参阅iBatis documentation。
正如塞勒指出的是,Java相当于将
<select id="selectFewStudents" resultType="MyMap">
select * from student_table where student_id in
<foreach item="currentRow" index="rowNum" collection="list" open="(" separator="," close=")">
#{currentRow}
</foreach>
</select>
iBatis的让你的变量项目和索引,你可以在循环中使用。
尝试类似:
<select id="StudentsQry" parameterClass="list">
select * from STUDENTS where
(id, name) in
<iterate open="(" close=")" conjunction="," >
(#[].id# , #[].name#)
</iterate>
<select>
其中id
和name
是Student
类的字段和我parameterClass
是List<Student>
。
为我工作。
动态生成的SQL的样子:
select * from STUDENTS where (id, name) in ((1,'a'), (2,'b'))
自我解释,很好。 – Saif 2014-08-26 09:43:01
如何在parameterClass属性中指定包含'<' and '>'的类? – RyanHennig 2016-05-03 20:50:30
谢谢。我会试着看看这是否存在于ibatis2中,也许会改为mybatis。 – cedric 2010-08-02 04:10:32