SpringDataJpa功能扩展源码

SpringDataJpa功能扩展源码

详情可以参考:SpringDataJpa功能扩展

BaseRepository

package wltyx.nyybw.aisell.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import wltyx.nyybw.aisell.query.BaseQuery;

import java.io.Serializable;
import java.util.List;

/**
 *所有Repository类的父类
 * 目前已有功能:
 * 1.根据Query拿到分页对象(分页)
 * 2.根据Query拿到对应的所有数据
 * 3.根据jpql与对应的参数拿到数据
 */
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T,ID>,JpaSpecificationExecutor<T> {
    /**
     * 根据Query拿到分页对象
     */
    public Page findPageByQurey(BaseQuery baseQuery);
    /**
     * 根据Query拿到对应的所有数据
     */
    public List<T> findByQuery(BaseQuery baseQuery);
    /**
     * 根据jpql与对应的参数拿到数据
     */
    public List<T> findByJpql(String jpql,Object...values);
}

BaseRepositoryImpl

package wltyx.nyybw.aisell.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import wltyx.nyybw.aisell.query.BaseQuery;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

public class BaseRepositoryImpl<T,ID extends Serializable> extends SimpleJpaRepository<T,ID> implements BaseRepository<T,ID>{

    private EntityManager em;

    /**
     *调用父类的构造器
     */
    public BaseRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
        this.em=em;
    }
    /**
     * 根据Query拿到分页对象
     */
    @Override
    public Page findPageByQurey(BaseQuery baseQuery) {
        return super.findAll(baseQuery.createSpec(), baseQuery.createPageable(baseQuery.createSort()));
    }
    /**
     * 根据Query拿到对应的所有数据
     */
    @Override
    public List<T> findByQuery(BaseQuery baseQuery) {
        return super.findAll(baseQuery.createSpec());
    }
    /**
     * 根据jpql与对应的参数拿到数据
     */
    @Override
    public List<T> findByJpql(String jpql, Object... values) {
        //第一步:创建Query对象
        Query query = em.createQuery(jpql);
        //第二步:把值设置到Query对象中去
        if (values!=null) {
            for (int i = 0; i < values.length; i++) {
                query.setParameter(i + 1, values[i]);
            }
        }
        //第三步:返回数据
        return query.getResultList();
    }
}

BaseRepositoryFactoryBean

package wltyx.nyybw.aisell.repository;

import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;

public class BaseRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T,S,ID> {

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new MyRepositoryFactory<T,ID>(entityManager); //注:这里创建是我们的自定义类
    }
    //继承JpaRepositoryFactory后,把返回的对象修改成我们自己的实现
    private static  class MyRepositoryFactory<T,ID extends Serializable>   extends JpaRepositoryFactory{

        private final EntityManager entityManager;

        public MyRepositoryFactory(EntityManager entityManager) {
            super(entityManager);
            this.entityManager = entityManager;
        }
        //这里返回最后的功能对象
        @Override
        protected Object getTargetRepository(RepositoryInformation information) {
            return new BaseRepositoryImpl<T,ID>((Class<T>)information.getDomainType(),entityManager);
        }
        //确定功能对象的类型
        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return BaseRepositoryImpl.class;
        }
    }
}

你可能感兴趣的:(SSSDJ框架集成)