Thursday, June 2, 2011

Parameterizing the table name in iBATIS (use of DYNAMIC tags in sql)


Notice the difference in syntax of parameters passed for schema or table name versus the parameters in sql condition like Where clause or And clause.

For table name use $paramName$ and for other conditions use #paramName#!

     
      <resultMap id="jokersMap" class="com.ragsetty.jokers.Kilaadi">
            <result property="name" column="PERSON_NAME" />
            <result property="height" column="PERSON_HEIGHT" />
            ......
            ......
      </resultMap>



      <statement id="findAllJokers" parameterClass="java.util.Map" resultMap="jokersMap">
            SELECT *
            <dynamic prepend="FROM">
                  <isNotNull property="tableName">
                        $tableName$
                  </isNotNull>
                  <isNull property="tableName">
                        MY_SCHEMA.DEFAULT_PERSON_TABLE
                  </isNull>
            </dynamic>
            WHERE PERSON_TYPE = #personType#
      </statement>

3 comments:

  1. hello, does anyone know how to do an update of a table in ibatis having that the table name is dynamic?

    ReplyDelete
  2. Thanks, nice tip

    ReplyDelete