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

pro.shuangxi.jpa.service.BaseServiceImpl Maven / Gradle / Ivy

The newest version!
package pro.shuangxi.jpa.service;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import pro.shuangxi.jpa.query.QueryWrapper;
import pro.shuangxi.jpa.repository.BaseRepository;

/**
 * @author :mengshx
 * @date :Created in 2022/9/22 8:36
 * @description:base
 */
abstract public class BaseServiceImpl,K> implements BaseService {
    @Autowired
    protected EntityManager entityManager;
    @Autowired
    protected T baseRepository;
    public K save(K entity) {
        return baseRepository.save(entity);
    }
    public List saveAll(Iterable entitys) {
        return baseRepository.saveAll(entitys);
    }
    public void delete(K entity) {
        baseRepository.delete(entity);
    }
    public void deleteById(Long id) {
        baseRepository.deleteById(id);
    }
    public void deleteByIds(Iterable ids) {
        baseRepository.deleteAllById(ids);
    }
    public 

void deleteByIds(P[] ids) { ArrayList idList = new ArrayList<>(); for (P id : ids) { idList.add(id); } baseRepository.deleteAllById(idList); } public K update(K entity) { return baseRepository.save(entity); } public List updateAll(Iterable entitys) { return baseRepository.saveAll(entitys); } public K saveOrUpdate(K entity) { return baseRepository.save(entity); } public long count() { return baseRepository.count(); } public long count(Specification wrapper) { return baseRepository.count(wrapper); } public List getAll() { return baseRepository.findAll(); } public List getAll(Specification wrapper) { return baseRepository.findAll(wrapper); } public K getById(Long id) { Optional optional = baseRepository.findById(id); if (optional.isPresent()) { return optional.get(); } return null; } public K getOne(Specification wrapper) { List all = baseRepository.findAll(wrapper); if (all.size()>0) { return all.get(0); } return null; } public Page page(Pageable page, Specification wrapper) { return baseRepository.findAll(wrapper,page); } public Page page(Pageable page) { return baseRepository.findAll(new QueryWrapper<>(),page); } public double sum(String column, Specification wrapper) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery query = builder.createTupleQuery(); Root root = query.from(getEntityClass()); query.where(wrapper.toPredicate(root, query, builder)); query.multiselect(builder.sum(root.get(column))); TypedQuery typedQuery = entityManager.createQuery(query); Tuple result = typedQuery.getSingleResult(); if (result.get(0) == null) { return 0; }else { return (double)result.get(0); } } public List groupBy(Specification wrapper,String ...columns) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery query = builder.createTupleQuery(); Root root = query.from(getEntityClass()); query.where(wrapper.toPredicate(root, query, builder)); List> select = new ArrayList<>(); List> groupBy = new ArrayList<>(); for (String column : columns) { Path path = root.get(column); select.add(path); groupBy.add(path); } query.multiselect(select); query.groupBy(groupBy); TypedQuery typedQuery = entityManager.createQuery(query); List resultList = typedQuery.getResultList(); List collect = resultList.stream().map(tuple -> (S) tuple.get(0)).collect(Collectors.toList()); return collect; } public Class getEntityClass(){ Type genericSuperclass = getClass().getGenericSuperclass(); Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments(); Type type = actualTypeArguments[1]; return (Class)type; } }