BaseRepository接口
直接创建BaseRepository来继承JpaRepository接口
package cn.itsource.pss.repository;
import cn.itsource.pss.query.BaseQuery;
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 java.io.Serializable;
import java.util.List;
/**
自定义一个Repository,它是JpaRepository的功能基础上继承增强
在上面添加@NoRepositoryBean标注,这样Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口
@param
@param
*/
@NoRepositoryBean
public interface BaseRepository
//根据Query拿到分页对象(分页)
Page findPageByQuery(BaseQuery baseQuery);
//根据Query拿到对应的所有数据(不分页)
List findByQuery(BaseQuery baseQuery);
//根据jpql与对应的参数拿到数据
List findByJpql(String jpql,Object… values);
}
BaseRepositoryImpl功能实现
定义好自定义的方法后,我们现在通过一个基本的Repository类来实现该方法:
首先添加BaseRepositoryImpl类,继承SimpleJpaRepository类,使其拥有Jpa Repository的基本方法。
我们发现Repository有两个构造函数:
SimpleJpaRepository(JpaEntityInformation entityInformation, EntityManager entityManager)
SimpleJpaRepository(Class domainClass, EntityManager em)
这里我们实现第二个构造函数,拿到domainClass和EntityManager两个对象。
package cn.itsource.pss.repository;
import cn.itsource.pss.query.BaseQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;
/**
实现父类中的三个方法
@param
@param
*/
public class BaseRepositoryImpl
private final EntityManager entityManager;
//必需要实现父类的这个构造器
public BaseRepositoryImpl(Class domainClass, EntityManager em) {
super(domainClass, em);
this.entityManager = em;
}
@Override
public Page findPageByQuery(BaseQuery baseQuery) {
//第一步:拿到所有高级查询条件
Specification spec = baseQuery.createSpecification();
//第二步:拿到排序的值
Sort sort = baseQuery.createSort();
//第三步:根据条件查询分页数据并且返回
Pageable pageable = new PageRequest(baseQuery.getJpaPage(), baseQuery.getPageSize(),sort);
Page page = super.findAll(spec, pageable);
return page;
}
@Override
public List findByQuery(BaseQuery baseQuery) {
//第一步:拿到所有高级查询条件
Specification spec = baseQuery.createSpecification();
//第二步:拿到排序的值
Sort sort = baseQuery.createSort();
//第三步:拿到数据返回
return findAll(spec, sort);
}
@Override
public List findByJpql(String jpql, Object... values) {
//第一步:创建Query对象
Query query = entityManager.createQuery(jpql);
//第二步:把值设置到Query对象中去
if (values!=null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i + 1, values[i]);
}
}
//第三步:返回数据
return query.getResultList();
}
}
创建自定义RepositoryFactoryBean
接下来我们来创建一个自定义的RepositoryFactoryBean来代替默认的RepositoryFactoryBean。
RepositoryFactoryBean负责返回一个RepositoryFactory,Spring Data Jpa 将使用RepositoryFactory来创建Repository具体实现,这里我们用BaseRepositoryImpl代替SimpleJpaRepository作为Repository接口的实现。这样我们就能够达到为所有Repository添加自定义方法的目的。
我们需要覆写创建RepositoryFactory的方法:createRepositoryFactory
package cn.itsource.pss.repository;
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.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
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new MyRepositoryFactory(entityManager); //注:这里创建是我们的自定义类
}
//继承JpaRepositoryFactory后,把返回的对象修改成我们自己的实现
private static class MyRepositoryFactory extends JpaRepositoryFactory{
private final EntityManager entityManager;
/**
* Creates a new {@link JpaRepositoryFactory}.
*
* @param entityManager must not be {@literal null}
*/
public MyRepositoryFactory(EntityManager entityManager) {
super(entityManager);
this.entityManager = entityManager;
}
//这里返回最后的功能对象
@Override
protected Object getTargetRepository(RepositoryInformation information) {
return new BaseRepositoryImpl((Class)information.getDomainType(),entityManager);
}
//确定功能对象的类型
@Override
protected Class> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepositoryImpl.class;
}
}
}
applicationContext.xml 中修改配置
factory-class=“cn.itsource.pss.repository.BaseRepositoryFactoryBean”
/>
自己写的接口继承BaseRepository
public interface EmployeeRepository extends BaseRepository
}
测试扩展功能
//测试分页查询
@Test
public void testFindPageByQuery() {
EmployeeQuery baseQuery = new EmployeeQuery();
baseQuery.setUsername(“1”);
//baseQuery.setAge(20);
//baseQuery.setEmail(“2”);
baseQuery.setOrderByName(“username”);
baseQuery.setOrderByType(“DESC”);
//baseQuery.setCurrentPage(2);
baseQuery.setPageSize(5);
Page page = employeeRepository.findPageByQuery(baseQuery);
for (Employee employee : page) {
System.out.println(employee);
}
}
//测试单独查询
@Test
public void findByQuery() {
EmployeeQuery baseQuery = new EmployeeQuery();
baseQuery.setUsername(“1”);
//baseQuery.setAge(20);
//baseQuery.setEmail(“2”);
baseQuery.setOrderByName(“username”);
baseQuery.setOrderByType(“DESC”);
List emps = employeeRepository.findByQuery(baseQuery);
for (Employee employee : emps) {
System.out.println(employee);
}
}
//测试自定义JPQL
@Test
public void findByJpql() {
List emps = employeeRepository.findByJpql(“select o from Employee o where username = ? and password = ?”,“admin”,“admin”);
for (Employee emp : emps) {
System.out.println(emp);
}
}