Spring Boot中Spring data注解的使用


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的博客

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flydean程序那些事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值