Posted
Filed under spring framework
Currently multiple parameters must be declared in the mapper interface using @Param annotation:

public interface SomeMapper {
    public SomeClass getWith2Params(@Param("p1") int p1, @Param("p2") String p2);
}

And then in mapper file:

<select id="getWith2Params" resultType="SomeClass">
        SELECT *
        FROM SomeTable
        WHERE p1 = #{p1} AND p2= #{p2}
</select>

Unlike single parameter, multiple parameters are not declared in the same place where they are used and this makes the coding less productive,
maintainable and readable.

An alternative to annotation for having full declaration and usage in the mapper file would be something like:

<select id="getWith2Params" parameters="int p1; string p2" resultType="SomeClass">
        SELECT *
        FROM SomeTable
        WHERE p1 = #{p1} AND p2= #{p2}
</select>

or

<select id="getWith2Params" parameters="int; string" resultType="MyClass">
        SELECT *
        FROM SomeTable
        WHERE p1 = #{1} AND p2= #{2}
</select>

This way all information regarding the query is kept in one simple
place, the mapper file, and no annotation would be required.

------------

@Param usage may also be simplified overriding it with a no-argument version:

public interface SomeMapper {
    public SomeClass getWith2Params(@Param int p1, @Param String p2);
}

And using parameter position in the query:

<select id="getWith2Params" resultType="SomeClass">
        SELECT *
        FROM SomeTable
        WHERE p1 = #{1} AND p2= #{2}
</select>

Or even (if possible) extracting the name of the parameters from @Param annotation:

<select id="getWith2Params" resultType="SomeClass">
        SELECT *
        FROM SomeTable
        WHERE p1 = #{p1} AND p2= #{p2}
</select>

instead of repeat declaring the name of the parameter.




2012/02/23 17:23 2012/02/23 17:23