Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法

2022/7/25 2:23:02

本文主要是介绍Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mybatis系列--14-动态sql之if,choose(when,otherwise),set语法

概述

本篇接上篇博客, 继续学习动态sql中的if,choose,set等元素

if

if 条件判断大家都很熟悉,当某个条件成立,则执行什么动作,
下面的示例中给出当title给出时,where语句中拼接title相关条件,当author给出时,where语句中拼接author相关条件的示例,其余文件参考上篇博客
BlogMapper.java

package com.kuang.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.kuang.pojo.Blog;

/**
 * 功能描述
 *
 * @since 2022-07-22
 */
public interface BlogMapper {
    List<Blog> getBlogs(@Param("title") String title, @Param("author") String author);
}

if的详细使用方法见BlogMapper.xml文件
test后面添加条件语句,与if结尾符中间添加执行语句,此处注意,为了保证两条if语句的执行正确,我们在sql中添加了一个where 1=1的语句,这个语句不够优雅,在项目中也不应该存在,应该使用更优雅的解决方案where标签
BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BlogMapper">
    <select id="getBlogs" resultType="blog">
        select * from blog where 1=1
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </select>
</mapper>

where 标签

where标签即上面所说的where 1=1的优雅的解决方案
添加改标签后,在执行的sql语句中会自动添加where 且会判断是否是第一个where条件,如果是,则将条件前面的and或者or给去掉
改造上面的例子

BlogMapper.java

package com.kuang.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.kuang.pojo.Blog;

/**
 * 功能描述
 *
 * @since 2022-07-22
 */
public interface BlogMapper {
    List<Blog> getBlogsWhere(@Param("title") String title, @Param("author") String author);
}

BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BlogMapper">
    <select id="getBlogsWhere" resultType="blog">
        select * from blog
        <where>
            <if test="title != null">
                and title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>
</mapper>

测试的时候你会发现,不同的输入会形成不同的执行sql,会添加where并会自动去除条件中多余的and

set标签

set标签顾名思义,是用来代替update语句中的set语句的,在set语句中我们知道,有的时候需要更新的参数不确定,存在时我们才更新,且更薪语句后面的,不确定是否需要添加,这些问题set标签都帮我们搞定了
看个例子
BlogMapper.java

package com.kuang.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.kuang.pojo.Blog;

/**
 * 功能描述
 *
 * @since 2022-07-22
 */
public interface BlogMapper {
    void updateBlog(@Param("title") String title, @Param("author") String author, @Param("id") String id);
}

BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BlogMapper">
    <update id="updateBlog">
        update blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>

trim标签

讲完whereset标签,我们发现这两个标签有共同点,在sql中会添加whereset,并自动的将语句中and,优化,构成一个正确的sql用于执行,其实这两个标签都是由trim标签实现的,实现方法也很简单,我们一起来揭开他的神秘面纱
trim标签可以设置 前缀(prefix),前缀覆盖(prefixOverrides),后缀(suffix) 前缀覆盖(suffixOverrides)

where标签的实现

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

set标签的实现

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

choose(when,otherwise)标签

choose标签与java中的switch语句类似,仅匹配条件中的一个, when 相当于case, otherwise相当于default,不同之处在于,choose标签不需要添加break类似的语句,不会继续执行,仅匹配一个条件

BlogMapper.java

package com.kuang.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.kuang.pojo.Blog;

/**
 * 功能描述
 *
 * @since 2022-07-22
 */
public interface BlogMapper {
  List<Blog> getBlogsChoose(@Param("title") String title, @Param("author") String author, @Param("views") int views);
}

BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.BlogMapper">
    <select id="getBlogsChoose" resultType="blog">
        select * from blog
        <where>
            <choose>
                <when test="title != null">
                    and title = #{title}
                </when>
                <when test="author != null">
                    and author = #{author}
                </when>
                <otherwise>
                    and views >= #{views}
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>


这篇关于Mybatis系列--14-动态sql之if,choose(when,otherwise),set,where,trim语法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程