mybatis提取<where><if>共用代码

  mybatis项目dao层中很多sql语句都会拥有某些相同的查询条件,以<where><if test=""></if></where>的形式拼接在sql语句后,一个两个的sql语句感觉不到什么,但是如果查询语句特别多,但是查询的条件总是类似的,那就可以考虑把<where><if>这部分代码抽取出来,封装一下,然后需要条件搜索的sql语句直接引用就可以了。

  先来看下没有抽取代码之前的条件sql语句

复制代码
第一条
<select id = "getUserEmailByProvinceAndOrderType" resultType="String">
        select DISTINCT(wo_responsibility) from t_view_workorder
        <where>
            <if test="province != '全国' and province != null">
                wo_province = #{province}
            </if>
            <if test="orderType != '全部' and orderType != null">
                and wo_type = #{orderType}
            </if>
            <if test="email != ''">
                and wo_responsibility = #{email}
            </if>
        </where>
</select>
第二条
<select id = "getUndoneDelayOrderByProvinceAndOrderTypeAndUserEmail" resultType="com.chinamobile.sias.workorder.po.Workorder">
        select * from t_view_workorder
        <where>
        <if test="province != '全国' and province != null">
                wo_province = #{province}
            </if>
            <if test="orderType != '全部' and orderType != null">
                and wo_type = #{orderType}
            </if>
            <if test="email != ''">
                and wo_responsibility = #{email}
            </if>
        <if test="true"> and (wo_complete_time is null or wo_complete_time='') and (select curdate()) >= wo_regulations_time </if>

    </where>

</select>
复制代码

以上是两条sql语句,可以看出,两个sql语句中有某些查询条件是相同的

复制代码
        <if test="province != '全国' and province != null">
            wo_province = #{province}
        </if>
        <if test="orderType != '全部' and orderType != null">
            and wo_type = #{orderType}
        </if>
        <if test="email != ''">
            and wo_responsibility = #{email}
        </if>    
复制代码

此时我们就可以对此段判断条件进行提取。如下:

复制代码
<sql id="common_where_if">
        <if test="province != '全国' and province != null">
            wo_province = #{province}
        </if>
        <if test="orderType != '全部' and orderType != null">
            and wo_type = #{orderType}
        </if>
        <if test="email != ''">
            and wo_responsibility = #{email}
        </if>
</sql>
复制代码

此时把<where>标签下相同的判断条件提去了出来,id自己取,这里定为 common_where_if.

那么如何使用这段代码呢,如下:

<include refid="common_where_if"/>

格式如下:

<select id = "getUserEmailByProvinceAndOrderType" resultType="String">
        select DISTINCT(wo_responsibility) from t_view_workorder
        <where>
            <include refid="common_where_if"/>
        </where>

</select>

此时就在<where>标签中引用了共用的判断条件,再多的sql语句,再多的查询条件,只需要一个<include>就能解决重复的代码。

 

posted on   嘣嘣嚓  阅读(23902)  评论(2编辑  收藏  举报

编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示