committed by
Mark Paluch
parent
d52d73f34c
commit
d172c56dde
47
jdbc/graalvm-native/README.adoc
Normal file
47
jdbc/graalvm-native/README.adoc
Normal file
@@ -0,0 +1,47 @@
|
||||
== Spring Data JDBC - GraalVM native image
|
||||
|
||||
This example compiles a basic Spring Data JDBC appication into a GraalVM native image.
|
||||
|
||||
=== Install GraalVM & native image tooling
|
||||
|
||||
Download and install GraalVM using https://sdkman.io/[SDKMAN!].
|
||||
|
||||
```
|
||||
$> sdk install java <recent version>.r17-grl
|
||||
$> gu install native-image
|
||||
```
|
||||
|
||||
=== Compile to native image
|
||||
|
||||
The maven build uses a dedicated profile `native` to trigger the native image creation.
|
||||
|
||||
```
|
||||
$> maven clean package -P native
|
||||
```
|
||||
|
||||
This will create the native executable in the target folder.
|
||||
|
||||
=== Run the image
|
||||
|
||||
Run the image directly from your console as shown below.
|
||||
This will print results of crud functions invoked via a `CommandLineRunner`.
|
||||
|
||||
```
|
||||
$> ./target/spring-data-jdbc-graalvm-native
|
||||
|
||||
. ____ _ __ _ _
|
||||
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
|
||||
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
|
||||
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
|
||||
' |____| .__|_| |_|_| |_\__, | / / / /
|
||||
=========|_|==============|___/=/_/_/_/
|
||||
:: Spring Boot :: (v3.0.0-SNAPSHOT)
|
||||
|
||||
INFO 82562 --- [ main] e.s.j.g.GraalvmNativeApplication : Starting GraalvmNativeApplication using Java 17.0.4 with PID 82562
|
||||
INFO 82562 --- [ main] e.s.j.g.GraalvmNativeApplication : Started GraalvmNativeApplication in 0.042 seconds (process running for 0.061)
|
||||
insertAuthors(): author1 = Author{name='Josh Long'}
|
||||
insertAuthors(): author2 = Author{name='Martin Kleppmann'}
|
||||
listAllAuthors(): author = Author{name='Josh Long'}
|
||||
Book{title='Reactive Spring'}
|
||||
...
|
||||
```
|
||||
94
jdbc/graalvm-native/pom.xml
Normal file
94
jdbc/graalvm-native/pom.xml
Normal file
@@ -0,0 +1,94 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-jdbc-graalvm-native</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data JDBC - GraalVM native sample</name>
|
||||
<description>Sample project demonstrating Spring Data JDBC features running as GraalVM native image</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
|
||||
<id>native</id>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.graalvm.buildtools</groupId>
|
||||
<artifactId>native-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
|
||||
<metadataRepository>
|
||||
<enabled>true</enabled>
|
||||
<version>0.2.1</version>
|
||||
</metadataRepository>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>native-build</id>
|
||||
<goals>
|
||||
<goal>build</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshot</id>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestone</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-release</id>
|
||||
<url>https://repo.spring.io/release</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.graalvmnative;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import example.springdata.jdbc.graalvmnative.model.Author;
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.repository.ListCrudRepository;
|
||||
|
||||
public interface AuthorRepository extends ListCrudRepository<Author, Long> {
|
||||
|
||||
Optional<Author> findByNameContainingIgnoreCase(String partialName);
|
||||
|
||||
@Query("SELECT * FROM author a WHERE a.name = :name LIMIT 1")
|
||||
Optional<Author> queryFindByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.graalvmnative;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import example.springdata.jdbc.graalvmnative.model.Author;
|
||||
import example.springdata.jdbc.graalvmnative.model.Book;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since 2022/10
|
||||
*/
|
||||
@Component
|
||||
public class CLR implements CommandLineRunner {
|
||||
|
||||
private final AuthorRepository authorRepository;
|
||||
|
||||
CLR(AuthorRepository authorRepository) {
|
||||
this.authorRepository = authorRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
insertAuthors();
|
||||
listAllAuthors();
|
||||
findById();
|
||||
findByPartialName();
|
||||
queryFindByName();
|
||||
deleteAll();
|
||||
}
|
||||
|
||||
private void deleteAll() {
|
||||
this.authorRepository.deleteAll();
|
||||
long count = this.authorRepository.count();
|
||||
System.out.printf("deleteAll(): count = %d%n", count);
|
||||
}
|
||||
|
||||
private void queryFindByName() {
|
||||
Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null);
|
||||
Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null);
|
||||
|
||||
System.out.printf("queryFindByName(): author1 = %s%n", author1);
|
||||
System.out.printf("queryFindByName(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void findByPartialName() {
|
||||
Author author1 = this.authorRepository.findByNameContainingIgnoreCase("sh lo").orElse(null);
|
||||
Author author2 = this.authorRepository.findByNameContainingIgnoreCase("in kl").orElse(null);
|
||||
|
||||
System.out.printf("findByPartialName(): author1 = %s%n", author1);
|
||||
System.out.printf("findByPartialName(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void findById() {
|
||||
Author author1 = this.authorRepository.findById(1L).orElse(null);
|
||||
Author author2 = this.authorRepository.findById(2L).orElse(null);
|
||||
|
||||
System.out.printf("findById(): author1 = %s%n", author1);
|
||||
System.out.printf("findById(): author2 = %s%n", author2);
|
||||
}
|
||||
|
||||
private void listAllAuthors() {
|
||||
List<Author> authors = this.authorRepository.findAll();
|
||||
for (Author author : authors) {
|
||||
System.out.printf("listAllAuthors(): author = %s%n", author);
|
||||
for (Book book : author.getBooks()) {
|
||||
System.out.printf("\t%s%n", book);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void insertAuthors() {
|
||||
Author author1 = this.authorRepository.save(new Author(null, "Josh Long",
|
||||
Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java"))));
|
||||
Author author2 = this.authorRepository.save(
|
||||
new Author(null, "Martin Kleppmann", Set.of(new Book(null, "Designing Data Intensive Applications"))));
|
||||
|
||||
System.out.printf("insertAuthors(): author1 = %s%n", author1);
|
||||
System.out.printf("insertAuthors(): author2 = %s%n", author2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.graalvmnative;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GraalvmNativeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GraalvmNativeApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.graalvmnative.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Author {
|
||||
|
||||
@Id
|
||||
private final Long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<Book> books;
|
||||
|
||||
public Author(Long id, String name, Set<Book> books) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Author author = (Author) o;
|
||||
return Objects.equals(id, author.id) && Objects.equals(name, author.name)
|
||||
&& Objects.equals(books, author.books);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, books);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Author{" + "name='" + name + '\'' + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.graalvmnative.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private final Long id;
|
||||
|
||||
private final String title;
|
||||
|
||||
public Book(Long id, String title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) o;
|
||||
return Objects.equals(id, book.id) && Objects.equals(title, book.title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book{" + "title='" + title + '\'' + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
12
jdbc/graalvm-native/src/main/resources/schema.sql
Normal file
12
jdbc/graalvm-native/src/main/resources/schema.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
create table author
|
||||
(
|
||||
id bigint auto_increment primary key,
|
||||
name varchar not null
|
||||
);
|
||||
|
||||
create table book
|
||||
(
|
||||
id bigint auto_increment primary key,
|
||||
author bigint not null references author (id),
|
||||
title varchar not null
|
||||
);
|
||||
@@ -0,0 +1,13 @@
|
||||
package example.springdata.jdbc.graalvmnative;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class GraalvmNativeApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@
|
||||
<module>jmolecules</module>
|
||||
<module>jooq</module>
|
||||
<!-- <module>mybatis</module> -->
|
||||
<module>graalvm-native</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
Reference in New Issue
Block a user