Spring Data Jpa代码讲解

JpaRepository 继承于 PagingAndSortingRepository 接口, 拥有PagingAndSortingRepository 的全部方法,而JpaSpecificationExecutor 不属于Repository 体系。因为JpaSpecificationExecutor 并不继承repository 接口,因此它不能单独使用,只能和jpa Repository 一块儿用。




什么是Repository? 
2.1 Repository(资源库):经过用来访问领域对象的一个相似集合的接口,在领域与数据映射层之间进行协调。这个叫法就相似于咱们一般所说的DAO,在这里,咱们就按照这一习惯把数据访问层叫Repository 
Spring Data给咱们提供几个Repository,基础的Repository提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系以下: 
Repository: 仅仅是一个标识,代表任何继承它的均为仓库接口类,方便Spring自动扫描识别 
CrudRepository: 继承Repository,实现了一组CRUD相关的方法 
PagingAndSortingRepository: 继承CrudRepository,实现了一组分页排序相关的方法 
JpaRepository: 继承PagingAndSortingRepository,实现一组JPA规范相关的方法 
JpaSpecificationExecutor: 比较特殊,不属于Repository体系,实现一组JPA Criteria查询相关的方法 
咱们本身定义的XxxxRepository须要继承JpaRepository,这样咱们的XxxxRepository接口就具有了通用的数据访问控制层的能力。 



2.2 JpaRepository 所提供的基本功能 
2.2.1 CrudRepository<T, ID extends Serializable>: 
这个接口提供了最基本的对实体类的添删改查操做 
T save(T entity);//保存单个实体 
        Iterable<T> save(Iterable<? extends T> entities);//保存集合 
        T findOne(ID id);//根据id查找实体 
        boolean exists(ID id);//根据id判断实体是否存在 
        Iterable<T> findAll();//查询全部实体,不用或慎用! 
        long count();//查询实体数量 
        void delete(ID id);//根据Id删除实体 
        void delete(T entity);//删除一个实体 
void delete(Iterable<? extends T> entities);//删除一个实体的集合 
        void deleteAll();//删除全部实体,不用或慎用! 
2.2.2 PagingAndSortingRepository<T, ID extends Serializable> 
这个接口提供了分页与排序功能 
Iterable<T> findAll(Sort sort);//排序 
        Page<T> findAll(Pageable pageable);//分页查询(含排序功能) 
2.2.3 JpaRepository<T, ID extends Serializable> 
这个接口提供了JPA的相关功能 
List<T> findAll();//查找全部实体 
        List<T> findAll(Sort sort);//排序 查找全部实体 
        List<T> save(Iterable<? extends T> entities);//保存集合 
        void flush();//执行缓存与数据库同步 
        T saveAndFlush(T entity);//强制执行持久化 
void deleteInBatch(Iterable<T> entities);//删除一个实体集合 
3.Spring data 查询 
3.1 简单条件查询:查询某一个实体类或者集合 
按照Spring data 定义的规则,查询方法以find|read|get开头 
涉及条件查询时,条件的属性用条件关键字链接,要注意的是:条件属性以首字母大写其他字母小写为规定。 
例如:定义一个Entity实体类 
class User{ 
private String firstname; 
private String lastname; 
} 
使用And条件链接时,应这样写: 
findByLastnameAndFirstname(String lastname,String firstname); 
条件的属性名称与个数要与参数的位置与个数一一对应 

3.2 使用JPA NamedQueries (标准规范实现) 
这种查询是标准的JPA规范所定义的,直接声明在Entity实体类上,调用时采用在接口中定义与命名查询对应的method,由Spring Data根据方法名自动完成命名查询的寻找。 
(1)在Entity实体类上使用@NamedQuery注解直接声明命名查询。 
@Entity 
@NamedQuery(name = "User.findByEmailAddress", 
  query = "select u from User u where u.emailAddress = ?1") 
public class User { 

} 
注:定义多个时使用下面的注解 
@NamedQueries(value = { 
                @NamedQuery(name = User.QUERY_FIND_BY_LOGIN, 
                                        query = "select u from User u where u." + User.PROP_LOGIN 
                                                + " = :username"), 
        @NamedQuery(name = "getUsernamePasswordToken", 
                        query = "select new com.aceona.weibo.vo.TokenBO(u.username,u.password) from User u where u." + User.PROP_LOGIN 
                            + " = :username")}) 
(2)在interface中定义与(1)对应的方法 
public interface UserRepository extends JpaRepository<User, Long> { 

  List<User> findByLastname(String lastname); 

  User findByEmailAddress(String emailAddress); 
} 
3.3 使用@Query自定义查询(Spring Data提供的) 
这种查询能够声明在Repository方法中,摆脱像命名查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring data的特有实现。 
例如: 
public interface UserRepository extends JpaRepository<User, Long> { 

  @Query("select u from User u where u.emailAddress = ?1") 
  User findByEmailAddress(String emailAddress); 
} 
3.4 @Query与 @Modifying 执行更新操做 
这两个annotation一块儿声明,可定义个性化更新操做,例如只涉及某些字段更新时最为经常使用,示例以下: 
@Modifying 
@Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
int setFixedFirstnameFor(String firstname, String lastname); 

3.5 索引参数与命名参数 
(1)索引参数以下所示,索引值从1开始,查询中 ”?X” 个数须要与方法定义的参数个数相一致,而且顺序也要一致 
@Modifying 
@Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
int setFixedFirstnameFor(String firstname, String lastname); 

(2)命名参数(推荐使用这种方式) 
能够定义好参数名,赋值时采用@Param("参数名"),而不用管顺序。以下所示: 
public interface UserRepository extends JpaRepository<User, Long> { 

  @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname") 
  User findByLastnameOrFirstname(@Param("lastname") String lastname, 
                                 @Param("firstname") String firstname); 
} 

4. Transactionality(事务) 
4.1 操做单个对象的事务 
Spring Data提供了默认的事务处理方式,即全部的查询均声明为只读事务,对于持久化,更新与删除对象声明为有事务。 
参见org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> 
@org.springframework.stereotype.Repository 
@Transactional(readOnly = true) 
public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>, 
                JpaSpecificationExecutor<T> { 
…… 
@Transactional 
        public void delete(ID id) { 

                delete(findOne(id)); 
        } 
…… 
} 
对于自定义的方法,如需改变spring data提供的事务默认方式,能够在方法上注解@Transactional声明 

4.2 涉及多个Repository的事务处理 
进行多个Repository操做时,也应该使它们在同一个事务中处理,按照分层架构的思想,这部分属于业务逻辑层,所以,须要在Service层实现对多个Repository的调用,并在相应的方法上声明事务。 
例如: 
@Service(“userManagement”) 
class UserManagementImpl implements UserManagement { 

  private final UserRepository userRepository; 
  private final RoleRepository roleRepository; 

  @Autowired 
  public UserManagementImpl(UserRepository userRepository, 
    RoleRepository roleRepository) { 
    this.userRepository = userRepository; 
    this.roleRepository = roleRepository; 
  } 

  @Transactional 
  public void addRoleToAllUsers(String roleName) { 

    Role role = roleRepository.findByName(roleName); 

    for (User user : userRepository.readAll()) { 
      user.addRole(role); 
      userRepository.save(user); 
    } 
} 


@Repository
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
   @Query("select a from User a where a.mobile = :mobile")
   User queryByMobile(@Param("mobile") String mobile);

   @Query("select a from User a where a.mobile = :mobile and a.password=:password")
   List<User> findByMobilePassword(@Param("mobile") String mobile, @Param("password") String password);

   @Query("select a from User a where a.mobile = :mobile and a.cardNo=:cardNo")
   User queryByMobileAndIdentify(@Param("mobile") String mobile, @Param("cardNo") String cardNo);

   @Query("select a from User a where a.id = :id and a.password=:password")
   User queryByIdAndPassword(@Param("id") Long id, @Param("password") String password);

    @Query(value = "select c.id from User c where (:status is null or c.status = :status) " +
            "and (:mobile is null or c.mobile = :mobile) " +
            "and (:name is null or c.name = :name) order by id desc")
    List<Long> findAllByParam(@Param("status")Integer status, @Param("name")String name,
                              @Param("mobile")String mobile);

   @Query(value = "select c from User c where c.company.id = :companyId " +
         "and (:status is null or c.status = :status) " +
         "and (:mobile is null or c.mobile = :mobile) " +
         "and (:name is null or c.name = :name) ")
   Page<User> queryAllByParam(@Param("status")Integer status, @Param("companyId")Long companyId,
                          @Param("mobile")String mobile, @Param("name")String name, Pageable page);

   @Query(value = "select c.id from User c where (:status is null or c.status = :status) " +
         "and (:name is null or c.name = :name) order by id desc")
   List<Long> queryCompanyIdsByParam(@Param("status")Integer status, @Param("name")String name);

   @Query(value = "select a from User a where a.role.id = :roleId ")
   List<User> findAllByRole(@Param("roleId")Long roleId);

   @Query(value = "select a from User a where a.id in :ids ")
   List<User> findAllByIds(@Param("ids")List<Long> ids);

   @Query(value = "select c.id from User c where :name is null or c.name = :name ")
   List<Long> findALLByName(@Param("name")String name);
   
   @Query(value = "select c from User c where  c.company.id = :companyId and c.status=1 ")
   List<User> queryByCompanyId(@Param("companyId")Long companyId);
   
   @Query(value = "select c from User c where  c.company.id = :companyId and c.status in(1,3)")
   List<User> queryByCompanyId2(@Param("companyId")Long companyId);
   
   @Query("select a from User a where a.name = :name and a.password=:password and a.role.id in (1,5,6)")
   List<User> findByNamePassword(@Param("name")String name,  @Param("password")String password);
   
   @Query("select a from User a where a.company.id = :companyId and a.role.id=:roleId and a.status=1")
   List<User> queryByCompanyIdAndRoleId(@Param("companyId")Long companyId,@Param("roleId") Long roleId);
   
   @Query(value = "select a from User a where a.role.id = :roleId and a.status = :status")
   List<User> findAllByRoleAndStatus(@Param("roleId")Long roleId, @Param("status") int status);

   @Query(value = "select a.* from t_user a left join t_company c on c.create_by = a.id where c.id = :companyId and a.role_id=:roleId and a.status=1",nativeQuery = true)
   List<User> findAllByCompanyIdAndRoleId(@Param("companyId")Long companyId,@Param("roleId") Long roleId);

}
相关文章
相关标签/搜索