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);
}