io.github.dengchen2020.mybatis.base.ComplexMybatisRepository Maven / Gradle / Ivy
package io.github.dengchen2020.mybatis.base;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.dengchen2020.core.exception.call.ViewToastException;
import io.github.dengchen2020.core.support.model.PageParam;
import io.github.dengchen2020.core.support.model.SimplePage;
import org.apache.ibatis.session.ResultHandler;
import java.util.Collections;
import java.util.List;
/**
* @author dengchen
* @since 2024/6/22
*/
public interface ComplexMybatisRepository extends BaseMapper {
/**
* 统一的查querydsl查询接口
*
* @param param 查询参数
* @return LambdaQueryWrapper extends T>
*/
default LambdaQueryWrapper findAllQuery(P param) {
throw new ViewToastException("该功能尚未实现");
}
/**
* Querydsl分页条件查询
*
* @param param 分页参数
* @return 分页后的数据
*/
default SimplePage extends T> findAll(P param, OrderItem... o) {
LambdaQueryWrapper query = findAllQuery(param);
return pageList(query, param, o);
}
/**
* Querydsl分页查询
*
* @param query LambdaQueryWrapper
* @param param 分页参数
* @param o 排序方式
* @return SimplePage
*/
default SimplePage pageList(LambdaQueryWrapper query, PageParam param, OrderItem... o) {
Page page = new Page<>(param.getPage(), param.getSize());
if (param.getSize() != null && param.getSize() == 0) {
return new SimplePage<>(selectCount(query), Collections.emptyList());
}
if (o != null && o.length > 0) {
page.orders().addAll(List.of(o));
}
if (!param.isSelectCount()) {
page.setSearchCount(false);
selectPage(page, query);
return new SimplePage<>(null, page.getRecords());
}
selectPage(page, query);
return new SimplePage<>(page.getTotal(), page.getRecords());
}
/**
* 返回流读取器
*
* @param query LambdaQueryWrapper
* @param param 查询参数
*/
default void streamList(LambdaQueryWrapper query, PageParam param, ResultHandler handler) {
selectList(query,handler);
}
}