MyBatis-Plus 通用查询工具类

mybatis-plus简称MP是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

这是官方给的定义,关于mybatis-plus的更多介绍及特性,可以参考mybatis-plus官网 。那么它是怎么增强的呢?其实就是它已经封装好了一些crud方法,我们不需要再写xml了,直接调用这些方法就行,就类似于JPA。

下面是在MP为基础封装了一个查询类,实现了如果需要增加查询条件只需在前端修改即可。

查询工具

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

    List list();

    // 查询列表
    List list(SearchModel searchModel);

    // 查询(根据ID 批量查询)
    Collection listByIds(Collection 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 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 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);
    }
}

MP实现了mapper层基础的CRUD方法,这里把一些常用的service层的方法整理了一下,又减少了一些代码量

代码地址

使用示例

你可能感兴趣的:(java,springboot,mybatis-plus)