本篇文章為大家展示了MyBatis-Plus 中怎么實(shí)現(xiàn)一個(gè)通用查詢(xún)工具類(lèi),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、怒江州網(wǎng)站維護(hù)、網(wǎng)站推廣。
SearchModel
public class SearchModel{ private Integer pageIndex; private Integer pageSize; private List fields; private String orderField; private boolean isAsc; public IPage getPage() { IPage page = new Page<>(pageIndex, pageSize); if (!StringUtil.isEmptyOrNull(orderField)) { OrderItem orderItem = new OrderItem(); orderItem.setAsc(isAsc); orderItem.setColumn(orderField); page.orders().add(orderItem); } return page; } public QueryWrapper getQueryModel() { QueryWrapper queryWrapper = new QueryWrapper<>(); for (Iterator iter = this.fields.iterator(); iter.hasNext(); ) { Field field = (Field) iter.next(); switch (field.getQueryMethod()) { case eq: queryWrapper.eq(true, field.getName(), field.getValue()); break; case like: queryWrapper.like(true, field.getName(), field.getValue()); } } if (!StringUtil.isEmptyOrNull(orderField)) { queryWrapper.orderBy(true, isAsc, orderField); } return queryWrapper; } }
Field
public class Field { public Field(String name, Object value) { this.name = name; this.value = value; this.queryMethod = QueryMethod.eq; } public Field(String name, Object value, QueryMethod queryMethod) { this.name = name; this.value = value; this.queryMethod = queryMethod; } private String name; private Object value; private QueryMethod queryMethod; }
QueryMethod
public enum QueryMethod { eq, like }
{ "fields": [ { "value": "v", "name": "project_code", "queryMethod": "eq" }, { "name": "type", "queryMethod": "like", "value": "b" }, { "name": "id", "queryMethod": "like", "value": "a" } ], "pageIndex": 1, "pageSize": 8, "orderField": "type", "isAsc": "false" }
在api中傳入上面的json對(duì)象即可完成一個(gè)查詢(xún)服務(wù),查詢(xún)條件通過(guò)前端傳入的字段控制
IBaseService
public interface IBaseService{ T save(T entity) throws Exception; boolean saveBatch(Collection entityList); // TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity); // 根據(jù)updateWrapper嘗試更新,否繼續(xù)執(zhí)行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection entityList); // 根據(jù) entity 條件,刪除記錄 boolean remove(Wrapper queryWrapper); // 根據(jù) ID 刪除 boolean removeById(Serializable id); // 根據(jù) columnMap 條件,刪除記錄 boolean removeByMap(Map columnMap); // 刪除(根據(jù)ID 批量刪除) boolean removeByIds(Collection extends Serializable> idList); List list(); // 查詢(xún)列表 List list(SearchModel searchModel); // 查詢(xún)(根據(jù)ID 批量查詢(xún)) Collection listByIds(Collection extends Serializable> idList); // 查詢(xún)(根據(jù) columnMap 條件) Collection listByMap(Map columnMap); // 翻頁(yè)查詢(xún) IPage page(SearchModel searchModel); T selectById(Serializable id); T selectOne(Wrapper queryWrapper); }
BaseServiceImpl
public class BaseServiceImpl, T> implements IBaseService { @Autowired private M baseMapper; @Override public T save(T entity) throws Exception { baseMapper.insert(entity); return entity; } @Transactional(rollbackFor = RuntimeException.class) @Override public boolean saveBatch(Collection entityList) { Integer size = entityList.size(); for (T entity : entityList) { baseMapper.insert(entity); size++; } return size == entityList.size(); } @Override public boolean saveOrUpdate(T entity) { int rs = baseMapper.updateById(entity); if (rs > 0) return true; return baseMapper.insert(entity) > 0; } @Override public boolean saveOrUpdate(T entity, Wrapper updateWrapper) { return false; } @Transactional(rollbackFor = RuntimeException.class) @Override public boolean saveOrUpdateBatch(Collection entityList) { for (T entity : entityList) { saveOrUpdate(entity); } return true; } @Override public boolean remove(Wrapper queryWrapper) { return baseMapper.delete(queryWrapper) > 0; } @Override public boolean removeById(Serializable id) { return baseMapper.deleteById(id) > 0; } @Override public boolean removeByMap(Map columnMap) { return baseMapper.deleteByMap(columnMap) > 0; } @Override public boolean removeByIds(Collection extends Serializable> idList) { return baseMapper.deleteBatchIds(idList) > 0; } @Override public List list() { return baseMapper.selectList(new QueryWrapper ()); } @Override public List list(SearchModel searchModel) { return baseMapper.selectList(searchModel.getQueryModel()); } @Override public Collection listByIds(Collection extends Serializable> idList) { return baseMapper.selectBatchIds(idList); } @Override public Collection listByMap(Map columnMap) { return baseMapper.selectByMap(columnMap); } @Override public IPage page(SearchModel searchModel) { return baseMapper.selectPage(searchModel.getPage(), searchModel.getQueryModel()); } @Override public T selectById(Serializable id) { return baseMapper.selectById(id); } @Override public T selectOne(Wrapper queryWrapper) { return baseMapper.selectOne(queryWrapper); } }
上述內(nèi)容就是MyBatis-Plus 中怎么實(shí)現(xiàn)一個(gè)通用查詢(xún)工具類(lèi),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。