动态SQL的使用

目录

动态SQL

理解

执行原理

常用标签

if标签

where标签

set标签

trim标签

foreach标签


动态SQL

理解

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

执行原理

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

常用标签

f标签,where、trim、foreach等标签。

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" resultMap="studentMap">
        select * from student
        where 1=1
        <if test="SID!=null and SID != 0">
            and SID = #{SID}
        </if>
        <if test="Sname != null">
            and Sname = #{Sname}
        </if>
    </select>

if标签通常是根据条件做判断,一般作为where子句的一部分。if表达式判断是否传递参数在其后面有一个test属性,该属性必填,为true或false。为true时,会拼接该if中SQL片段,为false,则不拼接SQL子句,该test的判断是通过OGNL表达式判断。

传递name和id属性:

    @Test
    public void selectStudent(){
        //获取会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setSID(1);
        student.setSname("钱一");
        studentMapper.selectStudent(student);
    }

执行结果:

只传递id:

    @Test
    public void selectStudent(){
        //获取会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setSID(1);
        studentMapper.selectStudent(student);
    }

执行结果:

不传递参数:

    @Test
    public void selectStudent(){
        //获取会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过接口获取对象实例
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        studentMapper.selectStudent(student);
    }

执行结果:

where标签

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

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

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

不传参数:

只传递一个参数:

传递两个参数:

set标签

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

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

trim标签

去掉SQL中多余的and关键字、逗号,或者给SQL拼接上where或者set关键字。

img

    <select id="selectStudent" parameterType="student" resultMap="studentMap">
        select * from student
        <trim prefix="where" prefixOverrides="and">
            <if test="SID != null and SID != 0">
                and SID = #{SID}
            </if>
            <if test="Sname != null">
                and Sname = #{Sname}
            </if>
        </trim>
    </select>

执行结果:

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

执行结果:

foreach标签

批量操作标签

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

    <!--
    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" resultMap="studentMap">
        select * from student where SID in
        <foreach collection="list" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>
执行结果:
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值