ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: select with iterate
Date Sun, 24 Apr 2005 06:41:31 GMT
<iterate> can be used in any <statement> tag. <insert> and <update>
are
extensions of the <statement> tag. For readability and maintainablity,
I would make seperate calls in your dao class. I suppose you could make
up some kind of giant INSERT statment using <iterate>:

<iterate property="idList">
INSERT INTO content (ListId, Body) VALUES (#idList[]#, null); 
GO
</iterate>

that would generate and execute the following statements:

INSERT INTO content (ListId, Body) VALUES (1, null);
GO
INSERT INTO content (ListId, Body) VALUES (2, null); 
GO
INSERT INTO content (ListId, Body) VALUES (3, null); 
GO

but that looks like it would be more prone to errors...

- Ron

--- Jason Punzalan <jasonpunzalan@gmail.com> wrote:
> Ahh...very helpful. thank you. what about inserts? can you use
> iterate to 
> execute multiple inserts or updates? or do i have to make separate
> calls in 
> my dao class?
> 
> 
> 
> On 4/23/05, Ron Grabowski <rongrabowski@yahoo.com> wrote:
> > 
> > I believe your code would incorrectly generate the following sql
> > statements (all on one line):
> > 
> > select * from content where id = 1
> > select * from content where id = 2
> > select * from content where id = 3
> > ...
> > 
> > Take a look at the these test cases:
> > 
> > http://tinyurl.com/dadn5
> > 
> >
>
http://svn.apache.org/repos/asf/incubator/ibatis/trunk/java/mapper/mapper2/test/com/ibatis/sqlmap/maps/DynamicAccount.xml
> > 
> > You probably want to do something like this:
> > 
> > SELECT * FROM content WHERE id IN
> > <iterate property="idList" open="(" close=")" conjunction=",">
> > #idList[]#
> > </iterate>
> > 
> > That would generate this sql:
> > 
> > SELECT * FROM content WHERE id IN (1,2,3)
> > 
> > If you want three seperate queries, your dao class would need to
> make
> > three seperate calls to the database.
> > 
> > --- Jason Punzalan <jasonpunzalan@gmail.com> wrote:
> > > Can someone show me an example of doing a select with iterate? Is
> it
> > > possible to do the following....
> > >
> > > <statement id="getAllByList" resultClass="Content"
> > > parameterClass="Content"
> > > resultMap="content-result" >
> > > <dynamic>
> > > <iterate property="idList">
> > > select * from content where
> > > id = #idList[]#
> > > </iterate>
> > > </dynamic>
> > > </statement>
> > >
> > > I'm currently getting an java.sql.SQLException...running mysql.
> > >
> > > Thanks
> > >
> >
> 

Mime
View raw message