ArangoDB与SpringData集成

今天看看 ArangoDB 怎样与 Spring Data 集成使用。

创建工程

首先访问 https://start.spring.io 创建一个 spring-boot 项目。然后添加如下依赖

        
            com.arangodb
            arangodb-spring-data
            3.2.3
        

        
            org.apache.httpcomponents
            httpclient
            4.5.1
        

完整的 pom.xml 文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
         
    
    my
    arangodbspring
    0.0.1-SNAPSHOT
    arangodbspring
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            com.arangodb
            arangodb-spring-data
            3.2.3
        

        
            org.apache.httpcomponents
            httpclient
            4.5.1
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


代码实例

实体类 User.java

package my.arangodbspring;

import com.arangodb.springframework.annotation.Document;
import org.springframework.data.annotation.Id;

@Document("users")
public class User {
    @Id
    private String id;
    private String name;
    private int age;

    public User() {
        super();
    }

    public User(final String name, final int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User: " + this.name;
    }
}

实体对象库类 UserRepository.java

增加了两个方法,一个是按用户名称查找,一个是使用AQL查询。

package my.arangodbspring;

import com.arangodb.springframework.annotation.Query;
import com.arangodb.springframework.repository.ArangoRepository;

public interface UserRepository extends ArangoRepository {
    Iterable findByName(String name);

    @Query("FOR user IN users FILTER user.age > @age SORT user.age DESC RETURN user")
    Iterable getOlderThan(int age);
}

测试类 UserRunner.java

其中包括了使用 ArangoOperations 和 UserRepository 操作集合的方式。

package my.arangodbspring;

import com.arangodb.ArangoCursor;
import com.arangodb.springframework.core.CollectionOperations;
import com.arangodb.util.MapBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.ComponentScan;

import com.arangodb.springframework.core.ArangoOperations;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.Arrays;
import java.util.Map;

@ComponentScan("my.arangodbspring")
public class UserRunner implements CommandLineRunner {
    @Autowired
    private ArangoOperations operations;
    @Autowired
    private UserRepository repository;

    @Override
    public void run(final String... args) throws Exception {
        operations.dropDatabase();

        System.out.println("Test for UserRepository ...");

        for (int i = 0; i < 10; i++) {
            final User user = new User("user_" + i, 20 + i);
            repository.save(user);
        }
        repository.findAll().forEach(System.out::println);

        repository.findAll(Sort.by(Sort.Direction.ASC, "name"))
                .forEach(System.out::println);

        repository.findAll(PageRequest.of(0, 5, Sort.by(Sort.Direction.ASC, "name")))
                .forEach(System.out::println);

        System.out.println(repository.findByName("user_5"));

        System.out.println(repository.getOlderThan(15));

        System.out.println("Test for ArangoOperations ...");

        CollectionOperations collectionOperations = operations.collection("users");
        System.out.println("users count: " + collectionOperations.count());

        Iterable users = operations.findAll(User.class);
        for (User user: users) {
            System.out.println("user: " + user);
        }

        String query = "FOR user IN users" +
                " FILTER user.name == @name || user.age >= @age " +
                " LIMIT 2, 5 " +
                " RETURN user";
        Map params = new MapBuilder()
                .put("name", "user_0")
                .put("age", 25)
                .get();
        ArangoCursor cursor = operations.query(query, params, null, User.class);
        cursor.forEachRemaining(user -> {
            System.out.println("user: " + user);
        });
    }

}

配置类 MyConfiguration.java

配置 ArangoDB 的机器,端口,用户名和密码等,这里是直接在代码里写了,也可以通过配置文件传入。

package my.arangodbspring;

import com.arangodb.ArangoDB;
import com.arangodb.Protocol;
import com.arangodb.springframework.annotation.EnableArangoRepositories;
import com.arangodb.springframework.config.AbstractArangoConfiguration;
import com.arangodb.springframework.config.ArangoConfiguration;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableArangoRepositories(basePackages = {"my.arangodbspring"})
public class MyConfiguration extends AbstractArangoConfiguration  {

    @Override
    public ArangoDB.Builder arango() {
        ArangoDB.Builder arango = new ArangoDB.Builder()
                .useProtocol(Protocol.HTTP_JSON)
                .host("localhost", 8529)
                .user("root")
                .password("");
        return arango;
    }

    @Override
    public String database() {
        return "mydb";
    }

}

应用启动类 ArangodbSpringApplication.java

package my.arangodbspring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ArangodbSpringApplication {

    public static void main(String[] args) {
        final Class[] runner = new Class[] { UserRunner.class};
        System.exit(SpringApplication.exit(SpringApplication.run(runner, args)));
    }

}

你可能感兴趣的:(ArangoDB与SpringData集成)