mybatis的动态SQL

在没有使用动态SQL的时候,所有的sql语句都是程序员提前写好的,我们可以通过传递不同的参数来获取不同的查询结果。

例子:有一个功能要根据班级和人名来进行查询,如何客户端只传来班级信息就根据班级查询,如果只传来人名就根据人名来查询,如果班级和人名都有就根据两个信息查询。当然这个功能不使用动态SQL也能实现,编写三条SQL语句,根据客户端传来的不同参数调用不同的SQL语句查询,但这样如果遇到更复杂的功能就需要编写大量的查询语句,不利于开发效率。

这时候通过动态SQL就能很好的解决这个问题.动态SQL就是能在mapper xml中添加逻辑判断,动态生成sql语句;

动态SQL介绍:(动态SQL都很好理解,有编程基础的基本一看就懂了)

1,<if>标签:如果test属性为真,就将标签之间的内容添加到sql语句中

<select id="selByAccinAccout" resultType="log">

<!--添加1=1是防止后面没有内容,sql会不规范-->
 select * from log where 1=1 
<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任何特字符号 -->
  <!--accin为传递进来的参数,可以直接通过名字引用--> <if test="accin!=null and accin!=''"> and accin=#{accin} </if> <if test="accout!=null and accout!=''"> and accout=#{accout} </if> </select>

2, <where>标签:1,当编写 where 标签时,如果内容中第一个是 and 去掉第一个and  2,如果<where>中有内容会生成 where 关键字,如果没有内容不生成 where 关键字

<select id="selByAccinAccout" resultType="log">
    select * from log
    <where>
        <if test="accin!=null and accin!=''">
            and accin=#{accin}
        </if>
        <if test="accout!=null and accout!=''">
            and accout=#{accout}
        </if>
    </where>
</select>

3,<choose> <when> <otherwise>标签:只有有一个成立,其他都不执行.

<select id="selByAccinAccout" resultType="log">
    select * from log
    <where>
        <choose>
            <when test="accin!=null and accin!=''">
                and accin=#{accin}
            </when>
            <when test="accout!=null and accout!=''">
                and accout=#{accout}
            </when>
        </choose>
    </where>
</select>

4, <set>标签:

1.去掉最后一个逗号

2.如果<set>里面有内容生成 set 关键字,没有就不生成

<update id="upd" parameterType="log" >
    update log
    <set>
        <!--防止set中没有内容报错-->
        id=#{id},
        <if test="accIn!=null and accIn!=''">
            accin=#{accIn},
        </if>
        <if test="accOut!=null and accOut!=''">
            accout=#{accOut},
        </if>
    </set>
    where id=#{id}
</update>

5. Trim标签:

Trim属性功能:

1,prefix 在前面添加内容

2,prefixOverrides 去掉前面内容

3,suffix 在后面添加内容

4,suffixOverrieds 去掉后面内容

trim功能执行顺序去掉内容后添加内容

<update id="upd" parameterType="log">
    update log
    <trim prefix="set" suffixOverrides=",">
        a=a,
    </trim>
    where id=100
</update>

6,<bind>标签:给参数重新赋值

作用场景:模糊查询,格式修改(例如加百分号)

<select id="selByLog" parameterType="log"
    resultType="log">
    <bind name="accin" value="'%'+accin+'%'"/>
    select * from where accin=#{accin}
</select>

7, <foreach>标签

标签属性作用:

1,collectino=”” 要遍历的集合

2, item 迭代变量, #{迭代变量名}获取内容

3,open 循环后左侧添加的内容

4, close 循环后右侧添加的内容

5, separator 每次循环时,元素之间的分隔符

<select id="selIn" parameterType="list"
    resultType="log">
    select * from log where id in
    <foreach collection="list" item="abc" open="("close=")" separator=",">
        #{abc}
    </foreach>
</select>

8,<sql> 和<include>:某些 SQL 片段如果希望复用,可以使用<sql>定义这个片段

<sql id="mysql">
    id,accin,accout,money
</sql>

引用sql

<select id="">
    select <include refid="mysql"></include>from log
</select>

 

posted on 2019-08-11 16:43  ggsdduzdl  阅读(119)  评论(0编辑  收藏  举报

导航