文章目錄
Spring Boot中Spring data注解的使用
Sring data JPA為我們提供了很多有用的注解,方便我們來實現各種復雜的功能。本文我們將會從Spring Data Annotations和Spring Data JPA Annotations兩部分來講解。
Spring Data Annotations
Spring Data Annotations是指這些注解來自於spring-data-commons包里面的。Spring Data不僅可以用於JPA, 它還有很多其他的數據提供方,JPA只是其中的一個具體實現。
@Transactional
使用@Transactional可以很簡單的將方法配置成為Transactional:
@Transactional
void pay() {}
@Transactional可以放在方法上,也可以放在class上面,如果放在class上面則說明該class中的所有方法都適用於Transactional。
@NoRepositoryBean
有時候我們在創建父Repository的時候,我們不需要為該父Repository創建一個具體的實現, 我們只是想為子Repository提供一個公共的方法而已,這時候,我們就可以在父類上面加入@NoRepositoryBean注解:
@NoRepositoryBean
public interface ParentRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
Optional<T> findById(ID id);
}
子類如下:
@Repository
public interface ChildRepository extends ParentRepository<Person, Long> {
}
@Param
我們可以通過使用@Param從而在Query語句中傳遞參數:
@Query("FROM Person p WHERE p.name = :name")
Person findByName(@Param("name") String name);
@Id
@Id表示Entity的primary key:
class Person {
@Id
Long id;
// ...
}
@Transient
通過使用@Transient, 表明Entity的某個字段是不需要被存儲的。
class Person {
// ...
@Transient
int age;
// ...
}
@CreatedBy, @LastModifiedBy, @CreatedDate, @LastModifiedDate
通過這些注解,我們可以從principals中獲得相應的數據:
public class Person {
// ...
@CreatedBy
User creator;
@LastModifiedBy
User modifier;
@CreatedDate
Date createdAt;
@LastModifiedDate
Date modifiedAt;
// ...
}
因為需要使用到principals,所有這些注解是和Spring Security配合使用的。
Spring Data JPA Annotations
Spring Data JPA Annotations是來自於spring-data-jpa包的。
@Query
通過使用@Query, 我們可以自定義SQL語句:
@Query("SELECT COUNT(*) FROM Person p")
long getPersonCount();
我們也可以傳遞參數:
@Query("FROM Person p WHERE p.name = :name")
Person findByName(@Param("name") String name);
我們還可以使用native SQL查詢:
@Query(value = "SELECT AVG(p.age) FROM person p", nativeQuery = true)
int getAverageAge();
@Procedure
通過@Procedure, 我們可以調用數據庫中的存儲過程:
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(
name = "count_by_name",
procedureName = "person.count_by_name",
parameters = {
@StoredProcedureParameter(
mode = ParameterMode.IN,
name = "name",
type = String.class),
@StoredProcedureParameter(
mode = ParameterMode.OUT,
name = "count",
type = Long.class)
}
)
})
class Person {}
我們可以在Entity上面添加該注解。然后看下怎么調用:
@Procedure(name = "count_by_name")
long getCountByName(@Param("name") String name);
@Lock
通過使用@Lock,我們可以選擇數據庫的隔離方式:
@Lock(LockModeType.NONE)
@Query("SELECT COUNT(*) FROM Person p")
long getPersonCount();
Lock的值可以有如下幾種:
- READ
- WRITE
- OPTIMISTIC
- OPTIMISTIC_FORCE_INCREMENT
- PESSIMISTIC_READ
- PESSIMISTIC_WRITE
- PESSIMISTIC_FORCE_INCREMENT
- NONE
@Modifying
@Modifying表示我們有修改數據庫的操作:
@Modifying
@Query("UPDATE Person p SET p.name = :name WHERE p.id = :id")
void changeName(@Param("id") long id, @Param("name") String name);
@EnableJpaRepositories
通過使用@EnableJpaRepositories,我們來配置Japa Repository的相關信息:
@Configuration
@EnableJpaRepositories(basePackages = "com.flydean.repository")
public class PersistenceConfig {
}
默認情況下,我們會在@Configuration類的子類中查找repositories,通過使用basePackages, 我們可以指定其他的目錄。
本文的例子可以參考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-jpa
更多教程請參考 flydean的博客