动态SQL

一、动态SQL

动态SQL根据条件动态的对SQL进行拼接组装

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

标签:如;if标签,where、trim、foreach等标签

1、if标签:

通过student中的条件查询对象或对象结果

通过id查询:select * from student where SID= ?

通过name查询:select * from student where Name= ?

通过id和name查询:select * from student where id=? and name =?

全表查询:select * from student

if标签使用示例:

	<select id="selectStudent" parameterType="student" resultType="student">
        select * from student
        where  1=1
        <if test="id != null and id != 0" >
            and  SID = #{id}
        </if>
        <if test="name != null" >
           and Sname = #{name}
        </if>
    </select>

if标签通常是根据条件做判断,一般作为where子句的一部分

if表达式判断是否传递参数在其后面有一个test属性,该属性必填,为true或false,

为true时,会拼接该if中SQL片段,为false,则不拼接SQL子句

该test的判断是通过OGNL表达式判断

传递name和id属性:

		Student213 student213 = new Student213();
        student213.setId(1);
        student213.setName("tulun");
        studentMapper213.selectStudent(student213);

执行结果:
在这里插入图片描述只传递id:

		Student213 student213 = new Student213();
        student213.setId(1);
        studentMapper213.selectStudent(student213);

执行结果:
在这里插入图片描述
不传递参数:

		Student213 student213 = new Student213();
        studentMapper213.selectStudent(student213);

执行结果:
在这里插入图片描述

2、where标签

where标签作用:如果该标签包含的元素有结果返回,就插入where,如果where后面的字符串包含and 或者or开头的,将其剔除。

	<select id="selectStudent" parameterType="student" resultType="student">
        select * from student
        <where>
            <if test="id != null and id != 0">
                and SID = #{id}
            </if>
            <if test="name != null">
                and Sname = #{name}
            </if>
        </where>
    </select>

where表达式一般和if表达式一块使用,如果条件一个都不满足,则不拼接where条件,如果有一个或者是多个条件成立,where会自动拼接在SQL中,并且紧随where之后的and和or去掉

不传递参数:
在这里插入图片描述
只传递一个参数:
在这里插入图片描述
传递两个参数:
在这里插入图片描述

3、set标签

set标签作用:

如果标签包含的元素有返回值,就插入一个set,如果set后面的SQL中以逗号结尾的,将最后的逗号去掉

    <update id="updateNameById">
        update  student
        <set>
            <if test="name != null" >
                Sname = #{name},
            </if>
        </set>
        where SID = #{id}
    </update>

Update table_name set id=XXX , name=XX,sex=XXX

where id =XX;

4、trim标签

去掉SQL中多余的and关键字、逗号,或者给SQL拼接上where或者set关键字
在这里插入图片描述

	<select id="selectStudent" parameterType="student" resultType="student">
        select * from student
        <trim prefix="where" prefixOverrides="and">
            <if test="id != null and id != 0">
                and SID = #{id}
            </if>
            <if test="name != null">
                and Sname = #{name}
            </if>
        </trim>
    </select>
    
   <update id="updateNameById">
        update  student
        <trim prefix="set" suffixOverrides=",">
            <if test="name != null" >
                Sname = #{name},
            </if>
        </trim>
        where SID = #{id}
    </update>         

5、foreach标签

批量操作标签

select * from student where id in(1,2,3,4,5);

insert into student (id,name) values(id1,name1),(id2,name2),(id3,name3);

<!--
    foreach表达式
    collection属性:必填
     当参数为数组时,collection属性是array
     当参数是list时,collection属性是list
     当参数是map时,collection属性属性是map
    item属性:变量名,值是从集合中迭代遍历的每一个值
    open属性:循环开始的字符串
    close属性:循环结束的字符串
    separator属性:每次循环的分隔符
    index属性:索引的属性名,当集合是数组时可以通过index来获取数据,当迭代是map时,index表示的是key

select * from  student  where id in(1,2,3,4,5);
    -->

    <select id="selectStudentsByIds" resultType="student">
        select * from student where SID in
        <foreach collection="list" open="(" close=")" separator="," item="id">
         #{id}
        </foreach>
    </select>

在这里插入图片描述

6、模糊匹配

字符串模糊匹配:使用 like 关键字

%:表示一个或者是多个字符

_:表示一个字符

需求:查询student表中用户名含L的所有用户

select * from student where Sname like %L%;

写法1:直接将通配符拼接在参数上

selectStudentByName(“%L%”
在这里插入图片描述
写法2:使用MySQL提供的concat函数

MySQL还提供字符串拼接的方法:concat(x1,x2) -》x1x2

	<select id="selectStudentByName" parameterType="string" resultType="student">
        select * from student where Sname like concat('%',concat(#{name},'%'))
    </select>

在这里插入图片描述
写法3:使用bind标签处理

    <select id="selectStudentByName"  resultType="student">
       <bind name="namesql" value="'%'+name+'%'"/>
        select * from student where Sname like #{namesql}
    </select>

注意:bind表达式中value属性处理中借助OGNL表达式,对应的参数需要具有getter方法
在这里插入图片描述
若有收获,就点个赞吧

————————————————
版权声明:本文为CSDN博主「平平无奇小菜鸟。」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_54356563/article/details/120940876

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值