前言
接著上一篇:Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 上
倉庫地址:GitHub倉庫
查詢
定義查詢請求體
package com.hy.fmp.dto.req; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/20 19:37 @Description: 查詢條件 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class TestFluentMybatisQueryReq { private String age; private String name; }
查詢寫法1
查詢接口方法定義
/** * 查詢接口1 * * @param queryReq 查詢請求 * @return 列表 */ List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq);
方法實(shí)現(xiàn),這里我們改用了mapper來實(shí)現(xiàn)一下官方給出的查詢語法模式。
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } }
control層方法定義
@ApiOperation(value = "查詢數(shù)據(jù)1", notes = "查詢數(shù)據(jù)1") @RequestMapping(value = "/query1", method = RequestMethod.POST) @ResponseBody public Result<List<TestFluentMybatisEntity>> query1( @RequestBody TestFluentMybatisQueryReq queryReq) { try { return Result.ok(baseService.query1(queryReq)); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
調(diào)試一下接口
一眼望去貌似沒問題,但是長期后端開發(fā)的朋友應(yīng)該能看出來,這個(gè)實(shí)現(xiàn)方式如果一旦age或者name參數(shù)為空的話,那么肯定查不出結(jié)果。因?yàn)榘凑照Z句的寫法,會強(qiáng)制比較age和name兩個(gè)參數(shù)。
我們將其中一個(gè)參數(shù)設(shè)置為空字符串試試看。
不出意料。現(xiàn)在我可以就該方法做調(diào)整,參數(shù)判斷然后替換select語句,為了更優(yōu)雅的實(shí)現(xiàn),我去官方文檔再找找。
查詢寫法2
查詢方法2
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } @Override public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listByMap( true, new HashMap<String, Object>() { { if (!StrUtil.hasEmpty(queryReq.getAge())) { this.put(TestFluentMybatisMapping.age.column, queryReq.getAge()); } if (!StrUtil.hasEmpty(queryReq.getName())) { this.put(TestFluentMybatisMapping.name.column, queryReq.getName()); } } }); } }
代碼說明
我對比了一下官方文檔的寫法,發(fā)現(xiàn)我這個(gè)版本的fm該listByMap方法多一個(gè)isColumn的布爾型參數(shù)。所以我追了一下源碼。
只影響錯(cuò)誤打印,主要就是設(shè)置的map參數(shù)必須要是列名或者實(shí)體對象內(nèi)的參數(shù)名。就不管了。
驗(yàn)證一下
沒什么問題,還是可以查出來。
新問題
但是按照這個(gè)查詢方法,如果兩個(gè)值都傳空字符串會查出全表數(shù)據(jù)嗎?
驗(yàn)證一下
咳咳,報(bào)錯(cuò)了。
所以我還是老老實(shí)實(shí)先把代碼參數(shù)判空優(yōu)化一下。
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } @Override public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) { if (StrUtil.hasEmpty(queryReq.getAge()) && StrUtil.hasEmpty(queryReq.getName())) { return testFluentMybatisMapper.listEntity(new TestFluentMybatisQuery().selectAll()); } return testFluentMybatisMapper.listByMap( true, new HashMap<String, Object>() { { if (!StrUtil.hasEmpty(queryReq.getAge())) { this.put(TestFluentMybatisMapping.age.column, queryReq.getAge()); } if (!StrUtil.hasEmpty(queryReq.getName())) { this.put(TestFluentMybatisMapping.name.column, queryReq.getName()); } } }); } }
驗(yàn)證一下
刪
添加通過ID刪除數(shù)據(jù)的接口方法
/** * 刪除接口 * * @param id id */ void deleteById(Integer id);
實(shí)現(xiàn)接口方法
@Override public void deleteById(Integer id) { testFluentMybatisMapper.deleteById(id); }
驗(yàn)證一下
刪除成功
總結(jié)
這兩篇文章主要是將之前的項(xiàng)目進(jìn)行工程化改造,增加了文檔、接口等一些列常規(guī)化操作。實(shí)現(xiàn)了數(shù)據(jù)庫表的基本增刪改查功能。其他的功能會在之后慢慢更新,fm融合了很多其他orm框架的東西,需要慢慢摸索摸索。
如果本文對你有幫助,請點(diǎn)個(gè)贊支持一下吧。
到此這篇關(guān)于Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 下的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/120875177