• Mybatis实战练习五【修改&删除一行数据】


    目录

    修改

    编写接口方法

    编写SQL语句

    编写测试方法

    删除一行数据

    编写接口方法

    编写SQL语句

    编写测试方法


    修改

    如图所示是修改页面,用户在该页面书写需要修改的数据,点击 提交 按钮,就会将数据库中对应的数据进行修改。注意一点,如果哪儿个输入框没有输入内容,我们是将表中数据对应字段值替换为空白还是保留字段之前的值?答案肯定是保留之前的数据。

    接下来我们就具体来实现

     

    编写接口方法

    BrandMapper 接口中定义修改方法。

    1. /**
    2. * 修改
    3. */
    4. void update(Brand brand);

    上述方法参数 Brand 就是封装了需要修改的数据,而id肯定是有数据的,这也是和添加方法的区别。  

    编写SQL语句

    BrandMapper.xml 映射配置文件中编写修改数据的 statement

    1. <update id="update">
    2. update tb_brand
    3. <set>
    4. <if test="brandName != null and brandName != ''">
    5. brand_name = #{brandName},
    6. if>
    7. <if test="companyName != null and companyName != ''">
    8. company_name = #{companyName},
    9. if>
    10. <if test="ordered != null">
    11. ordered = #{ordered},
    12. if>
    13. <if test="description != null and description != ''">
    14. description = #{description},
    15. if>
    16. <if test="status != null">
    17. status = #{status}
    18. if>
    19. set>
    20. where id = #{id};
    21. update>

    set 标签可以用于动态包含需要更新的列,忽略其它不更新的列。  

    编写测试方法

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

    执行测试方法结果如下:

    从结果中SQL语句可以看出,只修改了 status 字段值,因为我们给的数据中只给Brand实体对象的 status 属性设置值了。这就是 set 标签的作用。

    删除一行数据

    如上图所示,每行数据后面都有一个 删除 按钮,当用户点击了该按钮,就会将改行数据删除掉。那我们就需要思考,这种删除是根据什么进行删除呢?是通过主键id删除,因为id是表中数据的唯一标识。

    接下来就来实现该功能。

    编写接口方法

     在 BrandMapper 接口中定义根据id删除方法。

    1. <delete id="deleteById">
    2. delete from tb_brand where id = #{id};
    3. delete>

    编写SQL语句

     在 BrandMapper.xml 映射配置文件中编写删除一行数据的 statement

    1. <delete id="deleteById">
    2. delete from tb_brand where id = #{id};
    3. delete>

    编写测试方法

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

    1. @Test
    2. public void testDeleteById() throws IOException {
    3. //接收参数
    4. int id = 6;
    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.deleteById(id);
    16. //提交事务
    17. sqlSession.commit();
    18. //5. 释放资源
    19. sqlSession.close();
    20. }

     运行过程只要没报错,直接到数据库查询数据是否还存在。

  • 相关阅读:
    【Flink 实战系列】Flink on yarn 为什么 Allocated CPU VCores 显示不正确?
    武汉工程大学24计算机考研数据,有学硕招收调剂,而专硕不招收调剂!
    设计模式之:享元模式FlyweightPattern的实现
    QT入门10个小demo——MP4视频播放器
    基于JAVA信用卡逾期数据处理分析系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    Linux——进程间通信大汇总!(概述、管道通信、消息队列、共享内存、信号、信号量)
    Spring 事务失效的八种场景
    前后端 websocket 通信
    OpenMMlab导出swin-transformer模型并用onnxruntime和tensorrt推理
    安卓日志输出-logger
  • 原文地址:https://blog.csdn.net/m0_64550837/article/details/126604358