FluentMybatis Update语法 | FluentMybatis实践

简介: FluentMybatis Update语法 | FluentMybatis实践

本篇文章主要针对update语法进行验证。

本项目Github地址:项目仓库

数据准备

还是用之前在数据库存的数据,数据如下:

image.png


Update语法

简单的写法

fm的update简单写法可以直接使用is()来对表字段进行赋值,如果需要将字段设置为Null的话,直接使用isNull()方法。

接口层代码添加

/**
 * 简单的更新语法
 *
 * @return
 */
Integer updateSimple();

实现类代码添加

@Override
public Integer updateSimple() {
  return testFluentMybatisMapper.updateBy(
      new TestFluentMybatisUpdate()
          .set
          .name()
          .is("何九")
          .set
          .age()
          .isNull()
          .end()
          .where
          .id()
          .eq(2)
          .end());
}

控制层代码添加


@Autowired private IUpdateService updateService;
@ApiOperation(value = "简单语法", notes = "简单语法")
@RequestMapping(value = "/simple", method = RequestMethod.GET)
@ResponseBody
public Result<Integer> updateSimple() {
  try {
    return Result.ok(updateService.updateSimple());
  } catch (Exception exception) {
    return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  }

验证一下


image.png

image.png



代码说明


1、可以看一下代码执行的具体sql,如下:


2021-11-23 13:41:20.277 DEBUG 20820 --- [nio-8090-exec-1] c.h.f.f.m.T.updateBy                     : ==>  Preparing: UPDATE `test_fluent_mybatis` SET `name` = ?, `age` = ? WHERE `id` = ?
2021-11-23 13:41:20.464 DEBUG 20820 --- [nio-8090-exec-1] c.h.f.f.m.T.updateBy                     : ==> Parameters: 何九(String), null, 2(Integer)
2021-11-23 13:41:20.470 DEBUG 20820 --- [nio-8090-exec-1] c.h.f.f.m.T.updateBy                     : <==    Updates: 1


UpdateByEntity根据表实体更新数据

fm支持使用表实体进行数据更新,但是有一些限制,具体看我后面的代码说明。


接口层代码添加

/**
 * 根据实体更新语法
 *
 * @param req 实体参数
 * @return
 */
Integer updateByEntity(TestFluentMybatisEntity req);

实现类代码添加

@Override
public Integer updateByEntity(TestFluentMybatisEntity req) {
  return testFluentMybatisMapper.updateBy(
      new TestFluentMybatisUpdate()
          .set
          .byEntity(req, Ref.Field.TestFluentMybatis.name, Ref.Field.TestFluentMybatis.age)
          .end()
          .where
          .id()
          .eq(2)
          .end());
}

控制层代码添加


@Autowired private IUpdateService updateService;
@ApiOperation(value = "根据实体更新语法", notes = "根据实体更新语法")
@RequestMapping(value = "/updateByEntity", method = RequestMethod.POST)
@ResponseBody
public Result<Integer> updateByEntity(@RequestBody TestFluentMybatisEntity req) {
  try {
    return Result.ok(updateService.updateByEntity(req));
  } catch (Exception exception) {
    return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  }



验证一下

屏幕快照 2022-06-08 下午3.04.40.png




代码说明


1、先看看sql的执行语句,如下图:


2021-11-23 13:56:16.572 DEBUG 20820 --- [nio-8090-exec-4] c.h.f.f.m.T.updateBy                     : ==>  Preparing: UPDATE `test_fluent_mybatis` SET `name` = ?, `age` = ? WHERE `id` = ?
2021-11-23 13:56:16.573 DEBUG 20820 --- [nio-8090-exec-4] c.h.f.f.m.T.updateBy                     : ==> Parameters: 李四(String), 29(Integer), 2(Integer)
2021-11-23 13:56:16.580 DEBUG 20820 --- [nio-8090-exec-4] c.h.f.f.m.T.updateBy                     : <==    Updates: 1

2、这里需要注意,在使用byEntity方法的时候,不会使用entity中的id作为判断的。官方原话为:未指定字段列表时, 更新除主键外非null字段。


3、byEntity方法支持传递字段参数,很好理解,只更新传递的字段值,不论是否为null。官方原话为:指定字段时,更新指定的字段(包括null字段), 但指定主键无效。


4、我在方法中选定了name、age两个字段,所以只更新这两项。


相关文章
|
5月前
|
XML SQL Java
今日记录:学习一个Mybatis的技能之choose 和 bind
今日记录:学习一个Mybatis的技能之choose 和 bind
51 1
|
11月前
|
SQL
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践(2)
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践(2)
|
XML SQL Java
mybatis开发要点-insert主键ID获取和多参数传递
mybatis开发要点-insert主键ID获取和多参数传递
193 0
FluentMybatis Where语法(一) | FluentMybatis实践(2)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(2)
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践
|
SQL 关系型数据库 MySQL
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践(1)
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践(1)
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践(2)
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践
FluentMybatis 分页查询、sql日志输出 | FluentMybatis实践(2)