您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。