BAEL-1524 Split spring-boot-persistence module
This commit is contained in:
parent
3a0d19d9d3
commit
c2637f1509
|
@ -1,3 +1,7 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)
|
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)
|
||||||
|
- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool)
|
||||||
|
- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb)
|
||||||
|
- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases)
|
||||||
|
- More articles: [[<-- prev]](../spring-boot-persistence)
|
|
@ -104,6 +104,23 @@
|
||||||
<artifactId>tomcat-jdbc</artifactId>
|
<artifactId>tomcat-jdbc</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.xerial</groupId>
|
||||||
|
<artifactId>sqlite-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.derby</groupId>
|
||||||
|
<artifactId>derby</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hsqldb</groupId>
|
||||||
|
<artifactId>hsqldb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package com.baeldung.springboothsqldb.application;
|
package com.baeldung.springboothsqldb.application;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,33 +1,32 @@
|
||||||
package com.baeldung.springboothsqldb.application.controllers;
|
package com.baeldung.springboothsqldb.application.controllers;
|
||||||
|
|
||||||
import com.baeldung.springboothsqldb.application.entities.Customer;
|
import com.baeldung.springboothsqldb.application.entities.Customer;
|
||||||
import com.baeldung.springboothsqldb.application.repositories.CustomerRepository;
|
import com.baeldung.springboothsqldb.application.repositories.CustomerRepository;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
@RestController
|
||||||
@RestController
|
public class CustomerController {
|
||||||
public class CustomerController {
|
|
||||||
|
private final CustomerRepository customerRepository;
|
||||||
private final CustomerRepository customerRepository;
|
|
||||||
|
@Autowired
|
||||||
@Autowired
|
public CustomerController(CustomerRepository customerRepository) {
|
||||||
public CustomerController(CustomerRepository customerRepository) {
|
this.customerRepository = customerRepository;
|
||||||
this.customerRepository = customerRepository;
|
}
|
||||||
}
|
|
||||||
|
@PostMapping("/customers")
|
||||||
@PostMapping("/customers")
|
public Customer addCustomer(@RequestBody Customer customer) {
|
||||||
public Customer addCustomer(@RequestBody Customer customer) {
|
customerRepository.save(customer);
|
||||||
customerRepository.save(customer);
|
return customer;
|
||||||
return customer;
|
}
|
||||||
}
|
|
||||||
|
@GetMapping("/customers")
|
||||||
@GetMapping("/customers")
|
public List<Customer> getCustomers() {
|
||||||
public List<Customer> getCustomers() {
|
return (List<Customer>) customerRepository.findAll();
|
||||||
return (List<Customer>) customerRepository.findAll();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
|
@ -1,55 +1,55 @@
|
||||||
package com.baeldung.springboothsqldb.application.entities;
|
package com.baeldung.springboothsqldb.application.entities;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "customers")
|
@Table(name = "customers")
|
||||||
public class Customer {
|
public class Customer {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
public Customer() {}
|
public Customer() {}
|
||||||
|
|
||||||
public Customer(String name, String email) {
|
public Customer(String name, String email) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(long id) {
|
public void setId(long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmail(String email) {
|
public void setEmail(String email) {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
|
return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.springboothsqldb.application.repositories;
|
package com.baeldung.springboothsqldb.application.repositories;
|
||||||
|
|
||||||
import com.baeldung.springboothsqldb.application.entities.Customer;
|
import com.baeldung.springboothsqldb.application.entities.Customer;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
|
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
|
|
@ -0,0 +1,8 @@
|
||||||
|
jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
|
||||||
|
jdbc.url=jdbc:derby:memory:myD;create=true
|
||||||
|
jdbc.user=sa
|
||||||
|
jdbc.pass=
|
||||||
|
|
||||||
|
hibernate.dialect=org.hibernate.dialect.DerbyDialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
|
@ -0,0 +1,8 @@
|
||||||
|
jdbc.driverClassName=org.h2.Driver
|
||||||
|
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||||
|
jdbc.user=sa
|
||||||
|
jdbc.pass=sa
|
||||||
|
|
||||||
|
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
|
@ -0,0 +1,8 @@
|
||||||
|
jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
|
||||||
|
jdbc.url=jdbc:hsqldb:mem:myDb
|
||||||
|
jdbc.user=sa
|
||||||
|
jdbc.pass=
|
||||||
|
|
||||||
|
hibernate.dialect=org.hibernate.dialect.HSQLDialect
|
||||||
|
hibernate.show_sql=true
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
|
@ -0,0 +1,7 @@
|
||||||
|
jdbc.driverClassName=org.sqlite.JDBC
|
||||||
|
jdbc.url=jdbc:sqlite:memory:myDb?cache=shared
|
||||||
|
jdbc.user=sa
|
||||||
|
jdbc.pass=sa
|
||||||
|
hibernate.dialect=com.baeldung.dialect.SQLiteDialect
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
hibernate.show_sql=true
|
|
@ -1,61 +1,61 @@
|
||||||
package com.baeldung.springboothsqldb.application.tests;
|
package com.baeldung.springboothsqldb.application.tests;
|
||||||
|
|
||||||
import com.baeldung.springboothsqldb.application.entities.Customer;
|
import com.baeldung.springboothsqldb.application.entities.Customer;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
public class CustomerControllerUnitTest {
|
public class CustomerControllerUnitTest {
|
||||||
|
|
||||||
private static MediaType MEDIA_TYPE_JSON;
|
private static MediaType MEDIA_TYPE_JSON;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUpJsonMediaType() {
|
public void setUpJsonMediaType() {
|
||||||
MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception {
|
public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception {
|
||||||
Customer customer = new Customer("John", "john@domain.com");
|
Customer customer = new Customer("John", "john@domain.com");
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
|
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
|
||||||
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
|
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
|
||||||
String requestJson = objectWriter.writeValueAsString(customer);
|
String requestJson = objectWriter.writeValueAsString(customer);
|
||||||
|
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(MockMvcRequestBuilders.post("/customers")
|
.perform(MockMvcRequestBuilders.post("/customers")
|
||||||
.contentType(MEDIA_TYPE_JSON)
|
.contentType(MEDIA_TYPE_JSON)
|
||||||
.content(requestJson)
|
.content(requestJson)
|
||||||
)
|
)
|
||||||
|
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk());
|
.andExpect(MockMvcResultMatchers.status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception {
|
public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception {
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(MockMvcRequestBuilders.get("/customers"))
|
.perform(MockMvcRequestBuilders.get("/customers"))
|
||||||
|
|
||||||
.andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON))
|
.andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON))
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk());
|
.andExpect(MockMvcResultMatchers.status().isOk());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,10 +3,8 @@
|
||||||
- [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files)
|
- [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files)
|
||||||
- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source)
|
- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source)
|
||||||
- [Quick Guide on Loading Initial Data with Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql)
|
- [Quick Guide on Loading Initial Data with Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql)
|
||||||
- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool)
|
|
||||||
- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot)
|
|
||||||
- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb)
|
|
||||||
- [Configuring a DataSource Programmatically in Spring Boot](https://www.baeldung.com/spring-boot-configure-data-source-programmatic)
|
- [Configuring a DataSource Programmatically in Spring Boot](https://www.baeldung.com/spring-boot-configure-data-source-programmatic)
|
||||||
- [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source)
|
- [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source)
|
||||||
|
- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot)
|
||||||
- [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate)
|
- [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate)
|
||||||
- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases)
|
- More articles: [[more -->]](../spring-boot-persistence-2)
|
Loading…
Reference in New Issue