动态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方法
在这里插入图片描述
若有收获,就点个赞吧

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

动态sql语句基本语法 1 :普通SQL语句可以用Exec执行 例: Select * from tableName Exec('select * from tableName') Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL 错误: declare @fname varchar(20) set @fname = 'FiledName' Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 正确: Exec('select ' + @fname + ' from tableName') -- 请注意加号前后的单引号的边上加空格 当然将字符串改成变量的形式也可 declare @fname varchar(20) set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) set @s = 'select ' + @fname + ' from tableName' Exec(@s) -- 成功 exec sp_executesql @s -- 此句会报错 --注:@s参数必须为ntext或nchar或nvarchar类型,必须将declare @s varchar(1000) 改为declare @s Nvarchar(1000) 如下: declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000) set @fname = 'FiledName' --设置字段名 set @s = 'select ' + @fname + ' from tableName' Exec(@s) -- 成功 exec sp_executesql @s -- 此句正确 3. 输入或输出参数 (1)输入参数: declare @QueryString nvarchar(1000) --动态查询语句变量(注:必须为ntext或nchar哐nvarchar类型,不能是varchar类型) declare @paramstring nvarchar(200) --设置动态语句中的参数的字符串(注:必须为ntext或nchar哐nvarchar类型,不能是varchar类型) declare @input_id int--定义需传入动态语句的参数的值 set @QueryString='select * from tablename where id=@id' --id为字段名,@id为要传入的参数 set @paramstring='@id int' --设置动态语句中参数的定义的字符串 set @input_id =1 --设置需传入动态语句的参数的值为1 exec sp_executesql @querystring,@paramstring,@id=@input_id   若有多个参数: declare @QueryString nvarchar(1000) --动态查询语句变量(注:必须为ntext或nchar哐nvarchar类型,不能是varchar类型) declare @paramstring nvarchar(200) --设置动态语句中的参数的字符串(注:必须为ntext或nchar哐nvarchar类型,不能是varchar类型) declare @input_id int--定义需传入动态语句的参数的值,参数1 declare @input_name varchar(20)--定义需传入动态语句的参数的值,参数2 set @QueryString='select * from tablename where id=@id and name=@name' --id与name为字段名,@id与@name为要传入的参数 set @paramstring='@id int,@name varchar(20)' --设置动态语句中参数的定义的字符串,多个参数用","隔开 set @input_id =1 --设置需传入动态语句的参数的值为1 set @input_name='张三' --设置需传入动态语句的参数的值为"张三" exec sp_executesql @querystring,@paramstring,@id=@input_id,@name=@input_name --请注意参数的顺序 (2)输出参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值