Переглянути джерело

Merge branch 'dev' of Amoy2/jkzl-starter2.0 into master

suxiaoyang 6 роки тому
батько
коміт
c870ad2a27

+ 50 - 3
mysql-starter/src/main/java/com/yihu/mysql/query/BaseJpaService.java

@ -5,6 +5,8 @@ import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
@ -160,10 +162,11 @@ public abstract class BaseJpaService<T, R extends CrudRepository> {
        Root<T> root = query.from(getEntityClass());
        List<Predicate> ls = new ArrayList<>();
        for(int i=0; i< fields.length; i++){
            if(values[i].getClass().isArray())
                ls.add(criteriaBuilder.in(root.get(fields[i]).in((Object[])values[i])));
            else
            if (values[i].getClass().isArray()) {
                ls.add(criteriaBuilder.in(root.get(fields[i]).in((Object[]) values[i])));
            } else {
                ls.add(criteriaBuilder.equal(root.get(fields[i]), values[i]));
            }
        }
        query.where(ls.toArray(new Predicate[ls.size()]));
        return entityManager
@ -312,4 +315,48 @@ public abstract class BaseJpaService<T, R extends CrudRepository> {
        }
        return buffer.toString();
    }
    /**
     * 排序条件 第一个字符为 + 表示升序,第一个字符为 - 表示减序,不包含 + 或者 - 默认升序
     * @param sorts
     * @return
     */
    public Sort createSort(String sorts){
        Sort sort = null;
        if (!org.springframework.util.StringUtils.isEmpty(sorts)) {
            // 默认升序
            if(sorts.contains("-")){
                sort = new Sort(Sort.Direction.DESC,sorts.substring(1,sorts.length()).split(","));
            }else if(sorts.contains("+")){
                sort = new Sort(Sort.Direction.ASC,sorts.substring(1,sorts.length()).split(","));
            }else{
                sort = new Sort(Sort.Direction.ASC,sorts.split(","));
            }
        }
        return sort;
    }
    /**
     * 创建分页对象
     * @param page
     * @param size
     * @param sorts
     * @return
     */
    public Pageable createPage(Integer page, Integer size, String sorts){
        PageRequest pageRequest = null;
        if(null == page || page <= 0){
            page = defaultPage;
        }
        if(null == size || size <= 0){
            size = defaultSize;
        }
        if(!org.apache.commons.lang3.StringUtils.isEmpty(sorts)){
            pageRequest = new PageRequest(page,size,createSort(sorts));
        }else{
            pageRequest = new PageRequest(page,size);
        }
        return pageRequest;
    }
}