Skip to content

Commit 2cc4009

Browse files
committed
my-first-spring-app
Based on Getting Started - Accessing Oracle NoSQL Database from Spring Data Applications
1 parent a3aa573 commit 2cc4009

File tree

8 files changed

+252
-0
lines changed

8 files changed

+252
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# my-first-spring-app
2+
3+
## Preparation
4+
5+
Clone this repository
6+
7+
## Setup dependencies
8+
9+
see [pom.xml](./pom.xml)
10+
11+
12+
## Build and Run the application
13+
**Note**: default values are for on-premise and `http://localhost:8080`. See application.properties
14+
These can be modified by setting the following env variables
15+
16+
export NOSQL_ENDPOINT=$HOSTNAME
17+
export NOSQL_PORT=8080
18+
19+
20+
Running the App code:
21+
```shell
22+
mvn compile exec:java -Dexec.mainClass="org.example.app.App"
23+
```
24+
25+
## Deploy KVLite container image and run the application
26+
27+
1. Start up KVLite in a container
28+
29+
pull the image directly from the GitHub Container Registry:
30+
31+
```shell
32+
docker pull ghcr.io/oracle/nosql:latest-ce
33+
docker tag ghcr.io/oracle/nosql:latest-ce oracle/nosql:ce
34+
```
35+
36+
Start up KVLite in a container. You must give it a name and provide a hostname. Startup of
37+
KVLite is the default `CMD` of the image:
38+
39+
```shell
40+
docker run -d --name=kvlite --hostname=kvlite --env KV_PROXY_PORT=8080 -p 8080:8080 oracle/nosql:ce
41+
```
42+
43+
see instuction https://github.com/oracle/docker-images/tree/main/NoSQL
44+
45+
2. Build and Run the application as shown above
46+
47+
## Learn more
48+
49+
* [Getting Started - Accessing Oracle NoSQL Database from Spring Data Applications](https://blogs.oracle.com/nosql/post/getting-started-accessing-oracle-nosql-database-from-spring-data-applications)
50+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.example.app</groupId>
5+
<artifactId>my-app</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>my-app</name>
9+
<url>http://maven.apache.org</url>
10+
11+
12+
<properties>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>3.8.1</version>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
<version>2.7.0</version>
31+
</dependency>
32+
33+
<!-- https://mvnrepository.com/artifact/com.oracle.nosql.sdk/spring-data-oracle-nosql -->
34+
<dependency>
35+
<groupId>com.oracle.nosql.sdk</groupId>
36+
<artifactId>spring-data-oracle-nosql</artifactId>
37+
<version>1.4.1</version>
38+
</dependency>
39+
40+
41+
</dependencies>
42+
43+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.example.app;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ConfigurableApplicationContext;
8+
9+
@SpringBootApplication
10+
public class App implements CommandLineRunner
11+
{
12+
@Autowired
13+
private CustomerRepository repo;
14+
15+
public static void main( String[] args )
16+
{
17+
ConfigurableApplicationContext
18+
ctx = SpringApplication.run(App.class, args);
19+
SpringApplication.exit(ctx, () -> 0);
20+
ctx.close();
21+
System.exit(0);
22+
}
23+
24+
@Override
25+
public void run(String... args) throws Exception {
26+
27+
repo.deleteAll();
28+
29+
Customer s1 = new Customer();
30+
s1.firstName = "John";
31+
s1.lastName = "Doe";
32+
33+
repo.save(s1);
34+
System.out.println("\nsaved: " + s1); // customerId contains generated value
35+
36+
Customer s2 = new Customer();
37+
s2.firstName = "John";
38+
s2.lastName = "Smith";
39+
40+
repo.save(s2);
41+
System.out.println("\nsaved: " + s2); // customerId contains generated value
42+
43+
System.out.println("\nfindAll:");
44+
Iterable<Customer> customers = repo.findAll();
45+
46+
for (Customer s : customers) {
47+
System.out.println(" Customer: " + s);
48+
}
49+
50+
System.out.println("\nfindByLastName: Smith");
51+
customers = repo.findByLastName("Smith");
52+
53+
for (Customer s : customers) {
54+
System.out.println(" Customer: " + s);
55+
}
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.app;
2+
3+
import com.oracle.nosql.spring.data.config.AbstractNosqlConfiguration;
4+
import com.oracle.nosql.spring.data.config.NosqlDbConfig;
5+
import com.oracle.nosql.spring.data.repository.config.EnableNosqlRepositories;
6+
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.beans.factory.annotation.Value;
10+
11+
12+
import oracle.nosql.driver.kv.StoreAccessTokenProvider;
13+
14+
@Configuration
15+
@EnableNosqlRepositories
16+
public class AppConfig extends AbstractNosqlConfiguration {
17+
18+
@Value("${nosql.endpoint}")
19+
private String NOSQL_ENDPOINT;
20+
@Value("${nosql.port}")
21+
private String NOSQL_PORT;
22+
23+
@Bean
24+
public NosqlDbConfig nosqlDbConfig() {
25+
return new NosqlDbConfig(
26+
"http://" + NOSQL_ENDPOINT + ":" + NOSQL_PORT, // endpoint URL
27+
new StoreAccessTokenProvider()); // AuthorizationProvider
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.app;
2+
3+
import com.oracle.nosql.spring.data.core.mapping.NosqlId;
4+
5+
public class Customer {
6+
@NosqlId(generated = true)
7+
long customerId;
8+
String firstName;
9+
String lastName;
10+
11+
@Override
12+
public String toString() {
13+
return "Customer{" +
14+
"customerId=" + customerId +
15+
", firstName='" + firstName + '\'' +
16+
", lastName='" + lastName + '\'' +
17+
'}';
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.app;
2+
3+
import com.oracle.nosql.spring.data.repository.NosqlRepository;
4+
5+
public interface CustomerRepository
6+
extends NosqlRepository<Customer, Long>
7+
{
8+
Iterable<Customer> findByLastName(String lastname);
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
payroll.ociregion=${OCI_REGION:us-ashburn-1}
2+
payroll.ocid_comp=${OCI_NOSQL_COMPID:ocid1.compartment.oc1..aaaaaaaa4mlehopmvdluv2wjcdp4tnh2ypjz3nhhpahb4ss7yvxaa3be3diq}
3+
4+
nosql.endpoint=${NOSQL_ENDPOINT:localhost}
5+
nosql.port=${NOSQL_PORT:8080}
6+
7+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.example.app;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)