自定义Repository的实现方法

一 点睛

Spring Data Commons提供了CrudRepository和PagingAndSortingRepository,相关代码如下:

CrudRepository的定义

//Repository的子接口CrudRepository定义了和CRUD操作相关的内容。
package org.springframework.data.repository;

import java.io.Serializable;


@NoRepositoryBean
public interface CrudRepository extends Repository {
    //关于创建或修改的函数
     S save(S entity);         
     Iterable save(Iterable entities);
    //关于获取的函数
    T findOne(ID id);
    boolean exists(ID id);
    Iterable findAll();
    Iterable findAll(Iterable ids);
    long count();
    //关于删除的函数
    void delete(ID id);
    void delete(T entity);
    void delete(Iterable entities);
    void deleteAll();
}

PagingAndSortingRepository的定义

//定义了分页和排序操作相关的内容
package org.springframework.data.repository;

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;


@NoRepositoryBean
public interface PagingAndSortingRepository extends CrudRepository {
    Iterable findAll(Sort sort);  //排序
    Page findAll(Pageable pageable);  //分页
}

Spring Data JPA也提供了JpaRepsitory,相关代码如下:

//继承PagingAndSortingRepository,额外提供了一些其他的API
package org.springframework.data.jpa.repository;

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

import javax.persistence.EntityManager;

import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;


@NoRepositoryBean
public interface JpaRepository extends PagingAndSortingRepository {
    List findAll();
    List findAll(Sort sort);
    List findAll(Iterable ids);
     List save(Iterable entities);
    void flush();
     S saveAndFlush(S entity);
    void deleteInBatch(Iterable entities);
    void deleteAllInBatch();
    T getOne(ID id);
}

我们是否也可以做类似的封装,定义自己常用的数据库操作呢?当然可以,实现方法如下。

二 实现方法

1 自定义Repository接口

package com.wisely.support;

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean  //指明当前这个接口不是领域类的接口
//我们自定义的CustomRepository继承了JpaRepository接口,具备JPA的能力
public interface CustomRepositoryextends JpaRepository ,JpaSpecificationExecutor{
    //要定义的数据操作方法在接口中定义
    Page findByAuto(T example,Pageable pageable);
}

2 定义接口的实现

package com.wisely.support;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import static com.wisely.specs.CustomerSpecs.*;

//要实现 CustomRepository接口,继承SimpleJpaRepository类让我们可以使用其提供的方法(如findAll)
public class CustomRepositoryImpl 
                    extends SimpleJpaRepository  implements CustomRepository {
    
    private final EntityManager entityManager;//数据操作方法会用到entityManager
    
    //CustomRepositoryImpl的构造函数,需当前处理的领域类和entityManager作为构造参数,在这里也给entityManager赋值了
    public CustomRepositoryImpl(Class domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
        this.entityManager = entityManager;
    }

    @Override
    public Page findByAuto(T example, Pageable pageable) {
        //定义数据访问操作,如调用findAll方法并构造一些查询条件
        return findAll(byAuto(entityManager, example),pageable);
    }


}

3 自定义CustomRepositoryFactoryBean

package com.wisely.support;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
//自定义CustomRepositoryFactoryBean,继承JpaRepositoryFactoryBean
public class CustomRepositoryFactoryBean, S, ID extends Serializable>
        extends JpaRepositoryFactoryBean {

    @Override
    //重写createRepositoryFactory方法,用当前的CustomRepositoryFactory创建实例
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new CustomRepositoryFactory(entityManager);  //该类见下面的定义
    }
     //定义一个私有的静态类,并继承JpaRepositoryFactory
    private static class CustomRepositoryFactory extends JpaRepositoryFactory {

        //构造函数
        public CustomRepositoryFactory(EntityManager entityManager) {
            super(entityManager);
        }

        @Override
        @SuppressWarnings({"unchecked"})
        protected  SimpleJpaRepository getTargetRepository(
                RepositoryInformation information, EntityManager entityManager) {// 获得当前自定义类的实现
            return new CustomRepositoryImpl((Class) information.getDomainType(), entityManager);

        }

        @Override
        protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {// 获得当前自定义类的类型
            return CustomRepositoryImpl.class;
        }
    }
}

 

你可能感兴趣的:(Spring,Boot)