Vavr Support in Spring Data – Spring Data中的Vavr支持

最后修改: 2017年 7月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to take a look at the support for Vavr in Spring Data – which was added in the 2.0.0 Spring build snapshot.

在这个快速教程中,我们将看看Spring Data中对Vavr的支持–这是在2.0.0 Spring构建快照中添加的。

More specifically, we’re going to show an example of using Vavr Option and Vavr collections as return types of a Spring Data JPA repository.

更具体地说,我们将展示一个使用Vavr OptionVavr集合作为Spring Data JPA资源库的返回类型的例子。

2. Maven Dependencies

2.Maven的依赖性

First, let’s setup a Spring Boot project, since it makes configuring Spring Data much quicker, by adding the spring-boot-parent dependency to the pom.xml:

首先,让我们设置一个Spring Boot项目,因为它使配置Spring Data更快,在pom.xml中添加spring-boot-parent依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath />
</parent>

Evidently, we also need the vavr dependency, as well as a few other dependencies for Spring Data and testing:

显然,我们还需要vavr依赖,以及Spring Data和测试的其他几个依赖。

<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

The latest versions of vavr, spring-boot-starter-data-jpa, spring-boot-starter-test and h2 can be downloaded from Maven Central.

vavrspring-boot-starter-data-jpaspring-boot-starter-testh2的最新版本可以从Maven中央下载。

In this example, we’re only using Spring Boot because it provides Spring Data auto-configuration. If you are working in a non-Boot project, you can add the spring-data-commons dependency with Vavr support directly:

在这个例子中,我们只使用Spring Boot,因为它提供了Spring Data自动配置。如果你在一个非Boot项目中工作,你可以直接添加有Vavr支持的spring-data-commons依赖项。

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

3. Spring Data JPA Repository With Vavr

3.Spring Data JPA Repository With Vavr

Spring Data now contains support for defining repository query methods using Vavr‘s Option and Vavr collections: Seq, Set, and Map as return types.

Spring Data现在包含对使用VavrOptionVavr集合定义资源库查询方法的支持。SeqSetMap作为返回类型。

First, let’s create a simple entity class to manipulate:

首先,让我们创建一个简单的实体类来进行操作。

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;
    
    // standard constructor, getters, setters
}

Next, let’s create the JPA repository by implementing the Repository interface and defining two query methods:

接下来,让我们通过实现Repository接口和定义两个查询方法来创建JPA资源库。

public interface VavrUserRepository extends Repository<User, Long> {

    Option<User> findById(long id);

    Seq<User> findByName(String name);

    User save(User user);
}

Here, we have made use of Vavr Option for a method returning zero or one results, and Vavr Seq for a query method which returns multiple User records.

在这里,我们使用了Vavr Option来表示一个返回零或一个结果的方法,以及Vavr Seq来表示一个返回多个User记录的查询方法。

We also need a main Spring Boot class to auto-configure Spring Data and bootstrap our application:

我们还需要一个主Spring Boot类来自动配置Spring Data并引导我们的应用程序。

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Since we have added the h2 dependency, Spring Boot will auto-configure a DataSource using an in-memory H2 database.

由于我们添加了h2依赖,Spring Boot将使用内存H2数据库自动配置DataSource

4. Testing the JPA Repository

4.测试JPA存储库

Let’s add a JUnit test to verify our repository methods:

让我们添加一个JUnit测试来验证我们的存储库方法。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class VavrRepositoryIntegrationTest {

    @Autowired
    private VavrUserRepository userRepository;

    @Before
    public void setup() {
        User user1 = new User();
        user1.setName("John");
        User user2 = new User();
        user2.setName("John");

        userRepository.save(user1);
        userRepository.save(user2);
    }

    @Test
    public void whenAddUsers_thenGetUsers() {
        Option<User> user = userRepository.findById(1L);
        assertFalse(user.isEmpty());
        assertTrue(user.get().getName().equals("John"));

        Seq<User> users = userRepository.findByName("John");
        assertEquals(2, users.size());
    }
}

In the above test, we first add two user records to the database, then call the repository’s query methods. As you can see, the methods return the correct Vavr objects.

在上面的测试中,我们首先向数据库添加两条用户记录,然后调用资源库的查询方法。你可以看到,这些方法返回正确的Vavr对象。

5. Conclusion

5.结论

In this quick example, we have shown how we can define a Spring Data repository using Vavr types.

在这个快速的例子中,我们展示了如何使用Vavr类型定义Spring Data资源库。

As always, the full source code can be found over on GitHub.

一如既往,完整的源代码可以在GitHub上找到over