0

I'm looking for information about how to configure Spring Data JDBC in a Spring Boot app (a Gradle example would be ideal).

I've read through the docs, and I know that I need to define a Repository implementation for each domain class (or "aggregate" of domain classes), e.g.

interface UserRepository extends CrudRepository<User, Long> {

  // custom query methods
  long countByLastname(String lastname);
}

But it's not entirely clear what dependencies need to be added, how to inject the repository beans into other beans, how to specify to Spring where the repository beans can be found, etc.

I'd particularly like to see how to define a repository that manages more than one table/domain class. For example a repository that manages persistence of an Order and it's collection of OrderItems. The examples in the docs only show how to map a single domain class to a repository.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • I just got a flashback from a project I did months ago on Data JPA. Since you're using Boot, annotations on custom Repositories are not required. Another nice side of Boot ;) just verify if it's the same in JDBC. But it should be so. – LppEdd Feb 13 '19 at 00:31

2 Answers2

2

Since you're using Spring Boot to develop your application, you can leverage the starter Spring modules. In the specific case the dependency would be

implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'

The module version will be picked up automatically if you configured the Gradle Spring plugin

id 'org.springframework.boot' version '2.1.2.RELEASE' // Your version

When you create custom Repository interfaces, you just need to annotate them with the class annotation @Repository. The Spring-autoconfigured scanning mechanism will pick them up automatically (remember to place them in a sub-package, having the @SpringBootApplication annotated class as root).

You can then @Autowire your Repository in a Service, or whatever you want, using hopefully constructor injection.

@Autowired
MyClass(final MyRepository repository) { ... }

Note that if you have a single constructor, you don't need to specify the @Autowired annotation.

Btw, the Data JDBC is a pretty new project. There are limitations, but I suppose you've found them in the Data JDBC documentation.

Dónal
  • 185,044
  • 174
  • 569
  • 824
LppEdd
  • 20,274
  • 11
  • 84
  • 139
1

what dependencies need to be added

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

how to inject the repository beans into other beans

In your controller or service or any other bean, you can autowire this repository:-

private final UserRepository userRepository;

@Autowired
public UserController(UserRepository userRepository) {
    this.userRepository = userRepository;
}

how to specify to Spring where the repository beans can be found

Spring Boot will automatically scan the package (and sub-packages) where you have your main class annotated with @SpringBootApplication defined.

Also, annotate your interface with @Repository:-

@Repository
interface UserRepository extends CrudRepository<User, Long> {
Dónal
  • 185,044
  • 174
  • 569
  • 824
Kartik
  • 7,677
  • 4
  • 28
  • 50
  • The dependency looks incorrect, I want to use Spring Data JDBC, not JPA – Dónal Feb 12 '19 at 23:45
  • @Dónal ` org.springframework.boot spring-boot-starter-data-jdbc ` – Kartik Feb 12 '19 at 23:47
  • 1
    You do not need to annotate your spring data CrudRepository interface with `@Repository`! Spring data will create the implementation bean for you. – mrkernelpanic Feb 13 '19 at 07:55
  • 1
    Using `@Repository` on Spring Data repositories is superfluous. I also don't consider it a good habit or clean code but noise. – Jens Schauder Feb 13 '19 at 10:22
  • 1
    @JensSchauder well you work at Pivotal, so I'll trust you and take your suggestion as "the" way to go . Thanks – LppEdd Feb 13 '19 at 10:35