Spring Data

This project offers you an additional abstraction layer for accessing data storage; it has a bunch of interfaces that you need to extend, in order to take advantage of the built-in functionalities offered by Spring Data. When you extend these interfaces, all of the standard operations surrounding data storage will be ready to use.

Spring Data supports technologies such as relational and non-relational databases, map-reduce frameworks, and cloud-based data services. These technologies are supported by modules; if you are interested in the whole list of existing modules, you can visit http://projects.Spring.io/Spring-data/.

Let's play with Spring Data by using an SQL database, such as H2. Suppose that you want to build a Create, Read, Update, Delete (CRUD) operation for a country database table. With this framework, you only need to create the entity class and an empty interface extending the CrudRepository interface provided by Spring Data, as follows:

@Component
public interface CountryRepository extends CrudRepository<Country, Integer> {
}

Since the CrudRepository interface has all of the CRUD operations in it, you won't have to implement anything; you will only have to use its functionality. Let's see this in action, as follows:

@SpringBootApplication
public class SpringDataDemoApplication
{
@Bean
InitializingBean populateDatabase(CountryRepository
countryRepository)
{
return () ->
{
countryRepository.save(new Country(1, "USA"));
countryRepository.save(new Country(2, "Ecuador"));
};
}
@Bean
CommandLineRunner queryDatabase(CountryRepository
countryRepository)
{
return args ->
{
countryRepository.findAll()
.forEach(System.out::println);
};
}
public static void main(String[] args)
{
SpringApplication.run(SpringDataDemoApplication.class,args);
}
}

We have two Beans that use the repository interface created previously. The first method will run, and it will insert two rows into the table. The second method will query all of the rows in the table and then print them in the console. After running this application, you will see the following output in the console when the application starts:

...
Country [id: 1 name: USA ]
Country [id: 2 name: Ecuador ]
...

Spring Data has more features; it also gives you the chance to create queries in a fascinating way. Let's suppose that you want to filter the countries by name. In that case, you will need to add the method to your interface repository, as follows:

@Component
public interface CountryRepository extends CrudRepository<Country, Integer>
{
List<Country> findByName(String name);
}

Then, we can use the preceding method in the following way:

countryRepository.findByName("USA")

There's no implementation at all for this method, which is a great advantage. Spring Data uses the method's name to generate the required implementation, allowing us to forget trivial implementations for these kinds of queries. There are many interfaces that provide more functionalities, such as pagination, sorting, and reactive extensions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170