import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.reflection.property.PropertyNamer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CustomMapper<T> {
public BaseMapper<T> mapper;
private SCWrapper scWrapper;
private SWrapper sWrapper;
private UWrapper uWrapper;
private DWrapper dWrapper;
public abstract class AbstractWrapper<T,K extends AbstractWrapper<T,K>>{
protected AbstractLambdaWrapper<T, ?> lambdaWrapper;
public void clear(){
lambdaWrapper.clear();
}
public K eq(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.eq(pFunc,value);
return kThis();
}
public K in(SFunction<T, ?> pFunc, List value) {
lambdaWrapper.in(pFunc,value);
return kThis();
}
public K ge(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.ge(pFunc,value);
return kThis();
}
public K le(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.le(pFunc,value);
return kThis();
}
public K like(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.like(pFunc,value);
return kThis();
}
public K likeRight(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.likeRight(pFunc,value);
return kThis();
}
public K likeLeft(SFunction<T, ?> pFunc, Object value) {
lambdaWrapper.likeLeft(pFunc,value);
return kThis();
}
private K kThis(){
return (K)this;
}
}
public class SCWrapper extends AbstractWrapper<T,SCWrapper>{
private final QueryWrapper<T> wrapper = new QueryWrapper<>();
private final List<String> cols = new ArrayList<>();
public void clear(){
wrapper.clear();
cols.clear();
}
public SCWrapper col(SFunction<T, ?> pFunc) {
cols.add(this.to_(pFunc));
return this;
}
public <K> SCWrapper sum(SFunction<T, ?> pFunc,SFunction<K, ?> aFunc) {
cols.add("sum("+this.to_(pFunc)+") as "+this.toCamelCase(aFunc));
return this;
}
public <K> SCWrapper count(SFunction<T, ?> pFunc,SFunction<K, ?> aFunc) {
cols.add("count("+this.to_(pFunc)+") as "+this.toCamelCase(aFunc));
return this;
}
public SCWrapper groupBy(SFunction<T, ?> pFunc) {
wrapper.groupBy(this.to_(pFunc));
return this;
}
@Override
public SCWrapper eq(SFunction<T, ?> pFunc, Object value) {
wrapper.eq(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper in(SFunction<T, ?> pFunc, List value) {
wrapper.in(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper ge(SFunction<T, ?> pFunc, Object value) {
wrapper.ge(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper le(SFunction<T, ?> pFunc, Object value) {
wrapper.le(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper like(SFunction<T, ?> pFunc, Object value) {
wrapper.like(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper likeRight(SFunction<T, ?> pFunc, Object value) {
wrapper.likeRight(this.to_(pFunc),value);
return this;
}
@Override
public SCWrapper likeLeft(SFunction<T, ?> pFunc, Object value) {
wrapper.likeLeft(this.to_(pFunc),value);
return this;
}
public <K> List<K> select(Class<K> classK){
wrapper.select(cols.toArray(new String[]{}));
return mapper.selectMaps(wrapper)
.stream()
.map(map -> BeanUtil.mapToBean(map,classK,false,new CopyOptions()))
.collect(Collectors.toList());
}
private String toCamelCase(SFunction<?, ?> pFunc){
return PropertyNamer.methodToProperty(
LambdaUtils
.extract(pFunc)
.getImplMethodName());
}
private String to_(SFunction<?, ?> pFunc){
return this.to_(this.toCamelCase(pFunc));
}
private String to_(String camelCase) {
return StrUtil.toUnderlineCase(camelCase);
}
}
public class SWrapper extends AbstractWrapper<T,SWrapper>{
private final LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
public SWrapper() {
super.lambdaWrapper = wrapper;
}
public List<T> select(){
return mapper.selectList(wrapper);
}
public <E> List<E> select(Class<E> classE){
return mapper.selectList(wrapper)
.stream()
.map(t -> BeanUtil.copyProperties(t,classE))
.collect(Collectors.toList());
}
public T selectOne(){
return mapper.selectOne(wrapper);
}
public <E> E selectOne(Class<E> classE){
T t = mapper.selectOne(wrapper);
if(t == null)
return null;
else
return BeanUtil.copyProperties(t,classE);
}
public Page<T> selectPage(Integer current,Integer size){
return mapper.selectPage(Page.of(current,size),wrapper);
}
public <E> Page<E> selectPage(Integer current,Integer size,Class<E> classE){
Page<T> page = mapper.selectPage(Page.of(current,size),wrapper);
List<E> eList = page.getRecords()
.stream()
.map(t -> BeanUtil.copyProperties(t,classE))
.collect(Collectors.toList());
return Page.<E>of(page.getCurrent(),page.getSize())
.setRecords(eList);
}
}
public class UWrapper extends AbstractWrapper<T,UWrapper>{
private final LambdaUpdateWrapper<T> wrapper = new LambdaUpdateWrapper<>();
public UWrapper() {
this.lambdaWrapper = wrapper;
}
public UWrapper set(SFunction<T, ?> pFunc, Object value) {
wrapper.set(pFunc,value);
return this;
}
public Integer update(){
return mapper.update(null,wrapper);
}
}
public class DWrapper extends AbstractWrapper<T,DWrapper>{
private final LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
public DWrapper() {
this.lambdaWrapper = wrapper;
}
public Integer delete(){
return mapper.delete(wrapper);
}
}
private CustomMapper(BaseMapper<T> mapper) {
this.mapper = mapper;
}
public static <T> CustomMapper<T> builder(BaseMapper<T> mapper) {
return new CustomMapper<>(mapper);
}
public SCWrapper scWrapper() {
scWrapper = this.initWrapper(scWrapper,SCWrapper.class);
return scWrapper;
}
public SWrapper sWrapper() {
sWrapper = this.initWrapper(sWrapper,SWrapper.class);
return sWrapper;
}
public UWrapper uWrapper() {
uWrapper = this.initWrapper(uWrapper,UWrapper.class);
return uWrapper;
}
public DWrapper dWrapper() {
dWrapper = this.initWrapper(dWrapper,DWrapper.class);
return dWrapper;
}
private <K extends AbstractWrapper> K initWrapper(K k,Class<K> classK){
if(k == null) {
try {
return classK.getConstructor(CustomMapper.class)
.newInstance(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}else
k.clear();
return k;
}
}
使用示例:
CustomMapper<Employee> csm = CustomMapper.builder(this.employeeMapper);
//查询
List<Employee> Employees = csm
.sWrapper()
.eq(Employee::getDeptId,123L)
.select();
List<EmpSalarySumDTO> empSalarySumDTOS = csm
.scWrapper()
.sum(Employee::getSalary,EmpSalarySumDTO::getSalarySum)
.col(Employee::getDeptId)
.groupBy(Employee::getDeptId)
.select(EmpSalarySumDTO.class);
//修改
csm.uWrapper()
.set(Employee::getName,"张三")
.eq(Employee::getEmpId,6001L)
.update();
//删除
csm.dWrapper()
.eq(Employee::getEmpId,6001L)
.delete();