foreach with parameters

217 views
Skip to first unread message

javaDeveloper

unread,
Jul 18, 2012, 12:05:05 PM7/18/12
to mybati...@googlegroups.com
I have the following select element defined in a mapper file:

<select id="getData" parameterType="HashMap" resultType="HashMap">
         select COL_A from myTable where CONO = #{CONO} and LOCN = #{LOCN} and CUST = #{CUST}
     </select>

I want to change this element to look for a series of CUST values, but still use the other filter values (CONO and LOCN).  This means converting it from
CUST = #{CUST}
to
CUST IN (.....)

I looked at the mybatis documentation and see that foreach handles the in clause, but I don't see an example that passes other parameters (ie. a parameterType) that are necessary for the query. Can someone provide me such an example?

Thanks

Javier García Venero

unread,
Jul 18, 2012, 12:10:10 PM7/18/12
to mybati...@googlegroups.com
Hello, i have an example that use a foreach to construct in (listElement, listElement..)

....
<if test="registro.listaRoles != null">
      and r.COD_ROL_ENTIDAD  IN 
      <foreach item="rol" index="index" collection="registro.listaRoles" open="(" separator="," close=")" >
#{rol.codRol, jdbcType=DECIMAL}
</foreach>        
</if>

2012/7/18 javaDeveloper <vsmit...@yahoo.com>

javaDeveloper

unread,
Jul 18, 2012, 12:45:30 PM7/18/12
to mybati...@googlegroups.com

Javier,

I used your example and it worked. Thanks alot!


On Wednesday, July 18, 2012 11:10:10 AM UTC-5, Javier Garcia Venero wrote:
Hello, i have an example that use a foreach to construct in (listElement, listElement..)

....
<if test="registro.listaRoles != null">
      and r.COD_ROL_ENTIDAD  IN 
      <foreach item="rol" index="index" collection="registro.listaRoles" open="(" separator="," close=")" >
#{rol.codRol, jdbcType=DECIMAL}
</foreach>        
</if>

2012/7/18 javaDeveloper
I have the following select element defined in a mapper file:

Javier García Venero

unread,
Jul 18, 2012, 5:03:15 PM7/18/12
to mybati...@googlegroups.com
you're welcome :)

2012/7/18 javaDeveloper <vsmit...@yahoo.com>
Reply all
Reply to author
Forward
0 new messages