锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. Mybatis-动态SQL

Mybatis-动态SQL

0
  • 软件开发
  • 发布于 2024-09-19
  • 0 次阅读
黄健
黄健

1.什么是动态SQL?

Mabits是一个Java持久化框架,它提供了动态SQL的功能。动态SQL是一种根据不同条件动态生成SQL语句的技术。在Mabits中,动态SQL通常是通过使用一组特殊的标签和代码块来实现的,这些标签和代码块可以根据条件包含或排除不同的部分,从而生成不同的SQL语句。动态SQL可以让开发者构建更灵活、高效的数据库操作语句,因为他们可以根据实际需要创建出更加精确和优化的SQL语句。Mabits的动态SQL还允许开发者使用参数化查询,可以防止SQL注入攻击,并增强程序的安全性

接下来学习经常使用的几个标签

2.标签

标签是Mabits动态SQL中最常用的一个标签之一,它的作用是根据给定条件包含或排除不同的部分,以生成不同的SQL语句。在XML文件中,标签通常被嵌套在其他标签内,如标签内使用标签来动态生成SQL语句:

常见场景:非必传参数时使用

int addUser2(UserEntity user);

@Test
    void addUser2() {
        String username = "zhaoliu";
        String password = "123456";
        UserEntity user = new UserEntity();
        user.setUsername(username);
        user.setPwd(password);
        int result = userMapper.addUser2(user);
        System.out.println("修改行数: "+result);
    }
<insert id="addUser2">
        insert into userinfo(username,password
            <if test="photo != null">
                ,photo
            </if>
        ) values(#{username},#{pwd}
            <if test="photo != null">
                ,#{photo}
            </if>
        )
    </insert>

当不传入photo时,执行结果

JDBC Connection [HikariProxyConnection@579590740 wrapping com.mysql.cj.jdbc.ConnectionImpl@75b6dd5b] will not be managed by Spring
==>  Preparing: insert into userinfo(username,password ) values(?,? )
==> Parameters: zhaoliu(String), 123456(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@380e1909]
修改行数: 1

传入photo时,执行结果

JDBC Connection [HikariProxyConnection@645717550 wrapping com.mysql.cj.jdbc.ConnectionImpl@609e57da] will not be managed by Spring
==>  Preparing: insert into userinfo(username,password ,photo ) values(?,? ,? )
==> Parameters: zhaoliu2(String), 123456(String), dog.png(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@24d61e4]
修改行数: 1


//数据库
mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo   | createtime          | updatetime          | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
|  1 | lisi     | 123456   |         | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 |     1 |
|  3 | zhangsan | 123456   |         | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 |     1 |
|  4 | wangwu   | 123456   |         | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 |     1 |
|  5 | zhaoliu  | 123456   |         | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 |     1 |
|  6 | zhaoliu2 | 123456   | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 |     1 |
+----+----------+----------+---------+---------------------+---------------------+-------+
5 rows in set (0.00 sec)

注意理解:

3.标签

学习标签之前先看一个问题

<insert id="addUser3">
        insert into userinfo(
        <if test="username != null">
            username,
        </if>
        <if test="pwd != null">
            password,
        </if>
        <if test="photo != null">
            photo
        </if>
        ) values(
        <if test="username != null">
            #{username},
        </if>
        <if test="pwd != null">
            #{pwd},
        </if>
        <if test="photo != null">
            #{photo}
        </if>
        )
    </insert>

当遇到这种所有参数都为非必填参数时,如果第一个参数传了值,后续都没传,就会出现(username,)这个问题,此时很明显SQL 语句是错误的,肯定不能成功执行或者其它类似这种问题

标签就能解决此类问题

标签也是Mabits动态SQL中常用的一个标签,主要用于快速生成包含WHERE、SET等关键字的SQL语句,同时还能够自动处理SQL语句中的逗号(,)和AND/OR等连接符。

标签通常包含以下属性:

  1. prefixOverrides:表示在生成SQL语句前需要忽略的字符前缀。
  2. suffixOverrides:表示在生成SQL语句后需要忽略的字符后缀。
  3. prefix:表示在生成SQL语句前需要添加的字符串。
  4. suffix:表示在生成SQL语句后需要添加的字符串。
  5. suffixOverrides:表示在生成SQL语句中需要忽略掉的字符串。

用法一:删除前缀后缀

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

    </insert>

单元测试

@Transactional
    @Test
    void addUser3() {
        String username = "zhaoliu3";
        String password = "123456";
        UserEntity user = new UserEntity();
        user.setUsername(username);
        user.setPwd(password);
        int result = userMapper.addUser2(user);
        System.out.println("修改行数: "+result);
    }

当不传photo时,上一个pwd后的,被去除了

JDBC Connection [HikariProxyConnection@1228603887 wrapping com.mysql.cj.jdbc.ConnectionImpl@36c2d629] will be managed by Spring
==>  Preparing: insert into userinfo(username,password ) values(?,? )
==> Parameters: zhaoliu3(String), 123456(String)
<==    Updates: 1

用法二,解决多个非必传参数问题

    List<ArticleInfoVO> getListByIdOrTitle(@Param("id")Integer id ,@Param("title")String title);


<select id="getListByIdOrTitle" resultType="com.example.demo.entity.vo.ArticleInfoVO">
        select*from articleinfo
        <trim prefix="where" suffixOverrides="and">
            <if test="id!=null and id >0">
                id = #{id} and
            </if>
            <if test="title!=null and title!=''">
                title like concat('%',#{title},'%')
            </if>
        </trim>
    </select>


@Test
    void getListByIdOrTitle() {
        List<ArticleInfoVO> list = articleMapper.getListByIdOrTitle(null,null);
        System.out.println(list.size());
    }

执行结果:

传参都为空时,也就是trim中没有内容,那么prefix中的where也不会生产。就不用再使用

where 1=1 and …的方式解决传参为空的问题了

传一个参数,前缀where就会执行

4.标签

能更好的解决上述多个非必传参数问题

过使用标签和标签,可以根据用户传入的参数动态生成SQL语句的WHERE子句。如果参数不为空,则会包含相应的查询条件;否则,该查询条件会被忽略。

需要注意的是,由于标签会自动处理WHERE子句中的AND和OR关键字,因此在使用时应该合理安排条件语句的位置,避免出现意外结果。

执行

非常方便

不传id 传title

将and前缀清除了

注意:只能去除sql语句前缀的关键字,不能去除后缀关键字

标签也可以用替换

5.标签

通过使用标签和标签,可以根据用户传入的参数动态生成SQL语句的SET子句。如果参数不为空,则会包含相应的更新内容;否则,该更新内容会被忽略

需要注意的是,由于标签会自动处理SET子句中的逗号(,),因此在使用时应该合理安排更新内容的位置,避免出现意外结果

标签通过将SET子句中的所有条件用逗号(,)连接起来,可以根据不同的更新情况动态生成满足需求的SQL语句

标签也可以用替换

示例:修改lisi->lis2,password->456789

mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo   | createtime          | updatetime          | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
|  1 | lisi     | 123456   |         | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 |     1 |
|  3 | zhangsan | 123456   |         | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 |     1 |
|  4 | wangwu   | 123456   |         | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 |     1 |
|  5 | zhaoliu  | 123456   |         | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 |     1 |
|  6 | zhaoliu2 | 123456   | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 |     1 |
+----+----------+----------+---------+---------------------+---------------------+-------+

代码

//修改
    int updateUser(UserEntity user);

<update id="updateUser">
        update userinfo
        <set>
            <if test="username!=null and username!=''">
                username = #{username},
            </if>
            <if test="pwd!=null and pwd!=''">
                password = #{pwd},
            </if>
        </set>
        where id = #{id}
    </update>

    @Test
    void updateUser() {
        UserEntity user = new UserEntity();
        user.setUsername("lisi2");
        user.setPwd("456789");
        user.setId(1);
        int result = userMapper.updateUser(user);
        System.out.println("修改后: "+result);
    }

执行结果

JDBC Connection [HikariProxyConnection@236002428 wrapping com.mysql.cj.jdbc.ConnectionImpl@4d0e54e0] will not be managed by Spring
==>  Preparing: update userinfo SET username = ?, password = ? where id = ?
==> Parameters: lisi2(String), 456789(String), 1(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@95eb320]
修改后: 1

mysql> select*from userinfo;
+----+----------+----------+---------+---------------------+---------------------+-------+
| id | username | password | photo   | createtime          | updatetime          | state |
+----+----------+----------+---------+---------------------+---------------------+-------+
|  1 | lisi2    | 456789   |         | 2022-12-06 17:10:48 | 2022-12-06 18:10:48 |     1 |
|  3 | zhangsan | 123456   |         | 2023-05-18 17:21:49 | 2023-05-18 17:21:49 |     1 |
|  4 | wangwu   | 123456   |         | 2023-05-18 17:36:28 | 2023-05-18 17:36:28 |     1 |
|  5 | zhaoliu  | 123456   |         | 2023-05-20 09:22:08 | 2023-05-20 09:22:08 |     1 |
|  6 | zhaoliu2 | 123456   | dog.png | 2023-05-20 09:35:06 | 2023-05-20 09:35:06 |     1 |
+----+----------+----------+---------+---------------------+---------------------+-------+

不传参数pwd时

6.标签

<foreach> 标签是 MyBatis 中的一个迭代标签,可以对集合对象进行遍历,并生成多条 SQL 语句(如 INSERT、UPDATE、DELETE 等)。

使用 <foreach> 标签,我们可以将一个集合对象的元素依次取出,作为 SQL 语句中的参数进行插入、更新或删除操作。常用的语法如下:

<foreach collection="collection" item="item" separator="separator" open="open" close="close"> 
    <!-- SQL 语句 --> 
</foreach>

其中,各个属性和元素的含义如下:

  • collection:指定要遍历的集合对象的属性名。
  • item:指定在遍历过程中每个元素所对应的变量名。
  • separator:指定在生成多条 SQL 语句时,不同语句之间的分隔符,默认为英文逗号。
  • open:指定生成的 SQL 语句的头部。
  • close:指定生成的 SQL 语句的尾部。

示例:删除文章表的数据

mysql> select*from articleinfo;
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
| id | title | content   | createtime          | updatetime          | uid | rcount | state |
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
|  1 | Java  | Java正文  | 2023-05-15 09:12:59 | 2023-05-15 09:12:59 |   1 |      1 |     1 |
|  2 | mysql | mysql正文 | 2023-05-19 11:14:49 | 2023-05-19 11:14:49 |   1 |      1 |     1 |
+----+-------+-----------+---------------------+---------------------+-----+--------+-------+
2 rows in set (0.00 sec)

此处进行回滚,不真的删除

代码:

//根据id批量删除文章
    int delByIdList(List<Integer> idList);

<delete id="delByIdList">
        <!--  delete from articleinfo where id in(1,2,3....)      -->
        delete from articleinfo
        where id in(
            <foreach collection="idList" item="id" separator=",">
                #{id}
            </foreach>
        )
    </delete>


@Transactional
    @Test
    void delByIdList() {
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        idList.add(3);
//只有两条
        int result = articleMapper.delByIdList(idList);
        System.out.println("删除: "+result);
    }

执行单元测试

JDBC Connection [HikariProxyConnection@918738473 wrapping com.mysql.cj.jdbc.ConnectionImpl@55fee662] will be managed by Spring
==>  Preparing: delete from articleinfo where id in( ? , ? , ? )
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<==    Updates: 2
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b960b5b]
删除: 2

上述代码没有使用open,close属性.将()写在了foreach标签外,这里加上

和刚才的写在标签外的方式作用是相同的


原文链接: https://blog.csdn.net/chenchenchencl/article/details/130777671

标签: #Mybatis 37 #软件开发 1171
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.