• Mybatis实战练习六【批量删除&Mybatis参数传递】


    目录

    批量删除

    编写接口方法

    编写SQL语句

    编写测试方法

    Mybatis参数传递

    多个参数

    单个参数


    批量删除

    如上图所示,用户可以选择多条数据,然后点击上面的 删除 按钮,就会删除数据库中对应的多行数据。

     

    编写接口方法

    BrandMapper 接口中定义删除多行数据的方法。

    1. /**
    2. * 批量删除
    3. */
    4. void deleteByIds(int[] ids);

    参数是一个数组,数组中存储的是多条数据的id  

    编写SQL语句

    BrandMapper.xml 映射配置文件中编写删除多条数据的 statement

    编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了 foreach 标签供我们使用

    foreach 标签

    用来迭代任何可迭代的对象(如数组,集合)。

    • collection 属性:

      • mybatis会将数组参数,封装为一个Map集合。

        • 默认:array = 数组

        • 使用@Param注解改变map集合的默认key的名称

    • item 属性:本次迭代获取到的元素。

    • separator 属性:集合项迭代之间的分隔符。foreach 标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。

    • open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次

    • close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次

    1. <delete id="deleteByIds">
    2. delete from tb_brand where id
    3. in
    4. <foreach collection="array" item="id" separator="," open="(" close=")">
    5. #{id}
    6. foreach>
    7. ;
    8. delete>

    假如数组中的id数据是{1,2,3},那么拼接后的sql语句就是:  

    delete from tb_brand where id in (1,2,3);

    编写测试方法

    test/java 下的 com.itheima.mapper 包下的 MybatisTest类中 定义测试方法

    1. @Test
    2. public void testDeleteByIds() throws IOException {
    3. //接收参数
    4. int[] ids = {5,7,8};
    5. //1. 获取SqlSessionFactory
    6. String resource = "mybatis-config.xml";
    7. InputStream inputStream = Resources.getResourceAsStream(resource);
    8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    9. //2. 获取SqlSession对象
    10. SqlSession sqlSession = sqlSessionFactory.openSession();
    11. //SqlSession sqlSession = sqlSessionFactory.openSession(true);
    12. //3. 获取Mapper接口的代理对象
    13. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    14. //4. 执行方法
    15. brandMapper.deleteByIds(ids);
    16. //提交事务
    17. sqlSession.commit();
    18. //5. 释放资源
    19. sqlSession.close();
    20. }

    Mybatis参数传递

    Mybatis 接口方法中可以接收各种各样的参数,如下:

    • 多个参数

    • 单个参数:单个参数又可以是如下类型

      • POJO 类型

      • Map 集合类型

      • Collection 集合类型

      • List 集合类型

      • Array 类型

      • 其他类型

    多个参数

    如下面的代码,就是接收两个参数,而接收多个参数需要使用 @Param 注解,那么为什么要加该注解呢?这个问题要弄明白就必须来研究Mybatis 底层对于这些参数是如何处理的。

    User select(@Param("username") String username,@Param("password") String password);
    1. <select id="select" resultType="user">
    2. select *
    3. from tb_user
    4. where
    5. username=#{username}
    6. and password=#{password}
    7. select>

     

    我们在接口方法中定义多个参数,Mybatis 会将这些参数封装成 Map 集合对象,值就是参数值,而键在没有使用 @Param 注解时有以下命名规则:

    • 以 arg 开头 :第一个参数就叫 arg0,第二个参数就叫 arg1,以此类推。如:

    map.put("arg0",参数值1);

    map.put("arg1",参数值2);

     

    以 param 开头 : 第一个参数就叫 param1,第二个参数就叫 param2,依次类推。如:  

    map.put("param1",参数值1);

    map.put("param2",参数值2);

     

    代码验证:

    • UserMapper 接口中定义如下方法

    User select(String username,String password);

     在 UserMapper.xml 映射配置文件中定义SQL

    1. <select id="select" resultType="user">
    2. select *
    3. from tb_user
    4. where
    5. username=#{arg0}
    6. and password=#{arg1}
    7. select>

     或者

    1. <select id="select" resultType="user">
    2. select *
    3. from tb_user
    4. where
    5. username=#{param1}
    6. and password=#{param2}
    7. select>

    运行代码结果如下  

    • 在映射配合文件的SQL语句中使用用 arg 开头的和 param 书写,代码的可读性会变的特别差,此时可以使用 @Param 注解。

    在接口方法参数上使用 @Param 注解,Mybatis 会将 arg 开头的键名替换为对应注解的属性值。

    代码验证:

    • UserMapper 接口中定义如下方法,在 username 参数前加上 @Param 注解

     

    User select(@Param("username") String username, String password);

    Mybatis 在封装 Map 集合时,键名就会变成如下:

    map.put("username",参数值1);

    map.put("arg1",参数值2);

    map.put("param1",参数值1);

    map.put("param2",参数值2);

     在 UserMapper.xml 映射配置文件中定义SQL

    1. <select id="select" resultType="user">
    2. select *
    3. from tb_user
    4. where
    5. username=#{username}
    6. and password=#{param2}
    7. select>

    运行程序结果没有报错。而如果将 #{} 中的 username 还是写成 arg0  

    1. <select id="select" resultType="user">
    2. select *
    3. from tb_user
    4. where
    5. username=#{arg0}
    6. and password=#{param2}
    7. select>

    运行程序则可以看到错误  

    ==结论:以后接口参数是多个时,在每个参数上都使用 @Param 注解。这样代码的可读性更高。==  

    单个参数

    • POJO 类型

      直接使用。要求 属性名参数占位符名称 一致

    • Map 集合类型

      直接使用。要求 map集合的键名参数占位符名称 一致

    • Collection 集合类型

      Mybatis 会将集合封装到 map 集合中,如下:

      map.put("arg0",collection集合);

      map.put("collection",collection集合;

      ==可以使用 @Param 注解替换map集合中默认的 arg 键名。==

    • List 集合类型

      Mybatis 会将集合封装到 map 集合中,如下:

      map.put("arg0",list集合);

      map.put("collection",list集合);

      map.put("list",list集合);

      ==可以使用 @Param 注解替换map集合中默认的 arg 键名。==

    • Array 类型

      Mybatis 会将集合封装到 map 集合中,如下:

      map.put("arg0",数组);

      map.put("array",数组);

      ==可以使用 @Param 注解替换map集合中默认的 arg 键名。==

    • 其他类型

      比如int类型,参数占位符名称 叫什么都可以。尽量做到见名知意

  • 相关阅读:
    【历史上的今天】7 月 30 日:现代电视先驱诞生;以太坊启动;80 年代最畅销的电脑品牌
    QT+OSG/osgEarth编译之二十五:GeoTIFF+Qt编译(一套代码、一套框架,跨平台编译,版本:GeoTIFF-2.12.1)
    Python 全栈系列191 基于Redis队列的处理服务
    LeetCode 每日一题 2023/9/4-2023/9/10
    成都瀚网科技有限公司抖音带货的正规
    第五章:抽象类
    一个依赖搞定-Spring-Boot-反爬虫,防止接口盗刷!,不愧是阿里P8
    远程访问数据库,快解析助力疫情防控远程办公
    NewStarCTF 2023 week5--web
    quinn源码解析:QUIC数据包是如何发送的
  • 原文地址:https://blog.csdn.net/m0_64550837/article/details/126604756