方法全解

(1) INSERT

BaseMapper 的接口提供了 insert 和 insertBatch 方法,用于新增数据;

  • insert(entity):插入实体类数据,不忽略 null 值。
  • insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
  • insert(entity, ignoreNulls):插入实体类数据。
  • insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。
  • insertSelectiveWithPk(entity):插入带有主键的实体类,忽略 null 值。
  • insertWithPk(entity, ignoreNulls):带有主键的插入,此时实体类不会经过主键生成器生成主键。
  • insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
  • insertBatch(entities, size):批量插入实体类数据,按 size 切分。
  • insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。
  • insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。
  • insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。
① insert
/**
* insert(entity):插入实体类数据,不忽略 null 值。
* insert(entity, ignoreNulls):插入实体类数据。
*/
@Test
public void testInsert() {
/**
* 默认不忽略null值
* INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?)
*/
int row = accountMapper.insert(new Account().setUserName(null).setBirthday(new Date()).setAge(25));
Assertions.assertEquals(row, 1); /**
* ignoreNulls true:忽略null , false:不忽略null
* INSERT INTO `tb_account`(`user_name`) VALUES (?)
*/
int row2 = accountMapper.insert(new Account().setUserName("ly3").setBirthday(null).setAge(null), true);
Assertions.assertEquals(row2, 1);
}
② insertSelective
/**
* insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
*/
@Test
public void testInsertSelective() {
/**
* INSERT INTO `tb_account`(`user_name`) VALUES (?)
*/
int row = accountMapper.insertSelective(new Account().setUserName("陈远航").setAge(null));
Assertions.assertEquals(row, 1);
}
③ insertWithPk
/**
* insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。
*/
@Test
public void testInsertWithPk() {
/**
* INSERT INTO `tb_account`(`id`, `user_name`, `age`, `birthday`) VALUES (?, ?, ?, ?)
*/
int row = accountMapper.insertWithPk(new Account().setUserName("廖楷瑞").setId(5L).setBirthday(null));
Assertions.assertEquals(row, 1);
}
④ insertBatch
/**
* insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
* insertBatch(entities, size):批量插入实体类数据,按 size 切分。
*/
@Test
public void testInsertBatch() {
List<Account> accounts = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
Account account = new Account().setUserName("ly" + i).setBirthday(new Date()).setAge(20 + i);
accounts.add(account);
}
/**
* 批量插入,可以指定每次插入的数据大小size
* size=2 : INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?), (?, ?, ?)
* size=3 : INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)
* ......
*/
int rows = accountMapper.insertBatch(accounts, 2);
System.out.println("rows = " + rows);
}
⑤ insertOrUpdate
/**
* insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。
* insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。
* insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。
*/
@Test
public void testInsertOrUpdate() {
Account account = new Account().setUserName("ly-update2").setId(3L).setBirthday(new Date()).setAge(21);
/**
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
*/
int row = accountMapper.insertOrUpdate(account);
Assertions.assertEquals(row, 0, 1); account = new Account().setUserName("ly-update2").setBirthday(null).setAge(21);
/**
* INSERT INTO `tb_account`(`user_name`, `age`) VALUES (?, ?)
*/
int row2 = accountMapper.insertOrUpdateSelective(account);
Assertions.assertEquals(row2, 0, 1);
}

(2) DELETE

BaseMapper 的接口提供了 deleteById、deleteBatchByIds、deleteByMap、deleteByQuery 方法,用于删除数据;

  • deleteById(id):根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。
  • deleteBatchByIds(ids):根据多个主键批量删除数据。
  • deleteBatchByIds(ids, size):根据多个主键批量删除数据。
  • deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
  • deleteByCondition(whereConditions):根据查询条件来删除数据。
  • deleteByQuery(queryWrapper):根据查询条件来删除数据。
① deleteById
/**
* deleteById(id):根据主键删除数据。
* ??? 如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。 ??? 怀疑态度
*/
@Test
public void testDeleteById() {
/**
* DELETE FROM `tb_account` WHERE `id` = ?
*/
int row = accountMapper.deleteById(9L);
Assertions.assertTrue(row > 0);
}
② deleteBatchByIds
/**
* deleteBatchByIds(ids):根据多个主键批量删除数据。
* deleteBatchByIds(ids, size):根据多个主键批量删除数据。
*/
@Test
public void testDeleteBatchByIds() {
List<Integer> ids = List.of(5, 6, 7, 8);
/**
* DELETE FROM `tb_account` WHERE `id` = ? OR `id` = ? OR `id` = ? OR `id` = ?
*/
int rows = accountMapper.deleteBatchByIds(ids);
Assertions.assertTrue(rows > 0); /**
* 分批删除
* size=2 : DELETE FROM `tb_account` WHERE `id` = ? OR `id` = ?
*/
int rows2 = accountMapper.deleteBatchByIds(ids, 2);
Assertions.assertTrue(rows2 > 0);
}
③ deleteByMap
/**
* deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
*/
@Test
public void testDeleteByMap() {
/**
* DELETE FROM `tb_account` WHERE `tb_account`.`age` = ?
*/
int rows = accountMapper.deleteByMap(Map.of("age", 25));
Assertions.assertTrue(rows > 0);
}
④ deleteByCondition
 /**
* deleteByCondition(whereConditions):根据查询条件来删除数据。
*/
@Test
public void testDeleteByCondition() {
QueryCondition condition = QueryCondition.createEmpty().and("age = 27");
/**
* DELETE FROM `tb_account` WHERE age = 27
*/
int rows = accountMapper.deleteByCondition(condition);
Assertions.assertTrue(rows > 0); /**
* DELETE FROM `tb_account` WHERE `id` > ?
*/
int rows2 = accountMapper.deleteByCondition(ACCOUNT.ID.gt(35));
Assertions.assertTrue(rows2 > 0);
}
⑤ deleteByQuery
/**
* deleteByQuery(queryWrapper):根据查询条件来删除数据。
*/
@Test
public void testDeleteByQuery() {
/**
* DELETE FROM `tb_account` WHERE `age` >= ?
*/
QueryWrapper wrapper = QueryWrapper.create().where(ACCOUNT.AGE.ge(32));
int rows = accountMapper.deleteByQuery(wrapper);
Assertions.assertTrue(rows > 0);
}

(3) UPDATE

BaseMapper 的接口提供了 update、updateByMap、updateByQuery 方法,用于更新数据;

  • update(entity):根据主键来更新数据,若实体类属性数据为 null,该属性不会新到数据库。
  • update(entity, ignoreNulls):根据主键来更新数据到数据库。
  • updateByMap(entity, whereConditions):根据 Map 构建的条件来更新数据。
  • updateByMap(entity, ignoreNulls, whereConditions):根据 Map 构建的条件来更新数据。
  • updateByCondition(entity, whereConditions):根据查询条件来更新数据。
  • updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。
  • updateByQuery(entity, queryWrapper):根据查询条件来更新数据。
  • updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。
① update
 @Test
public void testUpdate() {
Account account = new Account().setId(5L)
.setAge(39)
.setUserName("测试修改")
.setBirthday(new Date(2023, Calendar.SEPTEMBER, 6, 12, 12, 12));
/**
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
*/
int rows = accountMapper.update(account);
Assertions.assertTrue(rows > 0);
} @Test
public void testUpdateIgnoreNulls() {
Account account = new Account().setId(6L)
.setAge(null)
.setUserName(null)
.setBirthday(new Date(2023, Calendar.SEPTEMBER, 6, 12, 12, 12));
/**
* 不忽略null值
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
*/
int rows = accountMapper.update(account, false);
System.out.println("rows = " + rows);
}
② updateByMap
@Test
public void testUpdateByMap() {
Account account = new Account()
.setId(7L) // 不生效
.setUserName("updateByMap")
.setAge(24)
.setBirthday(null);
// 注意:这样更新实体类的id是不生效的
Map<String, Object> whereCondition = Map.of("id", 13); /**
* 忽略null
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? WHERE `id` = ?
*/
accountMapper.updateByMap(account, whereCondition); /**
* 不忽略null
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
*/
accountMapper.updateByMap(account, false, whereCondition);
}
③ updateByCondition
/**
* updateByCondition(entity, whereConditions):根据查询条件来更新数据。
* updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。
*/
@Test
public void testUpdateByCondition() {
Account account = new Account()
.setId(8L)
.setUserName("updateByCondition")
.setBirthday(DateUtil.toDate(LocalDateTime.now()));
QueryCondition condition = QueryCondition.create(new QueryColumn("id"),
SqlConsts.EQUALS, 8);
/**
* 忽略null
* UPDATE `tb_account` SET `user_name` = ? , `birthday` = ? WHERE `id` = ?
*/
accountMapper.updateByCondition(account, condition); /**
* 不忽略null
* UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
*/
accountMapper.updateByCondition(account, false, condition);
}
④ updateByQuery
/**
* updateByQuery(entity, queryWrapper):根据查询条件来更新数据。
* updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。
*/
@Test
public void testUpdateByQuery() {
Account account = new Account().setUserName("updateByQuery"); /**
* UPDATE `tb_account` SET `user_name` = ? WHERE `id` = ?
*/
accountMapper.updateByQuery(account,
QueryWrapper.create().where(ACCOUNT.ID.eq(9L)));
}
⑤ UpdateEntity部分字段更新
@Test
public void testUpdateEntity1() {
Account account = UpdateEntity.of(Account.class, 10L);
/**
* 官方说明:通过 UpdateEntity 创建的对象,只会更新调用了 setter 方法的字段,若不调用 setter 方法,不管这个对象里的属性的值是什么,都不会更新到数据库。
*/
account.setAge(66)
.setUserName("UpdateEntity")
.setBirthday(null);
accountMapper.update(account);
} @Test
public void testUpdateEntity2() {
Account account = new Account()
.setId(10L)
.setUserName("UpdateEntity2");
UpdateWrapper wrapper = UpdateWrapper.of(account);
// wrapper.set(ACCOUNT.AGE, ACCOUNT.AGE.add(1)); wrapper.setRaw("age", "age + 1");
accountMapper.update(account);
} @Test
public void testUpdateEntity3() {
Account account = new Account()
.setId(10L)
.setUserName("UpdateEntity2");
UpdateWrapper wrapper = UpdateWrapper.of(account); // wrapper.set(ACCOUNT.AGE, ACCOUNT.AGE.add(1)); wrapper.setRaw(ACCOUNT.AGE, "select * from t_account where id = 3");
// accountMapper.updateByCondition(account, wrapper);
}
⑥ UpdateChain
@Test
public void testUpdateChain() {
/**
* UPDATE `tb_account` SET `birthday` = ? , `age` = age + 1 WHERE `age` = ?
*/
UpdateChain.of(Account.class)
.set(Account::getBirthday, new Date())
.setRaw(Account::getAge, "age + 1")
.where(ACCOUNT.AGE.eq(11)).update();
}

set() 和 setRaw() 的区别

在 Row、UpdateWrapper、UpdateChain 中,都提供了 set() 和 setRaw() 两个方法用于设置数据。 那么,他们有什么区别呢?

  • set() 方法用于设置参数数据。
  • setRaw() 用于设置 SQL 拼接数据。

Mybatis-Flex之增、删、改的更多相关文章

  1. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  3. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  4. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  5. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  6. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  7. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据

    第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...

  8. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  9. StringBuilder修改字符串内容,增,删,改,插

    package seday01;/** * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作的性能.因此String不适合频繁修改内容. * 若有频繁修改操作,使用StringBuilder来完 ...

  10. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

随机推荐

  1. 初识Redis与桌面客户端

    Redis介绍 什么是Redis Redis(Remote Dictionary Server) 是一个使用 C 语言编写的,开源的(BSD许可)高性能非关系型(NoSQL)的键值对数据库. Redi ...

  2. Codeforces 1462F The Treasure of The Segments

    题意 给\(n(1\leq n\leq 2*10^5)\)个线段$[l_i,r_i] (1≤l_i≤r_i≤10^9) $,问最少删除几个线段,使得剩下线段中,有至少一个线段与所有线段相交. 分析 对 ...

  3. Nearest cluster-based intrusion detection through convolutional neural networks 笔记

    Nearest cluster-based intrusion detection through convolutional neural networks 技术要点 So, the primary ...

  4. 基于go语言gin框架的web项目骨架

    该骨架每个组件之间可单独使用,组件之间松耦合,高内聚,组件的实现基于其他三方依赖包的封装. 目前该骨架实现了大多数的组件,比如事件,中间件,日志,配置,参数验证,命令行,定时任务等功能,目前可以满足大 ...

  5. 浅谈TCP协议的发生过程

    1. TCP协议 1.1 TCP协议的性质 面向连接的.可靠的.基于字节流 至于为什么面向连接,又为什么可靠,基于字节流的,等后面便可知道. 1.2 TCP协议栈收发数据的四个阶段 创建套接字 连接服 ...

  6. 两种方式,创建有返回值的DB2函数

    函数场景:路径信息由若干个机构编码组成,且一个机构编码是9位字符. 要求:获取路径信息,并且删除路径中包含'99'开头的机构编码. 从客户端及服务器端分别创建ignore99(pathinfo var ...

  7. 分布式事务 —— SpringCloud Alibaba Seata

    Seata 简介 传统的单体应用中,业务操作使用同一条连接操作不同的数据表,一旦出现异常就可以整体回滚.随着公司的快速发展.业务需求的变化,单体应用被拆分成微服务应用,原来的单体应用被拆分成多个独立的 ...

  8. 中国科教工作者协会与CCF PTA联合认证学习须知

    中国科教工作者协会与CCF PTA联合认证学习须知 1.参与认证人员需在科技学堂(www.sciclass.cn)上进行课程学习,然后在PTA官网(pta.ccf.org.cn)报名并参加认证考试,考 ...

  9. 第七单元《中国传统文化与管理》单元测试 mooc

    第七单元<中国传统文化与管理>单元测试 返回 本次得分为:8.00/10.00, 本次测试的提交时间为:2020-08-30, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 ...

  10. Webpack DevServer 代理本地静态目录

    前言 项目里需要离线切片地图,但切片地图文件很多,需求上有时无法单独提供一个独立的文件服务器或者切片服务器,放在 public 难免会在调试运行时被复制到 DevServer 上,而这个操作在低层级少 ...