自学内容网 自学内容网

动态SQL

前言

动态 SQL 是Mybatis的强⼤特性之⼀,能够完成不同条件下不同的 sql 拼接

接下来我们会使用的数据表如下:

对应的实体类为:UserInfo

所有的准备工作都在如下文章。

MyBatis 操作数据库入门-CSDN博客文章浏览阅读733次,点赞14次,收藏26次。什么是MyBatis?MyBatis是⼀款优秀的 持久层 框架,⽤于简化JDBC的开发Mybatis操作数据库的入门步骤:1.创建springboot⼯程2.数据库表准备、实体类3.引⼊Mybatis的相关依赖,配置Mybatis(数据库连接信息)4.编写SQL语句(注解/XML) ,进行测试了解更多MyBatis中文网1.创建springboot⼯程创建springboot⼯程,并导⼊ mybatis的起步依赖、mysql的驱动包。https://blog.csdn.net/WHabc2002/article/details/142743762


1.<if>标签和<trim>标签 

是存在⾮必填字段问题的解决方法。

 简单来说,<if>标签就是一个判断语句,test 里是要进行的判断,如果为true,则会填充标签里的内容。

<trim>标签可以修饰<if>标签填充的不合法SQL语句。
<trim>标签属性:
1.prefix:表⽰整个语句块,以prefix的值作为前缀
2.suffix:表⽰整个语句块,以suffix的值作为后缀
3.prefixOverrides:表⽰整个语句块要去除掉的前缀
4. suffixOverrides:表⽰整个语句块要去除掉的后缀 

 比如我们选择对UserInfo的多个字段采取动态⽣成的⽅式。(username,password,age,gender)

UserInfoXmlMapper
Integer insert3(UserInfo userInfo);

 UserInfoXmlMapper.xml

<insert id="insert3">
        insert into userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                username,
            </if>
            <if test="password!=null">
                password,
            </if>
            <if test="age!=null">
                age,
            </if>
            <if test="gender">
                gender
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                #{username},
            </if>
            <if test="password!=null">
                #{password},
            </if>
            <if test="age!=null">
                #{age},
            </if>
            <if test="gender">
                #{gender}
            </if>
        </trim>
    </insert>

2.<where>标签

如果我们想根据id,password来对表进行删除操作。

仅使用<if>标签和<trim>标签 

UserInfoXmlMapper.xml

<delete id="delete2">
        delete from userinfo
        <trim prefix="where" prefixOverrides="and">
            <if test="id!=null">
                id = #{id}
            </if>
            <if test="password!=null">
                and password = #{password}
            </if>
        </trim>
    </delete>

面对条件判断使用<where>标签可以化简。

使用<where>标签对上述代码化简

<delete id="delete3">
        delete from userinfo
        <where>
            <if test="id!=null">
                id = #{id}
            </if>
            <if test="password!=null">
                and password = #{password}
            </if>
        </where>
    </delete>

<where>标签的说明:

1.<where> 只会在⼦元素有内容的情况下才插⼊where⼦句

2.会⾃动去除⼦句的开头的AND或 OR


3.<set>标签

需求: 根据传⼊的⽤⼾对象属性来更新⽤⼾数据,可以使⽤标签来指定动态内容

与<where>标签的使用类似

接⼝定义: 根据传⼊的⽤⼾ id 属性,修改其他不为 null 的属性

UserInfoXmlMapper.xml

 <update id="update2">
        update userinfo
        <set>
         <if test="username!=null">
             username=#{username}
         </if>
        </set>
        where id=#{id}
    </update>

<set>标签标签的说明:

<set> :动态的在SQL语句中插⼊set关键字,并会删掉额外的逗号

4.<foreach>标签 

对集合进⾏遍历时可以使⽤该标签。标签有如下属性:
1.collection:绑定⽅法参数中的集合,如 List,Set,Map或数组对象
2.item:遍历时的每⼀个对象
3.open:语句块开头的字符串
4.close:语句块结束的字符串
5.separator:每次遍历之间间隔的字符串

需求: 根据多个id, 删除⽤户数据

UserInfoXmlMapper
Integer delete4(List<Integer> ids);

UserInfoXmlMapper.xml

    <delete id="delete4">
        delete from userinfo
        where id in
        <foreach collection="ids" open="(" close=")" separator="," item="id" >
            #{id}
        </foreach>
    </delete>


5.<include>标签

在xml映射⽂件中配置的SQL,有时可能会存在很多重复的⽚段,此时就会存在很多冗余的代码
我们可以对重复的代码⽚段进⾏抽取,将其通过 <sql> 标签封装到⼀个SQL⽚段,然后再通过
<include> 标签进⾏引⽤

1.<sql> :定义可重⽤的SQL⽚段

2.<include> :通过属性refid,指定包含的SQL⽚段 

 UserInfoXmlMapper.xml

<sql id="el">
        id, username, age, gender
    </sql>
    <select id="selectAllUser3" resultType="com.wh.myBatis.model.UserInfo">
        select
        <include refid="el"></include>
        from userinfo
    </select>

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 


原文地址:https://blog.csdn.net/WHabc2002/article/details/142785655

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!