关于java:MyBatisPlus-通用查询工具类

41次阅读

共计 4588 个字符,预计需要花费 12 分钟才能阅读完成。

mybatis-plus 简称 MP 是一个 Mybatis 的加强工具,在 Mybatis 的根底上只做加强不做扭转,为简化开发、提高效率而生。

这是官网给的定义,对于 mybatis-plus 的更多介绍及个性,能够参考 mybatis-plus 官网。那么它是怎么加强的呢?其实就是它曾经封装好了一些 crud 办法,咱们不须要再写 xml 了,间接调用这些办法就行,就相似于 JPA。

上面是在 MP 为根底封装了一个查问类,实现了如果须要减少查问条件只需在前端批改即可。

查问工具

  • 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 对象即可实现一个查问服务, 查问条件通过前端传入的字段管制

BaseService

  • 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);
    }
}

MP 实现了 mapper 层根底的 CRUD 办法,这里把一些罕用的 service 层的办法整顿了一下,又缩小了一些代码量

代码地址

应用示例

正文完
 0