动态SQL

1.什么是动态sql

  • 动态SQL就是根据不同的条件产生的不同SQL语句

2.标签

2.1 if标签

dao层

public User findConditon(@Param("name")String name, @Param("age")String age);

mapper层

 <!--如果姓名不为空则安姓名查找 如果姓名为空则按年龄查找 否则查询全部-->
    <select id="findConditon" resultType="User">
        select * from tb_user
        <where>
            <if test="name!=null and name!=''">
                and name = #{name}
            </if>
            <if test="age!=null and age!=''">
                and age = #{age}
            </if>
        </where>
    </select>

2.2 choose标签 类似switch

public User findByCondition(@Param("name")String name, @Param("age")String age,
                                @Param("pwd")String pwd);
<select id="findByCondition" resultType="User">
        select * from tb_user
        <where>
            <choose>
                <when test="name!=null and name!=''">
                    and name = #{name}
                </when>
                <when test="email!=null and email!=''">
                    and email = #{email}
                </when>
                <otherwise>
                    and age = #{age}
                </otherwise>
            </choose>
        </where>
    </select>

2.3where标签 拼接where关键字 ,会删除第一个 条件拼接关键字(not and or)

如果不使用where标签 就要在where其他判断语句前加入1=1 如 select * from tb_user where 1=1加其他的if判断语句,使用where 语句 ,会删除第一个 条件拼接关键字(not and or)

2.4set标签

这个标签配合if标签一起用 用于修改语句 拼接set关键字,并且将set name=zs,age=10, 后面的最后一个','去掉 如果传递的参数为null 那么就不会修改该列的值

public int updateUser(User user);
// test="参数" 这里的参数 是前端传过来的值 前端页面上是什么 这里就要写什么 
//而下面的name=#{name} 第一个name是你数据库中的字段 #{name}中是你的前端传过来的值
<update id="updateUser" parameterType="User">
        update tb_user
        <set>
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age},
            </if>
        </set>
        where id = #{id}
    </update>

2.5foreach标签 循环标签 适用于批量添加、删除 和查询记录

查询id为1 3 5 的用户信息
正常sql语句为 select * from tb_user where id in(1,3,5);
下面的为使用foreach遍历 循环查询
解释:
<foreach collection="集合类型" open="开始的字符" close="结束的字符"
item="集合中的成员" separator="集合成员之间的分割符">
#{item的值}
</foreach>
标签属性:
collection:表示循环的对象是数组还是list集合。如果dao方法的形参是数组,collection="array";
如果dao方法形参是list,collection="list";
open:循环开始的字符。sql.append("(");
close:循环结束的字符。sql.append(")");
item:集合成员,自定义的变量。Integer item = idList.get(i);
separator:集合成员之间的分隔符。sql.append(",");
#{item的值}:获取集合成员的值;

  • 具体代码实现
  • dao层
//传递的参数为id数组所以mapper层的collection="list"
public List<User> findByIds(@Param("ids")Integer[] ids);
    <select id="findByIds" resultType="User">
        select * from tb_user where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

2.6trim标签 动态灵活的拼接sql 可以替换前缀和后缀,效果和where/set 的相同

<!--

    update tb_user set username = ?, address = ? where id = ? and username = ? and address = ? 
-->
    <update id="updateTrim">
        update tb_user
        <trim prefix="set" suffixOverrides=",">
            <if test="username != null">
                username = #{username},
            </if>
            <if test="address != null">
                address = #{address},
            </if>
        </trim>
        <trim prefix="where" prefixOverrides="and" >
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="address != null">
                and address = #{address}
            </if>
        </trim>
    </update>

2.7sql片段

把大量重复的代码整理起来
一般用于查询语句的时候 select * … 这种不推荐 所以用sql片段可以很好的解决这个问题

<sql></sql> 
<include></include>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容