MyBatis/iBatis - reusable sql fragments in a separate SQL Map file?

丶灬走出姿态 提交于 2019-11-29 20:29:29

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!

Alexander Davliatov

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!