Java tutorial
/* * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wiiyaya.framework.provider.repository; import com.mysema.query.Tuple; import com.mysema.query.jpa.JPQLQuery; import com.mysema.query.types.EntityPath; import com.mysema.query.types.Predicate; import com.mysema.query.types.path.PathBuilder; import com.wiiyaya.framework.provider.tools.BaseDao; import com.wiiyaya.framework.provider.tools.JF; import com.wiiyaya.framework.provider.tools.JFTuple; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.Querydsl; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.data.querydsl.SimpleEntityPathResolver; import javax.persistence.EntityManager; import java.io.Serializable; import java.util.Collections; import java.util.List; public class BaseDaoImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseDao<T, ID> { private final EntityPath<T> path; private final PathBuilder<T> builder; private final Querydsl querydsl; public BaseDaoImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); this.path = SimpleEntityPathResolver.INSTANCE.createPath(entityInformation.getJavaType()); this.builder = new PathBuilder<T>(path.getType(), path.getMetadata()); this.querydsl = new Querydsl(entityManager, builder); } @Override public Page<T> findAll(Predicate predicate, Pageable pageable) { JPQLQuery countQuery = createWhereQuery(predicate); JPQLQuery query = querydsl.applyPagination(pageable, createWhereQuery(predicate)); Long total = countQuery.count(); List<T> content = total > pageable.getOffset() ? query.list(path) : Collections.<T>emptyList(); return new PageImpl<T>(content, pageable, total); } @Override public List<T> findAll(Predicate predicate) { return createWhereQuery(predicate).list(path); } @Override public Page<T> findAllJF(JF joinFetch, Pageable pageable) { JPQLQuery countQuery = createQuery(); joinFetch.prepareQry(countQuery, false); Long total = countQuery.count(); List<T> content = null; if (total > pageable.getOffset()) { JPQLQuery query = querydsl.applyPagination(pageable, createQuery()); joinFetch.prepareQry(query, true); content = query.list(path); } else { content = Collections.<T>emptyList(); } return new PageImpl<T>(content, pageable, total); } @Override public List<T> findAllJF(JF joinFetch) { JPQLQuery query = createQuery(); joinFetch.prepareQry(query, true); return query.list(path); } @Override public Page<Tuple> findAllJFTuple(JFTuple joinFetch, Pageable pageable) { JPQLQuery countQuery = createQuery(); joinFetch.prepareQry(countQuery, false); Long total = countQuery.count(); List<Tuple> content = null; if (total > pageable.getOffset()) { JPQLQuery query = querydsl.applyPagination(pageable, createQuery()); content = joinFetch.prepareQry(query, true); } else { content = Collections.<Tuple>emptyList(); } return new PageImpl<Tuple>(content, pageable, total); } @Override public List<Tuple> findAllJFTuple(JFTuple joinFetch) { return joinFetch.prepareQry(createQuery(), true); } private JPQLQuery createWhereQuery(Predicate... predicate) { return createQuery().where(predicate); } private JPQLQuery createQuery() { return querydsl.createQuery(path); } }