Search code examples
mybatisspring-mybatis

Create a mybatis dao template to execute dynamically any query


I have severals queries stored in database, and my goal is to create a mybatis dao that execute a selected query from my list.

Does anyone have idea on how to do this ? (Just one dao able to execute a passed query as parameter)


Solution

  • This can be done by injecting variable verbatim using string substitution:

    Mapper interface

    List<Map<String, Object>> get(@Param("query") String query);
    

    And xml mapping file:

    <select id="get" resultType="hashmap" statementType="STATEMENT">
       ${query}
    </select>