启用 JpaRepositories
package cn.xiaojf; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean; @SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = RdbCommonRepositoryImpl.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
自定义 repository 接口
package cn.xiaojf.today.data.rdb.repository; import cn.xiaojf.today.base.model.PageInfo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository; import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map; /**
* 自定义 repository 接口
* @author xiaojf 2016/12/8 19:31.
*/
@NoRepositoryBean
public interface RdbCommonRepository<T, ID extends Serializable> extends CrudRepository<T, ID> { }
自定义 repository 接口实现类
package cn.xiaojf.today.data.rdb.repository; import cn.xiaojf.today.base.exception.BusinessException;
import cn.xiaojf.today.base.model.PageInfo;
import cn.xiaojf.today.base.util.BeanToMapUtil;
import cn.xiaojf.today.base.util.CamelCaseUtil;
import cn.xiaojf.today.base.util.SpringBeanUtil;
import cn.xiaojf.today.data.rdb.util.EntityUtil;
import cn.xiaojf.today.data.rdb.util.QueryDynamicLoader;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.util.Assert; import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.*; /**
* 自定义关系数据库repository
* @author xiaojf 2016/12/8 19:35
*/
public class RdbCommonRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T,ID> implements RdbCommonRepository<T,ID> {
private final EntityManager entityManager;
private final JpaEntityInformation entityInformation;
private final Class<T> clazz; public RdbCommonRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
this.entityInformation = entityInformation;
this.clazz = entityInformation.getJavaType();
}
}
在继承了 RdbCommonRepository 接口后, springboot会自动的在相同目录寻找他的实现类,规则是RdbCommonRepository+Impl 类命名规则,也可以手动指定,此处不做多解释,可以自行百度,一般不需要自行指定,使用方式如下
package cn.xiaojf.today.sys.repository; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepository;
import cn.xiaojf.today.sys.entity.SysUser; /**
* 用户 repository
* @author xiaojf 2016/12/8 19:46.
*/
public interface SysUserRepository extends RdbCommonRepository<SysUser,String> {
SysUser findFirstByUsername(String username);
}