您好,登錄后才能下訂單哦!
本篇文章為大家展示了MyBatis-Plus 中怎么實現一個通用查詢工具類,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
SearchModel
public class SearchModel<T> { private Integer pageIndex; private Integer pageSize; private List<Field> fields; private String orderField; private boolean isAsc; public IPage<T> getPage() { IPage<T> 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<T> getQueryModel() { QueryWrapper<T> 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對象即可完成一個查詢服務,查詢條件通過前端傳入的字段控制
IBaseService
public interface IBaseService<T> { T save(T entity) throws Exception; boolean saveBatch(Collection<T> entityList); // TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity); // 根據updateWrapper嘗試更新,否繼續執行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList); // 根據 entity 條件,刪除記錄 boolean remove(Wrapper<T> queryWrapper); // 根據 ID 刪除 boolean removeById(Serializable id); // 根據 columnMap 條件,刪除記錄 boolean removeByMap(Map<String, Object> columnMap); // 刪除(根據ID 批量刪除) boolean removeByIds(Collection<? extends Serializable> idList); List<T> list(); // 查詢列表 List<T> list(SearchModel<T> searchModel); // 查詢(根據ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList); // 查詢(根據 columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap); // 翻頁查詢 IPage<T> page(SearchModel<T> searchModel); T selectById(Serializable id); T selectOne(Wrapper<T> queryWrapper); }
BaseServiceImpl
public class BaseServiceImpl<M extends BaseMapper<T>, T> implements IBaseService<T> { @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<T> 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<T> updateWrapper) { return false; } @Transactional(rollbackFor = RuntimeException.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList) { for (T entity : entityList) { saveOrUpdate(entity); } return true; } @Override public boolean remove(Wrapper<T> queryWrapper) { return baseMapper.delete(queryWrapper) > 0; } @Override public boolean removeById(Serializable id) { return baseMapper.deleteById(id) > 0; } @Override public boolean removeByMap(Map<String, Object> columnMap) { return baseMapper.deleteByMap(columnMap) > 0; } @Override public boolean removeByIds(Collection<? extends Serializable> idList) { return baseMapper.deleteBatchIds(idList) > 0; } @Override public List<T> list() { return baseMapper.selectList(new QueryWrapper<T>()); } @Override public List<T> list(SearchModel<T> searchModel) { return baseMapper.selectList(searchModel.getQueryModel()); } @Override public Collection<T> listByIds(Collection<? extends Serializable> idList) { return baseMapper.selectBatchIds(idList); } @Override public Collection<T> listByMap(Map<String, Object> columnMap) { return baseMapper.selectByMap(columnMap); } @Override public IPage<T> page(SearchModel<T> searchModel) { return baseMapper.selectPage(searchModel.getPage(), searchModel.getQueryModel()); } @Override public T selectById(Serializable id) { return baseMapper.selectById(id); } @Override public T selectOne(Wrapper<T> queryWrapper) { return baseMapper.selectOne(queryWrapper); } }
上述內容就是MyBatis-Plus 中怎么實現一個通用查詢工具類,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。