美文网首页
Mapper.XML文件相互引用

Mapper.XML文件相互引用

作者: theBookofChange | 来源:发表于2020-09-19 17:32 被阅读0次

    最近发现在mapper文件sql条件判断哪里出现拉大量的重复判断,所以思考拉一下能否像java一样把重复的判断拉出来复用

    针对<sql>标签从别的文件中引用过来

    • 如下有一个条件要被很多个mapper文件中的SQL引用
    <mapper namespace="cn.stylefeng.guns.sys.modular.system.mapper.BaseAreaMapper">
        <sql id="baseAreaCommonAsU">
            <if test="areaQuery.cityCode != null  and  areaQuery.cityCode !='' ">
                AND u.city_code=#{areaQuery.cityCode}
            </if>
        </sql>
    </mapper>
    
    • 引用公共的<sql>标签
        <select id="xxxxxx"
                resultMap="xxx">
            SELECT 字段A,字段B FROM 表 AS u
            <where> 
            <include refid="cn.stylefeng.guns.sys.modular.system.mapper.BaseAreaMapper.baseAreaCommon"/>
            </where> 
        </select>
    
    就这样我们就可以把公共的标签提取出来被各种mapper使用拉,舒舒服服,哈哈!
    因上家公司一直用mongo,所以后续mybatis优化标签的方法在做补充,也欢迎大家多多指导

    相关文章

      网友评论

          本文标题:Mapper.XML文件相互引用

          本文链接:https://www.haomeiwen.com/subject/wbseyktx.html