MyBatis / iBatis - 在单独的SQL Map文件中可重用的sql片段? [英] MyBatis/iBatis - reusable sql fragments in a separate SQL Map file?

查看:526
本文介绍了MyBatis / iBatis - 在单独的SQL Map文件中可重用的sql片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把几个SQL Map XML文件使用的sql片段放在一个单独的文件中。目前,包含这些片段的< sql> 元素与其他元素一起位于其中一个元素中,例如< select> ,这使得它们很难找到。

我可以使用一个只定义几个< sql> 元素的映射器而不是用于生成接口的实现?这个映射器的正确名称空间是什么?

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?

这是带有框架的SQL Map文件:

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
    </select>

        FROM some_table

        <include refid="whereDate"/>

        <include refid="otherSqlFragment"/>

    </select>

</mapper>

我想分开这样的元素:

First Sql Map file:

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>

第二个Sql Map文件:

Second Sql Map file:

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

    <select id="getSomeData"
            resultType="SomeClass"
            parameterType="DateParam" >
        SELECT some_column, another_column
    </select>

        FROM some_table

        <include refid="whereDate"/>

        <include refid="otherSqlFragment"/>

    </select>

</mapper>


推荐答案

这正是我以前工作的项目没有。常见的片段在一个单独的文件中定义,该文件包含在主iBATIS配置文件中。

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.

我们在根目录下有一个名为 Core的SQL映射文件.ism.xml 看起来像这样:

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>

    ....

然后在我们的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!

这篇关于MyBatis / iBatis - 在单独的SQL Map文件中可重用的sql片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆