22

I would like to put sql fragments used by several of my SQL Map XML files in a separate file. At the moment, the <sql> elements with these fragments are in one of the mappers together with other elements like <select>, which makes them hard to find.

Can I have a mapper that defines just a few <sql> elements and is not used to generate an implementation to an interface? What would be the correct namespace of this mapper?

This is the SQL Map file with the framents:

<mapper namespace="com.company.project.dao.someDao">

    <sql id="whereDate">
        WHERE date(`time`) BETWEEN #{startDate} AND #{endDate}
    </sql>  

    <sql id="someOtherSqlFragment">
        ...
    </sql>

    <select id="getSomeData" resultType="SomeClass" parameterType="DateParam" >
        SELECT some_column, another_column          
        FROM some_table
        <include refid="whereDate"/>
        <include refid="otherSqlFragment"/>
    </select>

</mapper>

I'd like to separate the elements like this:
First Sql Map file:

<mapper namespace="com.company.project.dao.???">

    <sql id="whereDate">
        WHERE date(`time`) BETWEEN #{startDate} AND #{endDate}
    </sql>  

    <sql id="someOtherSqlFragment">
        ...
    </sql>

</mapper>

Second Sql Map file:

<mapper namespace="com.company.project.dao.someDao">

    <select id="getSomeData" resultType="SomeClass" parameterType="DateParam" >
        SELECT some_column, another_column          
        FROM some_table     
        <include refid="whereDate"/>
        <include refid="otherSqlFragment"/>
    </select>

</mapper>

3 Answers 3

35

This is exactly what a project I used to work on did. Common fragments were defined in a separate file which was included in the main iBATIS config file.

We had a SQL map file at the root named Core.ism.xml which looked like this:

<sqlMap namespace="Core" >

    <sql id="fragmentBasicAuditFieldNames">
        CreateDate, CreateUser, 
        UpdateDate, UpdateUser, UpdateCode 
    </sql>

    ....

And then in our SQL map files we could reference it like this:

<include refid="Core.fragmentBasicAuditFieldNames" />

I hope I've understood what you were asking correctly!

3
  • Thanks! I also noticed that I'm using MapperFactoryBean class from mybatis-spring to "load" the other SQL Map files. The file with common fragments wasn't "loaded" anywhere, so I had to add it manually to the mybatis config file.
    – brabec
    Commented May 20, 2011 at 8:26
  • 1
    If the Core mapper file is the only thing in your mybatis config file, it may make more sense to use the mapperLocations property in SqlSessionFactoryBean to load the common mapper file.
    – AngerClown
    Commented May 20, 2011 at 12:02
  • 4
    If you are using MyBatis, replace "sqlMap" with "mapper". see code.google.com/p/mybatis/wiki/DocUpgrade3. I bluntly followed the above solution and ran into other issues. Then I realized I'm using MyBatis and have to use mapper instead of sqlMap.
    – Srikanth
    Commented Jan 9, 2012 at 20:33
6

Say, you have some

<mapper namespace="Common">
   <sql id="idsIn">
        ${column} IN
        <foreach item="id" collection="ids" separator="," open="(" close=")">
            #{id}
        </foreach>
    </sql>
</mapper>

Than in another mapper you can use it like:

<mapper namespace="OtherMapper">
    <sql id="someSql">
        ...
        <include refid="Common.idsIn">
            <property name="column" value="${column}"/>
            <!-- OR hardcode: <property name="column" value="id"/> -->
            <property name="filterPksTable" value="${filterPksTable}"/>
        </include>
        ...
    </sql>
</mapper>

Also, you can have a look here

0

You can access to ResultMap tag via its repository classname, like

... resultMap="com.example.repository.UserRepository.UserMapResult" ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.