All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mybaits.jpa.service.MyBatisJapService Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package com.mybaits.jpa.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybaits.jpa.jpaEnum.KeyWord;
import com.mybaits.jpa.sql.wrapper.JpaQueryWrapper;
import com.mybaits.jpa.util.PageInfoHelp;
import com.mybaits.jpa.util.SqlUtils;
import org.apache.commons.collections.map.HashedMap;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/11/13 0013.
 */
public class MyBatisJapService, T> extends ServiceImpl {


    JpaQueryWrapper jpaQueryWrapper=new JpaQueryWrapper();
    /**
     * 查询分页
     * @param t 查询条件
     * @param pageNum 分页页码
     * @param pageSize 分页条数
     * @return
     */
    public PageInfo jpaPageInfo(T t,Integer pageNum,Integer pageSize){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        PageInfoHelp.setDefault(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper=jpaQueryWrapper.getPageInfoQueryWrapper(t);
        List infos = baseMapper.selectList(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],infos);
        return new PageInfo(infos);
    }
    /**
     * 查询分页
     * @param t 泛型分页对象
     * @param pageNum 分页页码
     * @param pageSize 分页条数
     * @return
     */
    public PageInfo jpaPageInfo(T t, QueryWrapper queryWrapper,Integer pageNum,Integer pageSize){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        PageInfoHelp.setDefault(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        queryWrapper=jpaQueryWrapper.getPageInfoQueryWrapper(t,queryWrapper);
        List infos = baseMapper.selectList(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],infos);
        return new PageInfo(infos);
    }


    /**
     * 单个查询
     * @param params 查询参数
     * @return
     */
    public int jpaCount(Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.countBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords= SqlUtils.getKeyword(methodName);
        QueryWrapper queryWrapper=jpaQueryWrapper.getQueryWrapper(keyWords,attributes,params);
        int  count=baseMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * 单个查询
     * @param params 查询参数
     * @return
     */
    public int jpaCount(QueryWrapper queryWrapper,Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.countBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        queryWrapper=jpaQueryWrapper.getQueryWrapper(queryWrapper,keyWords,attributes,params);
        int  count=baseMapper.selectCount(queryWrapper);
        return count;
    }





    /**
     * 单个查询
     * @param params 查询参数
     * @return
     */
    public T jpaFindByOne(Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.findBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        QueryWrapper queryWrapper=jpaQueryWrapper.getQueryWrapper(keyWords,attributes,params);
        T t=baseMapper.selectOne(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],t);
        return t;
    }


    /**
     * 单个查询
     * @param params 查询参数
     * @return
     */
    public T jpaFindByOne(QueryWrapper queryWrapper,Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.findBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        queryWrapper=jpaQueryWrapper.getQueryWrapper(queryWrapper,keyWords,attributes,params);
        T t=baseMapper.selectOne(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],t);
        return t;
    }

    /**
     * 集合查询
     * @param params 查询参数
     * @return
     */
    public List jpaFindByMany(Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.findBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        QueryWrapper queryWrapper=jpaQueryWrapper.getQueryWrapper(keyWords,attributes,params);
        List list=baseMapper.selectList(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],list);
        return list;
    }
    /**
     * 集合查询
     * @param params 查询参数
     * @return
     */
    public List jpaFindByMany(QueryWrapper queryWrapper,Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.findBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        queryWrapper=jpaQueryWrapper.getQueryWrapper(queryWrapper,keyWords,attributes,params);
        List list=baseMapper.selectList(queryWrapper);
        LinkTableSelect linkTableSelect=new LinkTableSelect();
        linkTableSelect.baseMapper=baseMapper;
        linkTableSelect.linkSelect(stackTrace[1],list);
        return list;
    }

    /**
     * jpa delete
     * @param params 删除参数
     * @return 返回影响行数
     */
    public int jpaDelete(Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.deleteBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        QueryWrapper queryWrapper=jpaQueryWrapper.getQueryWrapper(keyWords,attributes,params);
        return baseMapper.delete(queryWrapper);
    }

    /**
     * jpa delete
     * @param params 删除参数
     * @return 返回影响行数
     */
    public int jpaDelete(QueryWrapper queryWrapper,Object...params){
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        String methodName = stackTrace[1].getMethodName();
        methodName=methodName.replace(KeyWord.deleteBy.getValue(), KeyWord.And.getValue());
        Map attributes=new HashedMap();
        methodName=setMethodAttribute(methodName,attributes);
        List keyWords=SqlUtils.getKeyword(methodName);
        queryWrapper=jpaQueryWrapper.getQueryWrapper(queryWrapper,keyWords,attributes,params);
        return baseMapper.delete(queryWrapper);
    }


    public boolean saveBatch(Collection entityList) {
        if(entityList!=null&&entityList.size()>0){
            return super.saveBatch(entityList);
        }
        return true;
    }

    public boolean saveOrUpdateBatch(Collection entityList) {
        if(entityList!=null&&entityList.size()>0){
            return super.saveOrUpdateBatch(entityList);
        }
        return true;
    }

    @Override
    public boolean saveBatch(Collection entityList, int batchSize) {
        if(entityList!=null&&entityList.size()>0){
            return super.saveBatch(entityList, batchSize);
        }
        return true;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection entityList, int batchSize) {
        if(entityList!=null&&entityList.size()>0){
            return super.saveOrUpdateBatch(entityList, batchSize);
        }
        return true;
    }

    public boolean updateBatchById(Collection entityList) {
        if(entityList!=null&&entityList.size()>0){
            return super.updateBatchById(entityList);
        }
        return true;
    }

    @Override
    public boolean updateBatchById(Collection entityList, int batchSize) {
        if(entityList!=null&&entityList.size()>0){
            return super.updateBatchById(entityList, batchSize);
        }
        return true;
    }

    @Override
    public boolean removeByIds(Collection idList) {
        if(idList!=null&&idList.size()>0){
            return super.removeByIds(idList);
        }
        return true;
    }

    @Override
    public boolean removeByMap(Map columnMap) {
        if(columnMap!=null&&columnMap.keySet().size()>0){
            return super.removeByMap(columnMap);
        }
        return true;
    }


    /**
     * 设置参数统配符号
     * @param method
     * @return
     */
    public String setMethodAttribute(String method,Map attributes){
        Type superClass = getClass().getGenericSuperclass();
        Type type = ((ParameterizedType) superClass).getActualTypeArguments()[1];
        Class classType =null;
        if (type instanceof ParameterizedType) {
            classType = (Class) ((ParameterizedType) type).getRawType();
        } else {
            classType = (Class) type;
        }
        Field[] fields = classType.getDeclaredFields();
        int a=1;
        for (Field field : fields) {
            String name= SqlUtils.firstCapitalization(field.getName());
            if(method.indexOf(name)==-1){
                continue;
            }
            String attribut="["+a+"]";
            a++;
            method=method.replaceAll(name,attribut);
            attributes.put(attribut,name);
        }
        return method;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy