[BAEL-1686] - Initial commit
This commit is contained in:
committed by
José Carlos Valero Sánchez
parent
a023f014d0
commit
b3fd211763
@@ -0,0 +1,39 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.controller.repository.AddressRepository;
|
||||
import com.baeldung.controller.repository.PersonRepository;
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.Person;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.UUID;
|
||||
|
||||
@SpringBootApplication @EntityScan("com.baeldung.entity") @EnableJpaRepositories("com.baeldung.controller.repository")
|
||||
public class Application {
|
||||
|
||||
@Autowired private PersonRepository personRepository;
|
||||
@Autowired private AddressRepository addressRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct private void initializeData() {
|
||||
// Create John
|
||||
final Address johnsAddress = new Address(UUID.randomUUID().toString(), "Fake Street 1", "Fake country");
|
||||
addressRepository.save(johnsAddress);
|
||||
final Person john = new Person(UUID.randomUUID().toString(), "John", johnsAddress);
|
||||
personRepository.save(john);
|
||||
|
||||
// Create Lisa
|
||||
final Address lisasAddress = new Address(UUID.randomUUID().toString(), "Real Street 1", "Real country");
|
||||
addressRepository.save(lisasAddress);
|
||||
final Person lisa = new Person(UUID.randomUUID().toString(), "Lisa", lisasAddress);
|
||||
personRepository.save(lisa);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.controller;
|
||||
|
||||
import com.baeldung.controller.repository.PersonRepository;
|
||||
import com.baeldung.entity.Person;
|
||||
import com.querydsl.core.BooleanBuilder;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController public class QueryController {
|
||||
|
||||
@Autowired private PersonRepository personRepository;
|
||||
|
||||
@GetMapping(value = "/personQuery", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Iterable<Person> getFilteredEvents(@QuerydslPredicate(root = Person.class) Predicate predicate) {
|
||||
final BooleanBuilder builder = new BooleanBuilder();
|
||||
return personRepository.findAll(builder.and(predicate));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.controller.repository;
|
||||
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.QAddress;
|
||||
import com.querydsl.core.types.dsl.StringExpression;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBindings;
|
||||
import org.springframework.data.querydsl.binding.SingleValueBinding;
|
||||
|
||||
public interface AddressRepository
|
||||
extends JpaRepository<Address, Long>, QueryDslPredicateExecutor<Address>, QuerydslBinderCustomizer<QAddress> {
|
||||
@Override default void customize(final QuerydslBindings bindings, final QAddress root) {
|
||||
bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::eq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.controller.repository;
|
||||
|
||||
import com.baeldung.entity.Person;
|
||||
import com.baeldung.entity.QPerson;
|
||||
import com.querydsl.core.types.dsl.StringExpression;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBindings;
|
||||
import org.springframework.data.querydsl.binding.SingleValueBinding;
|
||||
|
||||
public interface PersonRepository
|
||||
extends JpaRepository<Person, Long>, QueryDslPredicateExecutor<Person>, QuerydslBinderCustomizer<QPerson> {
|
||||
@Override default void customize(final QuerydslBindings bindings, final QPerson root) {
|
||||
bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::eq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity public class Address {
|
||||
|
||||
@Id private String id;
|
||||
private String address;
|
||||
private String country;
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public Address(String id, String address, String country) {
|
||||
this.id = id;
|
||||
this.address = address;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
|
||||
@Entity public class Person {
|
||||
|
||||
@Id private String id;
|
||||
private String name;
|
||||
@OneToOne @JoinColumn(name = "address") private com.baeldung.entity.Address address;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String id, String name, com.baeldung.entity.Address address) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public com.baeldung.entity.Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(com.baeldung.entity.Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
13
spring-data-rest-querydsl/src/main/resources/application.yml
Normal file
13
spring-data-rest-querydsl/src/main/resources/application.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
hikari:
|
||||
connection-timeout: 60000
|
||||
maximum-pool-size: 5
|
||||
pool-name: hikari-pool
|
||||
url: jdbc:mysql://localhost:3306/baeldung?verifyServerCertificate=false&useSSL=false&requireSSL=false
|
||||
username: root
|
||||
password: admin
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.springdatarestquerydsl;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @WebAppConfiguration
|
||||
public class SpringBootIntegrationTests {
|
||||
|
||||
@Autowired private WebApplicationContext webApplicationContext;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before public void setupMockMvc() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
}
|
||||
|
||||
@Test public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
|
||||
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(),
|
||||
Charset.forName("utf8"));
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/personQuery?name=john")).andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user