动态sql

作者: Stringer | 来源:发表于2016-11-25 08:50 被阅读10次

1.where、if

<select id="findUserList" parameterType="cn.ztc.mybatis.po.UserQueryVo" resultType="cn.ztc.mybatis.po.UserCustom">
        select * from user 
        <!-- where可以自动的去掉条件中的第一个and -->
        <where>
            <if test="userCustom.sex!=null">
                <if test="userCustom.sex!=null and userCustom.sex!=''">
                    and user.sex = #{userCustom.sex}
                </if>
                <if test="userCustom.username!=null and userCustom.username!=''">
                    and user.username like '%${userCustom.username}%'
                </if>
            </if>
        </where>
    </select>```

2.sql片段
<!-- 定义sql片段 
    一般是基于单表来定义sql片段,这样的sql片段可重用性高
    在sqk片段中不要包含where
-->
<sql id="query_user_where">
    <if test="userCustom.sex!=null!=null">
        <if test="userCustom.sex!=null and userCustom.sex!=''">
            and user.sex = #{userCustom.sex}
        </if>
        <if test="userCustom.username!=null and userCustom.username!=''">
            and user.username like '%${userCustom.username}%'
        </if>
    </if>
</sql>

<select id="findUserList" parameterType="cn.ztc.mybatis.po.UserQueryVo" resultType="cn.ztc.mybatis.po.UserCustom">
    select * from user 
    <!-- 可以自动的去掉条件中的第一个and -->
    <where>
    <!-- 引用sql片段 -->
        <include refid="query_user_where"></include>
    </where>
</select>```

3.foreach

<sql id="query_user_where">
        <if test="userCustom.sex!=null!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex = #{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username like '%${userCustom.username}%'
            </if>
            <if test="ids!=null">
                <!--    使用foreach遍历ids
                    collection:指定输入对象中的集合属性名
                    item:遍历生成的对象名
                    open:开始遍历时的拼接串
                    close:结束遍历时的拼接串
                    separator:遍历的两个对象中间的拼接串
                 -->
                <foreach collection="ids" item="user_id" open="and(" close=")" separator="or">
                    <!-- 每次遍历需要拼接的串 
                         and( id=? or id=? or id=? )
                    -->
                    id=#{user_id}
                </foreach>
            </if>
        </if>
    </sql>```

相关文章

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第十三章 使用动态SQL(一)

    第十三章 使用动态SQL(一) 动态SQL简介 动态SQL是指在运行时准备并执行的SQL语句。在动态SQL中,准备...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • 关于Mybatis的一些问题讨论

    Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理 动态sql的用途 Mybat...

  • MyBatis5-动态 SQL

    动态 SQL 什么是动态 SQL 就是动态的对 SQL 进行组装 拼接. : 可以自动去...

  • geoserver动态颜色参数样式、动态sql配置实现

    geoserver动态颜色参数样式、动态sql配置实现 动态颜色参数样式、动态sql 访问方式: http://l...

  • 强大的动态SQL

    1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...

  • MyBatis:动态 SQL

    1. 动态 SQL 简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素...

  • 无标题文章

    ### 一、简答题 #### 1、Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行...

  • 五,MyBatis动态SQL

    一,含义:动态SQL是指根据参数数据动态组织SQL的技术 二,动态SQL的应用场景:比如淘宝搜索的时候,可以动态的...

网友评论

      本文标题:动态sql

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