Skip to content

Commit 1863b56

Browse files
author
Tomáš Kraus
committed
Data se imperative example simplified.
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent c68f163 commit 1863b56

File tree

27 files changed

+527
-1312
lines changed

27 files changed

+527
-1312
lines changed

examples/data/README.md

Lines changed: 0 additions & 154 deletions
This file was deleted.

examples/data/mysql/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Helidon Data SE Imperative Example
2+
----
3+
4+
This example demonstrates a Java SE imperative application that utilizes Helidon Data, WebServer,
5+
and a MySQL database.
6+
7+
There are 2 repository interfaces in the example:
8+
9+
- `PokemonRepository`
10+
- `TypeRepository`
11+
12+
> **NOTE:** Database table `POKEMON` is initialized with ID auto increment to supply primary key values
13+
> by the database. MySQL database default String comparisons are case-insensitive.
14+
15+
## Start the Database
16+
17+
To run the application, a MySQL database is required. You can start the database with the necessary
18+
configuration using the following Docker command:
19+
20+
```shell
21+
docker run --name mysql \
22+
-p 3306:3306 \
23+
-e MYSQL_DATABASE='pets' \
24+
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
25+
-e MYSQL_USER='user' \
26+
-e MYSQL_PASSWORD='changeit' \
27+
-d mysql
28+
```
29+
30+
### Database Schema and Content
31+
32+
The application's Jakarta Persistence API implementation automatically creates the database schema
33+
using the `resources/init.sql` script. The schema consists of two main entities: `Pokemon` and `Type`.
34+
The initialization script populates the database with a basic set of records.
35+
36+
## Build and Run
37+
38+
1. Build the application using Maven:
39+
40+
```shell
41+
mvn package
42+
```
43+
44+
2. Run the application:
45+
46+
```shell
47+
java -jar target/helidon-examples-data-mysql.jar
48+
```
49+
50+
> **NOTE:** The default username and password from this example should never be used in a production environment!
51+
52+
## Test Example
53+
54+
The application provides `http://localhost:8080/pokemon` endpoint.
55+
56+
**List all pokemons:**
57+
```shell
58+
curl http://localhost:8080/pokemon/all
59+
```
60+
61+
**List all normal type pokemons:**
62+
```shell
63+
curl http://localhost:8080/pokemon/type/Normal
64+
```
65+
66+
**Retrieve a pokemon by name (`Meowth`):**
67+
```shell
68+
curl http://localhost:8080/pokemon/get/Meowth
69+
```
70+
71+
**Insert new pokemon:**
72+
```shell
73+
curl -i -X POST -H 'Content-type: application/json' -d '{"name":"Charmander","type":"Fire"}' http://localhost:8080/pokemon
74+
```
75+
76+
**Delete existing pokemon by ID (`20`):**
77+
```shell
78+
curl -i -X DELETE http://localhost:8080/pokemon/20
79+
```

examples/data/mysql/pom.xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 Oracle and/or its affiliates.
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+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>io.helidon.applications</groupId>
25+
<artifactId>helidon-se</artifactId>
26+
<version>4.3.0-SNAPSHOT</version>
27+
<relativePath/>
28+
</parent>
29+
30+
<groupId>io.helidon.examples.data</groupId>
31+
<artifactId>helidon-examples-data-mysql</artifactId>
32+
<version>1.0.0-SNAPSHOT</version>
33+
<name>Helidon Examples Data MySQ</name>
34+
35+
<description>
36+
The example shows how to use Helidon Data with MySQL.
37+
</description>
38+
39+
<properties>
40+
<mainClass>io.helidon.examples.data.mysql.Main</mainClass>
41+
</properties>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>io.helidon.config</groupId>
46+
<artifactId>helidon-config-yaml</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.helidon.data</groupId>
50+
<artifactId>helidon-data</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.helidon.data.jakarta.persistence</groupId>
54+
<artifactId>helidon-data-jakarta-persistence</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.helidon.webserver</groupId>
58+
<artifactId>helidon-webserver</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.helidon.http.media</groupId>
62+
<artifactId>helidon-http-media-jsonb</artifactId>
63+
<scope>runtime</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.helidon.logging</groupId>
67+
<artifactId>helidon-logging-jul</artifactId>
68+
<scope>runtime</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>jakarta.persistence</groupId>
72+
<artifactId>jakarta.persistence-api</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.eclipse.persistence</groupId>
76+
<artifactId>org.eclipse.persistence.jpa</artifactId>
77+
<scope>runtime</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.eclipse.persistence</groupId>
81+
<artifactId>org.eclipse.persistence.core</artifactId>
82+
<scope>runtime</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.mysql</groupId>
86+
<artifactId>mysql-connector-j</artifactId>
87+
<scope>runtime</scope>
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<configuration>
97+
<annotationProcessorPaths>
98+
<path>
99+
<groupId>io.helidon.bundles</groupId>
100+
<artifactId>helidon-bundles-apt</artifactId>
101+
<version>${helidon.version}</version>
102+
</path>
103+
<path>
104+
<groupId>io.helidon.data.jakarta.persistence</groupId>
105+
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
106+
<version>${helidon.version}</version>
107+
</path>
108+
</annotationProcessorPaths>
109+
</configuration>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-dependency-plugin</artifactId>
114+
<executions>
115+
<execution>
116+
<id>copy-libs</id>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
123+
</project>

examples/data/src/main/java/io/helidon/examples/data/service/DmlResult.java renamed to examples/data/mysql/src/main/java/io/helidon/examples/data/mysql/DmlResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.helidon.examples.data.service;
16+
package io.helidon.examples.data.mysql;
1717

1818
/**
1919
* Simple DML database operation result.

examples/data/src/main/java/io/helidon/examples/data/Main.java renamed to examples/data/mysql/src/main/java/io/helidon/examples/data/mysql/Main.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.helidon.examples.data;
16+
package io.helidon.examples.data.mysql;
1717

18-
import io.helidon.examples.data.service.BreedService;
19-
import io.helidon.examples.data.service.OwnerService;
20-
import io.helidon.examples.data.service.PetService;
2118
import io.helidon.logging.common.LogConfig;
2219
import io.helidon.webserver.WebServer;
2320
import io.helidon.webserver.http.HttpRouting;
@@ -59,11 +56,8 @@ public static void main(String... args) {
5956
* @param routing HTTP routing builder to configure routes for this service
6057
*/
6158
private static void routing(HttpRouting.Builder routing) {
62-
63-
// register HTTP services with endpoint prefixes
64-
routing.register("/pet", new PetService())
65-
.register("/owner", new OwnerService())
66-
.register("/breed", new BreedService());
59+
// register HTTP service with endpoint prefix
60+
routing.register("/pokemon", new PokemonService());
6761

6862
}
6963

0 commit comments

Comments
 (0)