MyBatis-Plus中怎么实现一个通用查询工具类
本篇文章为大家展示了MyBatis-Plus 中怎么实现一个通用查询工具类,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、怒江州网站维护、网站推广。
查询工具
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对象即可完成一个查询服务,查询条件通过前端传入的字段控制
BaseService
IBaseService
public interface IBaseService{ T save(T entity) throws Exception; boolean saveBatch(Collection entityList); // TableId 注解存在更新记录,否插入一条记录 boolean saveOrUpdate(T entity); // 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper updateWrapper); // 批量修改插入 boolean saveOrUpdateBatch(Collection entityList); // 根据 entity 条件,删除记录 boolean remove(Wrapper queryWrapper); // 根据 ID 删除 boolean removeById(Serializable id); // 根据 columnMap 条件,删除记录 boolean removeByMap(Map columnMap); // 删除(根据ID 批量删除) boolean removeByIds(Collection extends Serializable> idList); List list(); // 查询列表 List list(SearchModel searchModel); // 查询(根据ID 批量查询) Collection listByIds(Collection extends Serializable> idList); // 查询(根据 columnMap 条件) Collection listByMap(Map columnMap); // 翻页查询 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); } }
上述内容就是MyBatis-Plus 中怎么实现一个通用查询工具类,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
当前文章:MyBatis-Plus中怎么实现一个通用查询工具类
分享网址:http://azwzsj.com/article/jsocdd.html