Skip to content

Commit

Permalink
feat: using jdbc client
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 22, 2023
1 parent eb29875 commit fb0095a
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 148 deletions.
2 changes: 1 addition & 1 deletion aws-lambda-project/.github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v3

- name: Setup Java 17
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: ${{ matrix.distribution }}
Expand Down
40 changes: 24 additions & 16 deletions aws-lambda-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<version>3.2.0-RC2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learning.awslambda</groupId>
Expand All @@ -21,15 +21,14 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>17</java.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
<spring-cloud-aws.version>3.0.3</spring-cloud-aws.version>
<spring-cloud.version>2023.0.0-RC1</spring-cloud.version>
<springdoc-openapi.version>2.2.0</springdoc-openapi.version>
<wrapper.version>1.0.31.RELEASE</wrapper.version>
<aws.java.sdk.version>2.21.28</aws.java.sdk.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
<spotless.version>2.40.0</spotless.version>
<dependency-check-maven.version>8.4.2</dependency-check-maven.version>
<dependency-check-maven.version>9.0.0</dependency-check-maven.version>
<properties-maven-plugin.version>1.2.1</properties-maven-plugin.version>
<sonar-maven-plugin.version>3.10.0.2594</sonar-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
Expand All @@ -43,10 +42,6 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -62,7 +57,7 @@

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
Expand Down Expand Up @@ -121,13 +116,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-dependencies</artifactId>
<version>${spring-cloud-aws.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
Expand Down Expand Up @@ -376,4 +364,24 @@
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
package com.learning.awslambda.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.Objects;
import org.hibernate.Hibernate;

@Entity
@Table(name = "actors")
public class Actor {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@Column(nullable = false)
private String name;

public Actor(Long id, String name) {
Expand Down Expand Up @@ -46,7 +34,6 @@ public void setName(String name) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Actor actor = (Actor) o;
return id != null && Objects.equals(id, actor.id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
package com.learning.awslambda.repositories;

import com.learning.awslambda.entities.Actor;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

public interface ActorRepository extends JpaRepository<Actor, Long> {
Optional<Actor> findByNameLike(String name);
@Repository
public class ActorRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(ActorRepository.class);

private final JdbcClient jdbcClient;
private final NamedParameterJdbcTemplate jdbcTemplate;

public ActorRepository(JdbcClient jdbcClient, NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcClient = jdbcClient;
this.jdbcTemplate = jdbcTemplate;
}

@Transactional(readOnly = true)
public Optional<Actor> findByNameLike(String name) {
String sql = "select id, name from actors where name like :name";
return jdbcClient.sql(sql).param("name", name).query(Actor.class).optional();
}

@Transactional
public void deleteAll() {
String sql = "delete from actors";
int count = jdbcClient.sql(sql).update();
LOGGER.info("deleted {} rows", count);
}

@Transactional
public List<Actor> saveAll(List<Actor> actorList) {

List<Object[]> batch = actorList.stream()
.map(customer -> new Object[] {customer.getName()})
.toList();

String sql = "INSERT INTO actors(name) values :batch RETURNING id, name";

return jdbcTemplate.query(sql, new MapSqlParameterSource("batch", batch), rs -> {
List<Actor> actors = new ArrayList<>();
while (rs.next()) {
actors.add(new Actor(rs.getLong(1), rs.getString(2)));
}
return actors;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.learning.awslambda;

import com.learning.awslambda.common.ContainersConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16.1-alpine:///db"
})
@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate", "spring.test.database.replace=none"})
@Import(ContainersConfig.class)
class SchemaValidationTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
Expand All @@ -16,15 +14,4 @@ public class ContainersConfig {
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.1-alpine"));
}

@Bean
LocalStackContainer localstackContainer(DynamicPropertyRegistry registry) {
LocalStackContainer localStackContainer =
new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.0.0"));
registry.add("spring.cloud.aws.credentials.access-key", localStackContainer::getAccessKey);
registry.add("spring.cloud.aws.credentials.secret-key", localStackContainer::getSecretKey);
registry.add("spring.cloud.aws.region.static", localStackContainer::getRegion);
registry.add("spring.cloud.aws.endpoint", localStackContainer::getEndpoint);
return localStackContainer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ActorControllerIT extends AbstractIntegrationTest {

@BeforeEach
void setUp() {
actorRepository.deleteAllInBatch();
actorRepository.deleteAll();

actorList = new ArrayList<>();
actorList.add(new Actor(null, "First Actor"));
Expand Down

0 comments on commit fb0095a

Please sign in to comment.