Skip to content

Commit e498ae5

Browse files
authored
[DE-878] added tutorial (#322)
* added tutorial
1 parent e9c82bd commit e498ae5

File tree

8 files changed

+431
-0
lines changed

8 files changed

+431
-0
lines changed

.circleci/config.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,27 @@ jobs:
167167
working_directory: integration-tests
168168
- report
169169
- store_cache
170+
171+
tutorial:
172+
parameters:
173+
docker-img:
174+
type: 'string'
175+
default: 'docker.io/arangodb/arangodb:latest'
176+
executor: 'j21'
177+
steps:
178+
- timeout
179+
- checkout
180+
- setup_remote_docker
181+
- start-db:
182+
docker-img: <<parameters.docker-img>>
183+
- load_cache
184+
- mvn-install
185+
- run:
186+
name: Run tutorial
187+
command: mvn spring-boot:run
188+
working_directory: tutorial
189+
- store_cache
190+
170191
deploy:
171192
executor: 'j17'
172193
steps:
@@ -177,6 +198,7 @@ jobs:
177198
- config_gpg
178199
- deploy
179200
- store_cache
201+
180202
release:
181203
executor: 'j17'
182204
steps:
@@ -243,6 +265,9 @@ workflows:
243265
spring-boot-version:
244266
- '3.2.6'
245267
- '3.3.0'
268+
tutorial:
269+
jobs:
270+
- tutorial
246271
deploy:
247272
jobs:
248273
- deploy:

tutorial/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
![ArangoDB-Logo](https://www.arangodb.com/wp-content/uploads/2016/05/ArangoDB_logo_@2.png)
2+
3+
# Spring Data ArangoDB - Tutorial
4+
5+
This is a tutorial on how to configure [Spring Data ArangoDB](https://github.com/arangodb/spring-data), without using
6+
Spring Boot Starter ArangoDB.
7+
A more extensive demo about the features of Spring Data ArangoDB can be found in the
8+
[Spring Boot Starter ArangoDB Demo](https://github.com/arangodb/spring-boot-starter/tree/main/demo).
9+
10+
# Getting Started
11+
12+
## Build a project with Maven
13+
14+
First, we have to set up a project and add every needed dependency.
15+
We use `Maven` and `Spring Boot` for this demo.
16+
17+
We have to create a Maven `pom.xml`:
18+
19+
```xml
20+
<?xml version="1.0" encoding="UTF-8"?>
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<relativePath/>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>3.3.4</version>
31+
</parent>
32+
33+
<groupId>com.arangodb</groupId>
34+
<artifactId>spring-data-arangodb-tutorial</artifactId>
35+
<version>1.0.0</version>
36+
37+
<name>spring-data-arangodb-tutorial</name>
38+
<description>ArangoDB Spring Data Tutorial</description>
39+
40+
<properties>
41+
<java.version>21</java.version>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
</properties>
44+
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.arangodb</groupId>
52+
<artifactId>arangodb-spring-data</artifactId>
53+
<version>4.4.0</version>
54+
</dependency>
55+
</dependencies>
56+
57+
</project>
58+
```
59+
60+
## Entity classes
61+
62+
For this tutorial we will model our entity with a Java record class:
63+
64+
```java
65+
@Document("characters")
66+
public record Character(
67+
@Id
68+
String id,
69+
String name,
70+
String surname
71+
) {
72+
}
73+
```
74+
75+
## Create a repository
76+
77+
Now that we have our data model, we want to store data. For this, we create a repository interface which
78+
extends `ArangoRepository`. This gives us access to CRUD operations, paging, and query by example mechanics.
79+
80+
```java
81+
public interface CharacterRepository extends ArangoRepository<Character, String> {
82+
}
83+
```
84+
85+
## Create a Configuration class
86+
87+
We need a configuration class to set up everything to connect to our ArangoDB instance and to declare that all
88+
needed Spring Beans are processed by the Spring container.
89+
90+
- `@EnableArangoRepositories`: Defines where Spring can find your repositories
91+
- `arango()`: Method to configure the connection to the ArangoDB instance
92+
- `database()`: Method to define the database name
93+
- `returnOriginalEntities()`: Method to configures the behaviour of repository save methods to either return the
94+
original entities (updated where possible) or new ones. Set to `false` to use java records.
95+
96+
```java
97+
@Configuration
98+
@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"})
99+
public class AdbConfig implements ArangoConfiguration {
100+
101+
@Override
102+
public ArangoDB.Builder arango() {
103+
return new ArangoDB.Builder()
104+
.host("localhost", 8529)
105+
.user("root")
106+
.password("test");
107+
}
108+
109+
@Override
110+
public String database() {
111+
return "spring-demo";
112+
}
113+
114+
@Override
115+
public boolean returnOriginalEntities() {
116+
return false;
117+
}
118+
}
119+
```
120+
121+
## Create a CommandLineRunner
122+
123+
To run our demo as command line application, we have to create a class implementing `CommandLineRunner`:
124+
125+
```java
126+
@ComponentScan("com.arangodb.spring.demo")
127+
public class CrudRunner implements CommandLineRunner {
128+
129+
@Autowired
130+
private ArangoOperations operations;
131+
132+
@Autowired
133+
private CharacterRepository repository;
134+
135+
@Override
136+
public void run(String... args) {
137+
// first drop the database so that we can run this multiple times with the same dataset
138+
operations.dropDatabase();
139+
140+
System.out.println("# CRUD operations");
141+
142+
// save a single entity in the database
143+
// there is no need of creating the collection first. This happen automatically
144+
Character nedStark = new Character(null, "Ned", "Stark");
145+
Character saved = repository.save(nedStark);
146+
System.out.println("Ned Stark saved in the database: " + saved);
147+
}
148+
}
149+
```
150+
151+
## Run the applucation
152+
153+
Finally, we create a main class:
154+
155+
```java
156+
@SpringBootApplication
157+
public class DemoApplication {
158+
public static void main(final String... args) {
159+
System.exit(SpringApplication.exit(
160+
SpringApplication.run(CrudRunner.class, args)
161+
));
162+
}
163+
}
164+
```
165+
166+
And run it with:
167+
168+
```shell
169+
mvn spring-boot:run
170+
```
171+
172+
This should produce a console output similar to:
173+
174+
```
175+
Ned Stark saved in the database: Character[id=2029, name=Ned, surname=Stark]
176+
```
177+
178+
# Learn more
179+
180+
* [ArangoDB](https://www.arangodb.com)
181+
* [Spring Data ArangoDB](https://github.com/arangodb/spring-data)
182+
* [ArangoDB Java Driver](https://github.com/arangodb/arangodb-java-driver)
183+
* [Spring Boot Starter ArangoDB Demo](https://github.com/arangodb/spring-boot-starter/tree/main/demo)

tutorial/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<relativePath/>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>3.3.4</version>
11+
</parent>
12+
13+
<groupId>com.arangodb</groupId>
14+
<artifactId>spring-data-arangodb-tutorial</artifactId>
15+
<version>1.0.0</version>
16+
17+
<name>spring-data-arangodb-tutorial</name>
18+
<description>ArangoDB Spring Data Tutorial</description>
19+
20+
<properties>
21+
<java.version>21</java.version>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.arangodb</groupId>
32+
<artifactId>arangodb-spring-data</artifactId>
33+
<version>4.4.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.assertj</groupId>
37+
<artifactId>assertj-core</artifactId>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.arangodb.spring.demo;
2+
3+
import com.arangodb.ArangoDB;
4+
import com.arangodb.springframework.annotation.EnableArangoRepositories;
5+
import com.arangodb.springframework.config.ArangoConfiguration;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"})
10+
public class AdbConfig implements ArangoConfiguration {
11+
12+
@Override
13+
public ArangoDB.Builder arango() {
14+
return new ArangoDB.Builder()
15+
.host("172.28.0.1", 8529)
16+
.user("root")
17+
.password("test");
18+
}
19+
20+
@Override
21+
public String database() {
22+
return "spring-demo";
23+
}
24+
25+
@Override
26+
public boolean returnOriginalEntities() {
27+
return false;
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.spring.demo;
22+
23+
import com.arangodb.spring.demo.runner.CrudRunner;
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
27+
28+
@SpringBootApplication
29+
public class DemoApplication {
30+
public static void main(final String... args) {
31+
System.exit(SpringApplication.exit(
32+
SpringApplication.run(CrudRunner.class, args)
33+
));
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.spring.demo.entity;
22+
23+
import com.arangodb.springframework.annotation.Document;
24+
import org.springframework.data.annotation.Id;
25+
26+
@Document("characters")
27+
public record Character(
28+
@Id
29+
String id,
30+
String name,
31+
String surname
32+
) {
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.spring.demo.repository;
22+
23+
import com.arangodb.spring.demo.entity.Character;
24+
import com.arangodb.springframework.repository.ArangoRepository;
25+
26+
public interface CharacterRepository extends ArangoRepository<Character, String> {
27+
}

0 commit comments

Comments
 (0)