spring data实现自定义的repository实现类,实现跟jpa联通

如果你不想暴露那么多的方法,可以自己订制自己的Repository,还可以在自己的Repository里面添加自己使用的公共方法
当然更灵活的是自己写一个实现类,来实现自己需要的方法
1:写一个与接口同名的类,加上后缀为Impl,这个在前面xml里面配置过,可以自动被扫描到。这个类不需要实现任何接口。
2:在接口中加入自己需要的方法,比如:
public Page<Object[]> getByCondition(UserQueryModel u);
3:在实现类中,去实现这个方法就好了,会被自动找到

java代码:
    @PersistenceContext  
    private EntityManager em;     
    public Page<Object[]> getByCondition(UserQueryModel u){  
String hql = "select o.uuid,o.name from UserModel o where 1=1 and o.uuid=:uuid";  
        Query q = em.createQuery(hql);  
        q.setParameter("uuid", u.getUuid());          
        q.setFirstResult(0);  
        q.setMaxResults(1);       
Page<Object[]> page = new PageImpl<Object[]>(q.getResultList(),new PageRequest(0,1),3);   
        return page;  
}}  
原文地址:https://www.cnblogs.com/toSeeMyDream/p/6194715.html