zoukankan      html  css  js  c++  java
  • 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>就能解决重复的代码。

  • 相关阅读:
    鼠标滑动带动画下拉展开的滑动门代码
    很靓很大气的简约红色CSS菜单代码
    用Cookie来保存菜单当前位置代码
    单击单选按钮切换对应的菜单代码
    仿微软中国的滑动门导航菜单代码
    C#创建SQLServer的存储过程
    通过应用程序域AppDomain加载和卸载程序集(转载)
    多线程学习笔记一(转载)
    C#实现Treeview节点"正在载入..."效果
    JavaScript 学习笔记之函数理解二
  • 原文地址:https://www.cnblogs.com/EnzoDin/p/6445281.html
Copyright © 2011-2022 走看看