Mybatis的常用的9种动态sql标签

简介: Mybatis的常用的9种动态sql标签

1:动态标签

MyBatis9种动态标签

MyBatis提供了9种动态SQL标签:if ,where,trim,choose, when ,otherwise, foreach,set,bind

其执行原理为,使用OGNL从SQL参数对象中计算表达式的值,根据表达式的值动态拼接SQL,以此来完成动态SQL的功能。

2: if

当参数满足条件才会执行某个条件

 <if test="id!=null and id!='' ">
     id=#{id}
 </if>
  <if test="id!=null and id!='' ">
     from A表
 </if>

3:where

解决 where and 形式的sql生成, <where>一般会加载动态条件中配合使用, 在有条件的情况下它会自动在所有条件的前面加上WHERE关键字, 还会去掉所有条件前面的AND/OR

<where>
    <if test="id!=null and id!='' ">
        and  id=#{id}
    </if>
    <if test="username!=null and username!='' ">
        and user_name=#{username}
    </if>
    <if test="beginDate!=null and beginDate!='' ">
        and create_date >=#{beginDate}
    </if>
    <if test="endDate!=null and endDate!='' ">
        and create_date <![CDATA[<=]]> #{endDate}
    </if>
    <if test="deptId!=null and deptId!='' ">
        and dept_id=#{deptId}
    </if>
</where>

4:trim

<trim> 它的功能比较灵活、广泛。 它可以用来实现<where>节点的功能

prefix在所有包含的SQL前面加上指定的字符串

prefixOverrides 在所有包含的SQL前面加上去除指定的字符串

suffix 在所有包含的SQL后面加上指定的字符串

prefixOverrides 在所有包含的SQL后面加上去除指定的字符串

<trim prefix="WHERE" prefixOverrides="and|or"  >
    <if test="id!=null and id>0 ">
        and  id=#{id}
    </if>
    <!--OGNL调用对象的方法-->
    <if test="username!=null and username.indexOf('徐')>-1 ">
        and user_name=#{username}
    </if>
    <if test="beginDate!=null">
        and create_date >=#{beginDate}
    </if>
    <if test="endDate!=null">
        and create_date <![CDATA[<=]]> #{endDate}
    </if>
    <!--OGNL: gt 是大于  调用静态方法 -->
    <if test="deptId!=null and deptId gt @cn.tulingxueyuan.pojo.Emp@getNum() ">
        and dept_id=#{deptId}
    </if>
</trim>

5:choose when otherwise

<choose when otherwise>

多条件取其中一个

    <select id="QueryEmp2" resultType="Emp">
        <include refid="SelectEmp">
            <property name="columns" value="id,dept_id"/>
        </include>
        <where>
            <choose>
                <when test="deptName=='经理'">
                    dept_id=1
                </when>
                <when test="deptName=='普通员工'">
                    dept_id=2
                </when>
                <otherwise>
                    dept_id=#{id}
                </otherwise>
            </choose>
        </where>
    </select>

6:foreach

<foreach> 循环

实现in 范围查询 使用$可以实现但是有sql注入风险

collection 需要循环的集合的参数名字

item 每次循环使用的接收变量

separator 分割符设置(每次循环在结尾添加什么分隔符,会自动去除最后一个结尾的分隔符)

open 循环开始添加的字符串

close 循环结束添加的字符串

index 循环的下标的变量

<!--
-->
select * from emp
<where>
    <foreach collection="usernames" item="username" separator="," open=" username in (" close=")" index="i" >
        #{username}
    </foreach>
</where>

7:set

<update id="update">
        update emp
        <!--<trim prefix="set" suffixOverrides=",">
            <if test="username!=null and username!='' ">
            user_name=#{username},
            </if>
            <if test="createDate!=null and createDate!='' ">
            create_date=#{createDate},
            </if>
            <if test="deptId!=null and deptId!='' ">
            dept_id=#{deptId}
            </if>
        </trim>-->
        <set>
            <if test="username!=null and username!='' ">
                user_name=#{username},
            </if>
            <if test="createDate!=null">
                create_date=#{createDate},
            </if>
            <if test="deptId!=null and deptId!='' ">
                dept_id=#{deptId},
            </if>
        </set>
        where  id=#{id}
    </update>

8:bind

-- 空格拼接
select * from emp where user_name like '%' #{username} '%'  
-- concat拼接
select * from emp where user_name like concat('%',#{username},'%')                                                                       
<!--
 <bind> 在Mapper映射文件上下文声明一个变量
    name 变量名称
    value 值(支持OGNL表达式)
-->
<select id="QueryEmp4" resultType="Emp">
    <bind name="_username" value="'%'+username+'%' "/>
    select * from emp where user_name like  #{_username}
</select>  

9:sql

<!--
sql片段  解决SQL中重复的代码冗余,可以提取出来放在sql片段中
    1.  <sql 定义sql片段
            id 唯一标识
    2.   <include 在SQL中引用SQL片段片段
            refid 需要引用的SQL片段的id
            <property 声明变量, 就可以在SQL片段中动态调用,让不同的SQL调用同一个SQL片段达到不同的功能
                name 变量名
                value 变量值
                一般情况使用${}在sql片段中引用.一单引用了,一定保证每个include都声明了该变量
-->
<sql id="selectEmp">
    select ${columns} from emp
</sql>
<select id="QueryEmp4" resultType="Emp">
    <bind name="_username" value="'%'+username+'%' "/>
    <include refid="selectEmp">
        <property name="columns" value="*"></property>
    </include>
    where user_name like  #{_username}
</select> 

10:OGNL表达式

e1 or e2
e1 and e2
e1 == e2,e1 eq e2
e1 != e2,e1 neq e2
e1 lt e2:小于
e1 lte e2:小于等于,其他gt(大于),gte(大于等于) 7 e1 in e2
e1 in e2
e1 not in e2
e1 + e2,e1 * e2,e1/e2,e1 - e2,e1%e2
!e,not e:非,求反
e.method(args)调用对象方法
e.property对象属性值
e1[ e2 ]按索引取值,List,数组和Map
@class@method(args)调用类的静态方法
@class@field调用类的静态字段值
目录
打赏
0
0
0
0
8
分享
相关文章
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
297 0
|
2月前
|
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
131 5
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
5月前
|
九、MyBatis动态SQL
九、MyBatis动态SQL
77 2
|
4月前
|
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
92 0
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
592 6
|
7月前
|
mybatis实现动态sql
MyBatis的动态SQL功能为开发人员提供了强大的工具来应对复杂的查询需求。通过使用 `<if>`、`<choose>`、`<foreach>`等标签,可以根据不同的条件动态生成SQL语句,从而提高代码的灵活性和可维护性。本文详细介绍了动态SQL的基本用法和实际应用示例,希望对您在实际项目中使用MyBatis有所帮助。
277 11
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
canal-starter 监听解析 storeValue 不一样,同样的sql 一个在mybatis执行 一个在数据库操作,导致解析不出正确对象
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等