当前位置: 首页 > news >正文

MyBatis 之二(增、删、改操作)

文章目录

  • 1. 修改操作
    • 1.1 在 mapper(interface)里面添加修改方法的声明
    • 1.2 在 XMl 中添加 <update> 标签和修改的 sql 代码
    • 1.3 在 UserMapper 中右键 Generate 点击 Test 生成 update 测试类
  • 2. 删除操作
    • 2.1 在 mapper (interface)中添加删除的代码声明
    • 2.2 在 XMl 中添加 <delete> 标签和删除的 sql 代码
    • 2.3 在 UserMapper 中右键 Generate 点击 Test 生成 delete 测试类
  • 3. 添加操作
    • 3.1 添加用户,返回受影响的行数
      • 3.1.1 在 mapper(interface)添加方法声明
      • 3.1.2 在 XML 中添加<insert>标签和添加的 sql 代码
      • 3.1.3 生成测试类
    • 3.2 添加用户,返回自增 id
      • 3.2.1 添加方法声明
      • 3.2.2 在 XML 中添加 <insert> 标签和添加的 sql 代码
      • 3.2.3 生成测试类

回顾一下,在上一篇 MyBatis 之一(概念、创建项目、操作模式、交互流程)中,学习了 MyBatis 是一款优秀的持久层框架,学习 MyBatis 可以更方便快速的操作数据库,也学习了如何搭建 MyBatis 的开发环境,与使用 MyBatis 模式和语法操作数据库,并且也简单的了解了 MyBatis 框架的交互流程

本篇将学习如何用 MyBatis 进行数据库的增、删、改操作

这三个操作对应使用 MyBatis 的标签为

  • insert 标签:插入语句
  • update 标签:修改语句
  • delete 标签:删除语句

1. 修改操作

还是使用上一篇中创建的数据库,再给 userinfo 表中添加一条数据

INSERT INTO `mycnblog`.`userinfo` (`id`, `username`, `password`, `photo`,
`createtime`, `updatetime`, `state`) VALUES
(2, 'zhangsan', 'zhangsan', '', '2021-5-21 17:10:48', '2022-5-21 17:10:48', 1);

在这里插入图片描述

1.1 在 mapper(interface)里面添加修改方法的声明

@Mapper
public interface UserMapper {// 修改方法【根据 id 修改名称】public int update(@Param("id") Integer id,@Param("name") String username);
}

1.2 在 XMl 中添加 标签和修改的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 根据用户 id 修改用户名称 --><update id="update">update userinfo set username=#{name}  where id=#{id}</update>
</mapper>

1.3 在 UserMapper 中右键 Generate 点击 Test 生成 update 测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid update() {int result = userMapper.update(2,"老六");Assertions.assertEquals(1,result);}
}

通过断言 asserEquals 判断如果 sql 受影响行数为1,就运行正确,下面运行程序
在这里插入图片描述

然后在 mySQL中 查询,可以看到这样的测试默认情况下是污染数据库的
在这里插入图片描述

在不污染数据的前提下,执行单元测试,要添加注解 @Transactional

在这里插入图片描述

运行程序,可以看到程序虽然执行成功了,但查询数据后,没有被污染

在这里插入图片描述

2. 删除操作

2.1 在 mapper (interface)中添加删除的代码声明

@Mapper
public interface UserMapper {// 删除方法public int delete(@Param("id") Integer id);
}

2.2 在 XMl 中添加 标签和删除的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 根据用户 id 删除用户 --><delete id="delete">delete from userinfo where id=#{id}</delete>
</mapper>

2.3 在 UserMapper 中右键 Generate 点击 Test 生成 delete 测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid delete() {int result = userMapper.delete(2);System.out.println("受影响的行数:" + result);Assertions.assertEquals(1, result);}
}

我这里没添加@Transactiona,所以就直接把这条数据删除了
在这里插入图片描述

3. 添加操作

3.1 添加用户,返回受影响的行数

3.1.1 在 mapper(interface)添加方法声明

@Mapper
public interface UserMapper {// 添加用户,返回受影响的行数public int add(Userinfo userinfo);
}

3.1.2 在 XML 中添加标签和添加的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 添加用户,返回受影响的行数 --><insert id="add">insert into userinfo(username,password,photo) values(#{username},#{password},#{photo})</insert>
</mapper>

3.1.3 生成测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid add() {Userinfo userinfo = new Userinfo();userinfo.setUsername("王五");userinfo.setPassword("123");userinfo.setPhoto("default.png");int result = userMapper.add(userinfo);System.out.println("添加的结果:" + result);Assertions.assertEquals(1,result);}
}

在这里插入图片描述

3.2 添加用户,返回自增 id

3.2.1 添加方法声明

@Mapper
public interface UserMapper {// 添加用户,返回受影响的行数和自增的 idpublic int addGetId(Userinfo userinfo);
}

3.2.2 在 XML 中添加 标签和添加的 sql 代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 是设置要实现的接口的具体包名 + 类名-->
<mapper namespace="com.example.mybatisdome1.mapper.UserMapper"><!-- 添加用户,返回受影响的行数和自增 id --><insert id="addGetId" useGeneratedKeys="true" keyProperty="id" keyColumn="id">insert into userinfo(username,password,photo) values(#{username},#{password},#{photo})</insert>
</mapper>

在这里插入图片描述

3.2.3 生成测试类

@SpringBootTest
class UserMapperTest {@Autowiredprivate UserMapper userMapper;@Testvoid addGetId() {Userinfo userinfo = new Userinfo();userinfo.setUsername("张三");userinfo.setPassword("123");userinfo.setPhoto("default.png");System.out.println("添加之前 user id:" + userinfo.getId());int result = userMapper.addGetId(userinfo);System.out.println("受影响的行数:" + result);System.out.println("添加之后 uer id:" + userinfo.getId());Assertions.assertEquals(1,result);}
}

在这里插入图片描述

http://www.lryc.cn/news/9134.html

相关文章:

  • 28k入职腾讯测试岗那天,我哭了,这5个月付出的一切总算没有白费~
  • 【surfaceflinger源码分析】surfaceflinger进程的消息驱动模型
  • 「架构师」001计算机组成与体系结构
  • 既然有HTTP协议,为什么还要有RPC
  • 【新2023】华为OD机试 - 选座位(Python)
  • 数据分析与SAS学习笔记4
  • Xepor:一款针对逆向工程和安全分析的Web路由框架
  • Hadoop核心组成和生态系统简介
  • Flutter-Charts_painter大数据量绘制性能优化-数据收敛
  • 使用 GeForce Experience 更新 NVIDIA GPU 显卡驱动
  • Java泛型的<? super T>,<? extend T>的区别
  • 如何做出好看的Excel可视化图表?
  • 智能吸吹一体式方案设计特点
  • CSDN 编辑器 Marddown 语法备忘
  • 回归预测 | MATLAB实现NGO-BiLSTM北方苍鹰算法优化双向长短期记忆网络多输入单输出回归预测
  • Linux——操作系统安装
  • AFLNET lightftp项目报错解决方法
  • av 146 003
  • 干了1年“点点点”,自己辞职了,下一步是继续干测试还是转开发?
  • 国产技术迎来突破,14nm芯片横空出世,低代码也有好消息
  • 使用clickhouse-backup工具备份clickhouse数据库
  • python cartopy绘制扇形区域图/cartopy绘制北极部分区域
  • 如何设置股票接口版交易软件的指标涨跌家数?
  • C++之lambda函数(匿名函数)
  • WGCNA | 值得你深入学习的生信分析方法!~(网状分析-第四步-模块的功能注释)
  • 如何看待年轻人躺平式生活观?
  • JS 设计模式 - 怎么让你的代码提示一个档次
  • 遮挡贴图(Occlusion Map)和微表面贴图(Microsurface Map)
  • 【Vue】基本交互指令
  • MySQL 中的 distinct 和 group by 哪个效率更高?