Fluent Mybatis学习之Update语法实践

网友投稿 861 2022-11-20

Fluent Mybatis学习之Update语法实践

Fluent Mybatis学习之Update语法实践

目录前言数据准备Update语法简单的写法UpdateByEntity根据表实体更新数据UpdateByExclude根据表实体排除更新数据applyFunc总结

前言

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

本项目github地址:项目仓库

数据准备

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

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 updateSimple() {

try {

return Result.ok(updateService.updateSimple());

} catch (Exception exception) {

return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);

}

}

验证一下

代码说明

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 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);

}

}

验证一下

代码说明

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两个字段,所以只更新这两项。

UpdateByExclude根据表实体排除更新数据

fm对排除字段情有独钟,简单理解一下,就是在设置字段的时候,byEntity是只选定选择的字段,byExclude是只排除选择的字段。

接口层代码添加

/**

* 根据排除项更新语法

*

* @param req 实体参数

* @return

*/

Integer updateByExclude(TestFluentMybatisEntity req);

实现类代码添加

@Override

public Integer updateByExclude(TestFluentMybatisEntity req) {

return testFluentMybatisMapper.updateBy(

new TestFluentMybatisUpdate()

.set

.byExclude(req, TestFluentMybatisEntity::getAge, TestFluentMybatisEntity::getDelFlag)

.end()

.where

.id()

.eq(2)

.end());

}

控制层代码添加

@Autowired private IUpdateService updateService;

@ApiOperation(value = "根据排除项更新语法", notes = "根据排除项更新语法")

@RequestMapping(value = "/updateByExclude", method = RequestMethod.POST)

@ResponseBody

public Result updateByExclude(@RequestBody TestFluentMybatisEntity req) {

try {

return Result.ok(updateService.updateByExclude(req));

} catch (Exception exception) {

return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);

}

}

验证一下

代码说明

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

2021-11-23 15:21:42.262 DEBUG 20820 --- [nio-8090-exec-6] c.h.f.f.m.T.updateBy : ==> Preparing: UPDATE `test_fluent_mybatis` SET `name` = ?, `create_time` = ? WHERE `id` = ?

2021-11-23 15:21:42.266 DEBUG 20820 --- [nio-8090-exec-6] c.h.f.f.m.T.updateBy : ==> Parameters: 李四(String), null, 2(Integer)

2021-11-23 15:21:42.271 DEBUG 20820 --- [nio-8090-exec-6] c.h.f.f.m.T.updateBy : <== Updates: 1

2、这里需要注意,实体中没有给create_time设值,所以会把其设置为null,官方原话为:未指定字段列表时, 更新除主键外字段(包括null字段)

3、而我指定了字段age和del_flag,但是并没有作用,这就是byExclude的作用,官方原话为:排除指定字段。

applyFunc

可以在更新语法中,增加对字段的函数操作,使用applyFunc即可,这个方法还是很好用的,简化代码。

接口层代码添加

/**

* 使用applyFunc更新语法

*

* @return

*/

Integer updateByApplyFunc();

实现类代码添加

@Override

public Integer updateByApplyFunc() {

return testFluentMybatisMapper.updateBy(

new TestFluentMybatisUpdate()

.set

.name()

.applyFunc("concat(name, ?)", "_男")

.set

.age()

.applyFunc("age+5")

.end()

.where

.id()

.eq(2)

.end());

}

控制层代码添加

@Autowired private IUpdateService updateService;

@ApiOperation(value = "使用applyFunc更新语法", notes = "使用applyFunc更新语法")

@RequestMapping(value = "/updateByApplyFunc", method = RequestMethod.GET)

@ResponseBody

public Result updateByApplyFunc() {

try {

return Result.ok(updateService.updateByApplyFunc());

} catch (Exception exception) {

return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);

}

}

验证一下

代码说明

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

2021-11-23 15:31:09.772 DEBUG 20820 --- [nio-8090-exec-8] c.h.f.f.m.T.updateBy : ==> Preparing: UPDATE `test_fluent_mybatis` SET `name` = concat(name, ?), `age` = age+5 WHERE `id` = ?

2021-11-23 15:31:09.782 DEBUG 20820 --- [nio-8090-exec-8] c.h.f.f.m.T.updateBy : ==> Parameters: _男(String), 2(Integer)

2021-11-23 15:31:09.787 DEBUG 20820 --- [nio-8090-exec-8] c.h.f.f.m.T.updateBy : <== Updates: 1

2、sql可以看出,将那么字段拼接了后缀"_男",同时年龄增加5岁。

总结

总的来看,fm提供的方法还是很优越的,后面会继续把fm剩下的功能调试完。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:青蛙跳杯子
下一篇:make: *** [ext/phar/phar.php] Error 127
相关文章

 发表评论

暂时没有评论,来抢沙发吧~