Skip to content

Latest commit

 

History

History
104 lines (82 loc) · 3.86 KB

README-simple.md

File metadata and controls

104 lines (82 loc) · 3.86 KB

📚 Spring Data DynamoDB Examples - 🚀 Simple Repository

This example shows how to use custom methods as part of Spring Data repository interfaces to implement custom logic.

Further explanation can be found

📜 Code sample

The repository interface defines the query method

@EnableScan
public interface UserRepository extends CrudRepository<User, String> {
	List<User> findByLastName(String lastName);
}

for a respective entity class

@DynamoDBTable(tableName = "User")
public class User {

	private String id;
	private String firstName;
	private String lastName;

	public User() {
		// Default constructor is required by AWS DynamoDB SDK
	}

	public User(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	@DynamoDBHashKey
	@DynamoDBAutoGeneratedKey
	public String getId() {
		return id;
	}

	@DynamoDBAttribute
	public String getFirstName() {
		return firstName;
	}

	@DynamoDBAttribute
	public String getLastName() {
		return lastName;
	}
	
	// setters skipped
}

and used

	@Autowired
	private UserRepository repository;

	@Test
	public void sampleTestCase() {
		User gosling = new User("James", "Gosling");
		repository.save(gosling);

		User hoeller = new User("Juergen", "Hoeller");
		repository.save(hoeller);

		List<User> result = repository.findByLastName("Gosling");
		Assert.assertThat(result.size(), is(1));
		Assert.assertThat(result, hasItem(gosling));
		log.info("Found in table: {}", result.get(0));
	}

📝 How to prepare:

  • Update src/main/resources/application.properties
Key Sample value Description
amazon.aws.accesskey N/A AWS accesskey for DynamoDB
amazon.aws.secretkey N/A AWS secretkey for DynamoDB

▶️ How to run:

mvn integration-test

📃 Output should look like:

2018-10-10 00:56:22.055  INFO 3174 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-10-10 00:56:22.561  INFO 3174 --- [           main] o.s.s.d.d.r.s.DynamoDBRepositoryFactory  : Spring Data DynamoDB Version: 5.0.3-SNAPSHOT (2.0)
2018-10-10 00:56:22.562  INFO 3174 --- [           main] o.s.s.d.d.r.s.DynamoDBRepositoryFactory  : Spring Data Version:          2.0.8.RELEASE
2018-10-10 00:56:22.562  INFO 3174 --- [           main] o.s.s.d.d.r.s.DynamoDBRepositoryFactory  : AWS SDK Version:              1.11.301
2018-10-10 00:56:22.562  INFO 3174 --- [           main] o.s.s.d.d.r.s.DynamoDBRepositoryFactory  : Java Version:                 9.0.1 - Java HotSpot(TM) 64-Bit Server VM 9.0.1+11
2018-10-10 00:56:22.562  INFO 3174 --- [           main] o.s.s.d.d.r.s.DynamoDBRepositoryFactory  : Platform Details:             Mac OS X 10.14
2018-10-10 00:56:22.696  INFO 3174 --- [           main] c.g.d.s.simple.UserRepositoryIT          : Started UserRepositoryIT in 1.236 seconds (JVM running for 1.997)
2018-10-10 00:56:23.165  INFO 3174 --- [           main] c.g.d.s.simple.UserRepositoryIT          : Created table User
2018-10-10 00:56:43.223  INFO 3174 --- [           main] c.g.d.s.simple.UserRepositoryIT          : Table User is active
2018-10-10 00:56:43.346  INFO 3174 --- [           main] c.g.d.s.simple.UserRepositoryIT          : Found in table: User [id=7296cf2a-ac68-4a59-9bb4-b1cc3278715a, firstName=James, lastName=Gosling]
2018-10-10 00:56:43.391  INFO 3174 --- [           main] c.g.d.s.simple.UserRepositoryIT          : Deleted table User