深入解析Mybatis-Plus框架:简化Java持久层开发(八)

🍀 前言

博客地址:

👋 简介

本章节介绍如何通过Mybatis-Plus更新数据库中的数据。
本章节不需要前置准备,继续使用之前的测试类,数据库表进行操作。

📖 正文

1 Mapper接口

# 根据主键 ID 来更新
int updateById(T entity);
# entity 用于设置更新的数据,wrapper 用于组装更新条件
int update(T entity, Wrapper<T> updateWrapper);
1.1 updateById

根据主键id更新数据
需求:修改id为2的角色的名称为游客1号,角色编码为tourist02

@Test
public void updateByMapper() {
    Role role = Role.builder()
            .id(2L)
            .roleName("游客1号")
            .roleCode("tourist01")
            .build();

    int i = roleMapper.updateById(role);
    System.out.println("更新:" + i);
}

// 更新:1

实际执行的SQL

UPDATE tb_role SET role_name='游客1号', role_code='tourist01' WHERE id=2
1.2 update

同过其他条件来更新数据
需求:更新角色编码为TEST02的角色数据,修改角色名称为游客2号,角色编码为tourist02

@Test
public void updateByMapper() {
    // 构造数据
    Role role = Role.builder()
    .roleName("游客2号")
    .roleCode("tourist02")
    .build();
    // 条件构造器
    LambdaUpdateWrapper<Role> wrapper = Wrappers.
    <Role>lambdaUpdate().
    eq(Role::getRoleCode, "TEST02");

    int i = roleMapper.update(role, wrapper);
    System.out.println("更新:" + i);
}

// 更新:1

实际执行的SQL

UPDATE tb_role SET role_name='游客2号', role_code='tourist02' WHERE (role_code = 'TEST02')

2 Service接口

// 根据 ID 来更新,entity 用于设置 ID 以及其他更新条件
boolean updateById(T entity);
// wrapper 用于设置更新数据以及条件
boolean update(Wrapper<T> updateWrapper);
// entity 用于设置更新的数据,wrapper 用于组装更新条件
boolean update(T entity, Wrapper<T> updateWrapper);
// 批量更新
boolean updateBatchById(Collection<T> entityList);
// 批量更新,可手动设置批量提交阀值
boolean updateBatchById(Collection<T> entityList, int batchSize);
// 保存或者更新
boolean saveOrUpdate(T entity);
2.1 update

boolean update(T entity, Wrapper<T> updateWrapper)该方式更新和Mapper接口的方式一样,区别只是返回值类型不一样,可以参考上一章删除的操作。
这里使用下boolean update(Wrapper<T> updateWrapper)来进行更新
需求:将角色编码为TEST03的角色修改为,角色名称为游客3号,角色编码为tourist03

@Test
public void updateByService() {
    // 条件构造器
    LambdaUpdateWrapper<Role> wrapper = Wrappers.
            <Role>lambdaUpdate()
            .set(Role::getRoleName, "游客3号")
            .set(Role::getRoleCode, "tourist03")
            .eq(Role::getRoleCode, "TEST03");
    boolean b = roleService.update(wrapper);
    System.out.println("更新:" + b);
}

// 更新:true

实际执行的SQL

UPDATE tb_role SET role_name='游客3号',role_code='tourist03' WHERE (role_code = 'TEST03')
2.2 updateBatchById

批量更新
需求:修改id为2,3,4的角色

@Test
public void updateByService() {
    List<Role> roles = new ArrayList<>();
    roles.add(Role.builder().id(2L).roleName("测试1号").roleCode("test01").build());
    roles.add(Role.builder().id(3L).roleName("测试2号").roleCode("test02").build());
    roles.add(Role.builder().id(4L).roleName("测试3号").roleCode("test03").build());
    boolean b = roleService.updateBatchById(roles);
    System.out.println("更新:" + b);
}

// 更新:true

实际执行的SQL

UPDATE tb_role SET role_name='测试1号', role_code='test01' WHERE id=2
UPDATE tb_role SET role_name='测试2号', role_code='test02' WHERE id=3
UPDATE tb_role SET role_name='测试3号', role_code='test03' WHERE id=4

✏ 总结

💖 欢迎关注我的公众号

在这里插入图片描述

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值