首页 > 数据库技术 > 详细

Mybatis动态SQL

时间:2019-10-22 15:58:01      阅读:74      评论:0      收藏:0      [点我收藏+]

SQL片段、<if>元素

SQL片段一般基于单表建立,这样SQL片段可重用性才高。SQL片段一般不要包含where条件。
引用其他Mapper的SQL片段时,需要在refid属性中加上其他Mapper的namespace属性!

<!--定义-->
<sql id="query_user_where">
    <if test="id!=null and id!=''">
        and User.id=#{id} 
    </if>
    <if test="name!=null and name!=''"> 
        and User.name=#{name};
    </if>
    <if test="age!=null and age!=''"> 
        and User.age=#{age};
    </if>
</sql>
<!--引用-->
<include refid="query_user_where"/>

<where>元素

where元素会自动去除以and或or开头的SQL。

<!--定义-->
<where>
    <if test="id!=null and id!=''">
        and User.id=#{id} 
    </if>
    <if test="name!=null and name!=''"> 
        and User.name=#{name};
    </if>
<where>

Mybatis动态SQL

原文:https://www.cnblogs.com/feiqiangsheng/p/11719916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!