Java 迭代 Ibatis 中的对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3360070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate list of Objects in Ibatis
提问by cedric
I have a list of object where I want to iterate and access a particular field in ibatis sql.
我有一个对象列表,我想在其中迭代和访问 ibatis sql 中的特定字段。
Ex.
前任。
public Class Student
{
String id;
String name;
}
I will pass as parameter a List of Student object(List(Student))
and do iteration accessing the id for each object bean. How do I do this?
我将作为参数传递一个学生对象列表(List(Student))
并迭代访问每个对象 bean 的 id。我该怎么做呢?
采纳答案by Sylar
The foreach-tag is what you are looking for. Example:
该的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>
See the user guidefor more info, chapter "dynamic sql".
有关更多信息,请参阅用户指南,“动态 sql”一章。
By the way, iBatis is no longer developed and is frozen, it is now called "MyBatis" and the whole developer team moved away from Apache to the new MyBatis home.
顺便说一下,iBatis 不再开发并被冻结,现在称为“MyBatis”,整个开发团队从 Apache 搬到了 新的 MyBatis 家。
回答by chedine
A simple example.
一个简单的例子。
<select id="selectFewStudents" resultMap="MyMap" parameterClass="list">
select * from student_table where student_id in
<iterate open="(" close=")" conjunction=",">
#[]#
</iterate>
</select>
Refer iBatis documentationfor more info.
有关更多信息,请参阅iBatis 文档。
As Sylar pointed out, the java equivalent would be
正如 Sylar 所指出的,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 allows you to variables item and index which you can use inside the loop.
iBatis 允许你在循环中使用变量 item 和 index。
回答by dillip pattnaik
Try something like:
尝试类似:
<select id="StudentsQry" parameterClass="list">
select * from STUDENTS where
( id, name ) in
<iterate open="(" close=")" conjunction="," >
( #[].id# , #[].name# )
</iterate>
<select>
where id
and name
are fields of Student
class and my parameterClass
is List<Student>
.
whereid
和name
是Student
类的字段,而 myparameterClass
是List<Student>
.
Worked for me.
为我工作。
The sql formed dynamically will look like:
动态形成的 sql 将如下所示:
select * from STUDENTS where ( id, name ) in ( (1,'a'), (2,'b') )