Skip to content

Commit dcd3079

Browse files
Merge pull request #1935 from oracle-devrel/witold-swierzy-patch-10
Witold swierzy patch 10
2 parents 7e45d8a + a550fda commit dcd3079

File tree

9 files changed

+252
-0
lines changed

9 files changed

+252
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This repo contains some simple Sprong-Boot Java program demonstrating using Oracle API for MongoDB
2+
3+
# License
4+
5+
Copyright (c) 2025 Oracle and/or its affiliates.
6+
7+
Licensed under the Universal Permissive License (UPL), Version 1.0.
8+
9+
See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.oracle</groupId>
8+
<artifactId>SpringBootDemo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
21+
<version>3.5.4</version>
22+
<exclusions>
23+
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
24+
<exclusion>
25+
<groupId>ch.qos.logback</groupId>
26+
<artifactId>logback-classic</artifactId>
27+
</exclusion>
28+
</exclusions>
29+
</dependency>
30+
31+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<version>3.5.4</version>
36+
<scope>test</scope>
37+
<exclusions>
38+
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
39+
<exclusion>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-testcontainers -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-testcontainers</artifactId>
49+
<version>3.5.4</version>
50+
<scope>test</scope>
51+
<exclusions>
52+
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
53+
<exclusion>
54+
<groupId>ch.qos.logback</groupId>
55+
<artifactId>logback-classic</artifactId>
56+
</exclusion>
57+
</exclusions>
58+
</dependency>
59+
<!-- https://mvnrepository.com/artifact/org.testcontainers/testcontainers -->
60+
<dependency>
61+
<groupId>org.testcontainers</groupId>
62+
<artifactId>testcontainers</artifactId>
63+
<version>1.21.3</version>
64+
<scope>test</scope>
65+
<exclusions>
66+
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
67+
<exclusion>
68+
<groupId>ch.qos.logback</groupId>
69+
<artifactId>logback-classic</artifactId>
70+
</exclusion>
71+
</exclusions>
72+
</dependency>
73+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
74+
<dependency>
75+
<groupId>org.junit.jupiter</groupId>
76+
<artifactId>junit-jupiter-api</artifactId>
77+
<version>5.13.4</version>
78+
<scope>test</scope>
79+
<exclusions>
80+
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
81+
<exclusion>
82+
<groupId>ch.qos.logback</groupId>
83+
<artifactId>logback-classic</artifactId>
84+
</exclusion>
85+
</exclusions>
86+
</dependency>
87+
</dependencies>
88+
<exclusions>
89+
<exclusion>
90+
<groupId>org.slf4j</groupId>
91+
<artifactId>slf4j-simple</artifactId>
92+
</exclusion>
93+
</exclusions>
94+
</project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aaa
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
package com.oracle;
3+
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
import org.springframework.data.mongodb.core.mapping.Field;
7+
8+
@Document(collection = "colors")
9+
public class Color {
10+
@Id
11+
private String objectId;
12+
13+
@Field(name = "name")
14+
private String name;
15+
16+
public Color() {}
17+
18+
public Color(String id, String name) {
19+
this.objectId = id;
20+
this.name = name;
21+
}
22+
23+
public void setObjectId(String id) {
24+
this.objectId = id;
25+
}
26+
27+
public String getObjectId() {
28+
return this.objectId;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public String toString() {
40+
return this.objectId+" : "+this.name;
41+
}
42+
43+
@Override
44+
public final boolean equals(Object o) {
45+
if (!(o instanceof Color color)) return false;
46+
47+
return getObjectId().equals(color.getObjectId());
48+
}
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.oracle;
2+
3+
import org.springframework.data.mongodb.repository.MongoRepository;
4+
import org.springframework.data.mongodb.repository.Query;
5+
6+
public interface ColorRepository extends MongoRepository<Color,String> {
7+
@Query
8+
Color findByName(String Name);
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.oracle;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
6+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
7+
@SpringBootApplication
8+
public class Main {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Main.class,args);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring:
2+
data:
3+
mongodb:
4+
uri: ${DB_URI}
5+
database: ${DB_NAME}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
package com.oracle;
3+
4+
import com.mongodb.client.MongoCollection;
5+
import com.mongodb.client.model.Filters;
6+
import com.mongodb.client.model.ReplaceOptions;
7+
import org.bson.Document;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.data.mongodb.core.MongoTemplate;
12+
import org.bson.conversions.Bson;
13+
14+
@SpringBootTest
15+
public class MongoTest {
16+
@Autowired
17+
ColorRepository colorRepository;
18+
19+
@Autowired
20+
MongoTemplate mt;
21+
22+
Color blue = new Color("123","blue"),
23+
grey = new Color("LU0010001-2000001891770-CAMT053-42","grey"),
24+
green = new Color("001","green"),
25+
yellow = new Color("002","yellow"),
26+
red = new Color("003", "red");
27+
28+
@Test
29+
void insertTest() {
30+
try {
31+
System.out.println("Save test");
32+
33+
System.out.println(colorRepository.save(blue));
34+
System.out.println(colorRepository.save(grey));
35+
System.out.println(colorRepository.save(green));
36+
System.out.println(colorRepository.save(yellow));
37+
System.out.println(colorRepository.save(red));
38+
39+
System.out.println("Save test done.");
40+
}
41+
catch(Exception e) {e.printStackTrace();}
42+
}
43+
44+
@Test
45+
void replaceTest() {
46+
try {
47+
System.out.println("Replace test");
48+
49+
MongoCollection<Document> col = mt.getCollection("colors");
50+
Bson filter = Filters.eq("_id", "LU0010001-2000001891770-CAMT053-42");
51+
ReplaceOptions replace = new ReplaceOptions().upsert(true);
52+
Document d = new Document("_id", "LU0010001-2000001891770-CAMT053-42");
53+
d.append("name", "white");
54+
col.replaceOne(filter, d, replace);
55+
56+
System.out.println("End of reeplace test.");
57+
}
58+
catch (Exception e) {e.printStackTrace();}
59+
}
60+
61+
@Test
62+
void findTest() {
63+
try {
64+
System.out.println("Find test");
65+
Color c = colorRepository.findByName("red");
66+
System.out.println(c);
67+
System.out.println("End of find test");
68+
}
69+
catch (Exception e) {e.printStackTrace();}
70+
}
71+
}

0 commit comments

Comments
 (0)