Fluent Mybatis學習之Update語法實踐
前言
本篇文章主要針對update語法進行驗證。
本項目Github地址:項目倉庫
數(shù)據(jù)準備
還是用之前在數(shù)據(jù)庫存的數(shù)據(jù),數(shù)據(jù)如下:

Update語法
簡單的寫法
fm的update簡單寫法可以直接使用is()來對表字段進行賦值,如果需要將字段設置為Null的話,直接使用isNull()方法。
接口層代碼添加
/** * 簡單的更新語法 * * @return */ Integer updateSimple();
實現(xiàn)類代碼添加
@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);
}
}
驗證一下


代碼說明
1、可以看一下代碼執(zhí)行的具體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根據(jù)表實體更新數(shù)據(jù)
fm支持使用表實體進行數(shù)據(jù)更新,但是有一些限制,具體看我后面的代碼說明。
接口層代碼添加
/** * 根據(jù)實體更新語法 * * @param req 實體參數(shù) * @return */ Integer updateByEntity(TestFluentMybatisEntity req);
實現(xiàn)類代碼添加
@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 = "根據(jù)實體更新語法", notes = "根據(jù)實體更新語法")
@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);
}
}
驗證一下


代碼說明
1、先看看sql的執(zhí)行語句,如下圖:
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方法支持傳遞字段參數(shù),很好理解,只更新傳遞的字段值,不論是否為null。官方原話為:指定字段時,更新指定的字段(包括null字段), 但指定主鍵無效。
4、我在方法中選定了name、age兩個字段,所以只更新這兩項。
UpdateByExclude根據(jù)表實體排除更新數(shù)據(jù)
fm對排除字段情有獨鐘,簡單理解一下,就是在設置字段的時候,byEntity是只選定選擇的字段,byExclude是只排除選擇的字段。
接口層代碼添加
/** * 根據(jù)排除項更新語法 * * @param req 實體參數(shù) * @return */ Integer updateByExclude(TestFluentMybatisEntity req);
實現(xiàn)類代碼添加
@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 = "根據(jù)排除項更新語法", notes = "根據(jù)排除項更新語法")
@RequestMapping(value = "/updateByExclude", method = RequestMethod.POST)
@ResponseBody
public Result<Integer> 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的執(zhí)行語句,如下圖:
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
可以在更新語法中,增加對字段的函數(shù)操作,使用applyFunc即可,這個方法還是很好用的,簡化代碼。
接口層代碼添加
/** * 使用applyFunc更新語法 * * @return */ Integer updateByApplyFunc();
實現(xiàn)類代碼添加
@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<Integer> updateByApplyFunc() {
try {
return Result.ok(updateService.updateByApplyFunc());
} catch (Exception exception) {
return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
}
}
驗證一下


代碼說明
1、先看看sql的執(zhí)行語句,如下圖:
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提供的方法還是很優(yōu)越的,后面會繼續(xù)把fm剩下的功能調試完。
到此這篇關于FluentMybatis學習之Update語法實踐的文章就介紹到這了,更多相關FluentMybatis的內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatis?handleResultSet結果集解析過程示例
這篇文章主要為大家介紹了MyBatis?handleResultSet結果集解析過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

