From 88a4663190c49e71608a6fbce181a2d41c96fa5d Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Sun, 9 Feb 2020 20:58:12 -0700 Subject: [PATCH 1/2] BAEL-3737 Calling Stored Procedures from Spring Data JPA Repositories --- spring-boot-modules/spring-boot-jpa/pom.xml | 61 +++++++++++++++++++ .../jpa/SpringBootJpaApplication.java | 13 ++++ .../jpa/controller/CarController.java | 27 ++++++++ .../java/com/baeldung/jpa/entity/Car.java | 34 +++++++++++ .../jpa/repository/CarRepository.java | 22 +++++++ .../com/baeldung/jpa/service/CarService.java | 23 +++++++ .../src/main/resources/application.properties | 17 ++++++ .../src/main/resources/car-mysql.sql | 27 ++++++++ .../jpa/SpringBootJpaApplicationTests.java | 13 ++++ 9 files changed, 237 insertions(+) create mode 100644 spring-boot-modules/spring-boot-jpa/pom.xml create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql create mode 100644 spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java diff --git a/spring-boot-modules/spring-boot-jpa/pom.xml b/spring-boot-modules/spring-boot-jpa/pom.xml new file mode 100644 index 0000000000..c7a49c0fca --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.4.RELEASE + + + com.baeldung + jpa + 0.0.1-SNAPSHOT + spring-boot-jpa + spring-boot-jpa + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java new file mode 100644 index 0000000000..f7253c4b8d --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.jpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootJpaApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootJpaApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java new file mode 100644 index 0000000000..444f13ff8d --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java @@ -0,0 +1,27 @@ +package com.baeldung.jpa.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.jpa.entity.Car; +import com.baeldung.jpa.service.CarService; + +@RestController +public class CarController { + @Autowired + private CarService carService; + + @GetMapping(path = "/modelcount") + public long getTotalCarsByModel(@RequestParam("model") String model) { + return carService.getTotalCarsByModel(model); + } + + @GetMapping(path = "/carsafteryear") + public List findCarsAfterYear(@RequestParam("year") Integer year) { + return carService.findCarsAfterYear(year); + } +} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java new file mode 100644 index 0000000000..3e850e391d --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java @@ -0,0 +1,34 @@ +package com.baeldung.jpa.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Car { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column + private long id; + + @Column + private String model; + + @Column + private Integer year; + + public long getId() { + return id; + } + + public String getModel() { + return model; + } + + public Integer getYear() { + return year; + } + +} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java new file mode 100644 index 0000000000..6b3bf57feb --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java @@ -0,0 +1,22 @@ +package com.baeldung.jpa.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.jpa.repository.query.Procedure; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import com.baeldung.jpa.entity.Car; + +@Repository +public interface CarRepository extends JpaRepository { + + @Procedure(procedureName = "GET_TOTAL_CARS_BY_MODEL") + int getTotalCarsByModel(@Param("model_in") String model); + + @Query(value = "CALL FIND_CARS_AFTER_YEAR(:year_in);", nativeQuery = true) + List findCarsAfterYear(@Param("year_in") Integer year_in); + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java new file mode 100644 index 0000000000..62e9a3b067 --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java @@ -0,0 +1,23 @@ +package com.baeldung.jpa.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.jpa.entity.Car; +import com.baeldung.jpa.repository.CarRepository; + +@Service +public class CarService { + @Autowired + private CarRepository carRepository; + + public int getTotalCarsByModel(String model) { + return carRepository.getTotalCarsByModel(model); + } + + public List findCarsAfterYear(Integer year) { + return carRepository.findCarsAfterYear(year); + } +} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties b/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties new file mode 100644 index 0000000000..ce149df16b --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties @@ -0,0 +1,17 @@ + +#spring.jpa.hibernate.ddl-auto=validate + +#MySql +spring.datasource.url=jdbc:mysql://localhost:3306/baeldung +spring.datasource.username=baeldung +spring.datasource.password=baeldung +#spring.datasource.driver-class-name=com.mysql.jdbc.Driver +#spring.datasource.platform=mysql +#spring.datasource.initialization-mode=always +#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect + + + +#For debugging +#spring.jpa.show-sql = true + diff --git a/spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql b/spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql new file mode 100644 index 0000000000..bb4ab2a86e --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql @@ -0,0 +1,27 @@ +DROP TABLE IF EXISTS car; + +CREATE TABLE car (id int(10) NOT NULL AUTO_INCREMENT, + model varchar(50) NOT NULL, + year int(4) NOT NULL, + PRIMARY KEY (id)); + +INSERT INTO car (model, year) VALUES ('BMW', 2000); +INSERT INTO car (model, year) VALUES ('BENZ', 2010); +INSERT INTO car (model, year) VALUES ('PORCHE', 2005); +INSERT INTO car (model, year) VALUES ('PORCHE', 2004); + +DELIMITER $$ + +DROP PROCEDURE IF EXISTS FIND_CARS_AFTER_YEAR$$ +CREATE PROCEDURE FIND_CARS_AFTER_YEAR(IN year_in INT) +BEGIN + SELECT * FROM car WHERE year >= year_in ORDER BY year; +END$$ + +DROP PROCEDURE IF EXISTS GET_TOTAL_CARS_BY_MODEL$$ +CREATE PROCEDURE GET_TOTAL_CARS_BY_MODEL(IN model_in VARCHAR(50), OUT count_out INT) +BEGIN + SELECT COUNT(*) into count_out from car WHERE model = model_in; +END$$ + +DELIMITER ; diff --git a/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java b/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java new file mode 100644 index 0000000000..d1d6a4bb9f --- /dev/null +++ b/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung.jpa; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringBootJpaApplicationTests { + + @Test + void contextLoads() { + } + +} From c1124dc92c877fa418fb23890eb2326dab5790e5 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Tue, 25 Feb 2020 00:01:04 -0700 Subject: [PATCH 2/2] BAEL-3737 Put more examples on procedure reference and move the project to persistence-modules/spring-data-jpa-4/ --- persistence-modules/spring-data-jpa-4/pom.xml | 10 +++ .../StoredProcedureApplication.java | 6 +- .../controller/CarController.java | 47 ++++++++++++++ .../baeldung/storedprocedure}/entity/Car.java | 9 ++- .../repository/CarRepository.java | 18 +++++- .../storedprocedure/service/CarService.java | 39 ++++++++++++ .../src/main/resources/application.properties | 6 +- .../src/main/resources/car-mysql.sql | 0 spring-boot-modules/spring-boot-jpa/pom.xml | 61 ------------------- .../jpa/controller/CarController.java | 27 -------- .../com/baeldung/jpa/service/CarService.java | 23 ------- .../src/main/resources/application.properties | 17 ------ .../jpa/SpringBootJpaApplicationTests.java | 13 ---- 13 files changed, 127 insertions(+), 149 deletions(-) rename spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java => persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/StoredProcedureApplication.java (56%) create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/controller/CarController.java rename {spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa => persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure}/entity/Car.java (52%) rename {spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa => persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure}/repository/CarRepository.java (52%) create mode 100644 persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/service/CarService.java rename {spring-boot-modules/spring-boot-jpa => persistence-modules/spring-data-jpa-4}/src/main/resources/car-mysql.sql (100%) delete mode 100644 spring-boot-modules/spring-boot-jpa/pom.xml delete mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java delete mode 100644 spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java delete mode 100644 spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties delete mode 100644 spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java diff --git a/persistence-modules/spring-data-jpa-4/pom.xml b/persistence-modules/spring-data-jpa-4/pom.xml index e0b441231e..8a476012c7 100644 --- a/persistence-modules/spring-data-jpa-4/pom.xml +++ b/persistence-modules/spring-data-jpa-4/pom.xml @@ -23,6 +23,16 @@ spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-data-jdbc + + + + mysql + mysql-connector-java + + com.h2database h2 diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/StoredProcedureApplication.java similarity index 56% rename from spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/StoredProcedureApplication.java index f7253c4b8d..5f05764e21 100644 --- a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/SpringBootJpaApplication.java +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/StoredProcedureApplication.java @@ -1,13 +1,13 @@ -package com.baeldung.jpa; +package com.baeldung.storedprocedure; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class SpringBootJpaApplication { +public class StoredProcedureApplication { public static void main(String[] args) { - SpringApplication.run(SpringBootJpaApplication.class, args); + SpringApplication.run(StoredProcedureApplication.class, args); } } diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/controller/CarController.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/controller/CarController.java new file mode 100644 index 0000000000..6aef600d01 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/controller/CarController.java @@ -0,0 +1,47 @@ +package com.baeldung.storedprocedure.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.storedprocedure.entity.Car; +import com.baeldung.storedprocedure.service.CarService; + +@RestController +public class CarController { + @Autowired + private CarService carService; + + @GetMapping(path = "/modelcount") + public long getTotalCarsByModel(@RequestParam("model") String model) { + return carService.getTotalCarsByModel(model); + } + + @GetMapping(path = "/modelcountP") + public long getTotalCarsByModelProcedureName(@RequestParam("model") String model) { + return carService.getTotalCarsByModelProcedureName(model); + } + + @GetMapping(path = "/modelcountV") + public long getTotalCarsByModelVaue(@RequestParam("model") String model) { + return carService.getTotalCarsByModelValue(model); + } + + @GetMapping(path = "/modelcountEx") + public long getTotalCarsByModelExplicit(@RequestParam("model") String model) { + return carService.getTotalCarsByModelExplicit(model); + } + + @GetMapping(path = "/modelcountEn") + public long getTotalCarsByModelEntity(@RequestParam("model") String model) { + return carService.getTotalCarsByModelEntity(model); + } + + @GetMapping(path = "/carsafteryear") + public List findCarsAfterYear(@RequestParam("year") Integer year) { + return carService.findCarsAfterYear(year); + } +} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/entity/Car.java similarity index 52% rename from spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/entity/Car.java index 3e850e391d..2817c25ff7 100644 --- a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/entity/Car.java +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/entity/Car.java @@ -1,12 +1,19 @@ -package com.baeldung.jpa.entity; +package com.baeldung.storedprocedure.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.NamedStoredProcedureQuery; +import javax.persistence.StoredProcedureParameter; +import javax.persistence.ParameterMode; @Entity +@NamedStoredProcedureQuery(name = "Car.getTotalCardsbyModelEntity", procedureName = "GET_TOTAL_CARS_BY_MODEL", parameters = { + @StoredProcedureParameter(mode = ParameterMode.IN, name = "model_in", type = String.class), + @StoredProcedureParameter(mode = ParameterMode.OUT, name = "count_out", type = Integer.class) }) + public class Car { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/repository/CarRepository.java similarity index 52% rename from spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/repository/CarRepository.java index 6b3bf57feb..3d9428628e 100644 --- a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/repository/CarRepository.java +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/repository/CarRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.jpa.repository; +package com.baeldung.storedprocedure.repository; import java.util.List; @@ -8,14 +8,26 @@ import org.springframework.data.jpa.repository.query.Procedure; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import com.baeldung.jpa.entity.Car; +import com.baeldung.storedprocedure.entity.Car; @Repository public interface CarRepository extends JpaRepository { + @Procedure + int GET_TOTAL_CARS_BY_MODEL(String model); + + @Procedure("GET_TOTAL_CARS_BY_MODEL") + int getTotalCarsByModel(String model); + @Procedure(procedureName = "GET_TOTAL_CARS_BY_MODEL") - int getTotalCarsByModel(@Param("model_in") String model); + int getTotalCarsByModelProcedureName(String model); + + @Procedure(value = "GET_TOTAL_CARS_BY_MODEL") + int getTotalCarsByModelValue(String model); + @Procedure(name = "Car.getTotalCardsbyModelEntity") + int getTotalCarsByModelEntiy(@Param("model_in") String model); + @Query(value = "CALL FIND_CARS_AFTER_YEAR(:year_in);", nativeQuery = true) List findCarsAfterYear(@Param("year_in") Integer year_in); diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/service/CarService.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/service/CarService.java new file mode 100644 index 0000000000..104f46e324 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/storedprocedure/service/CarService.java @@ -0,0 +1,39 @@ +package com.baeldung.storedprocedure.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.storedprocedure.entity.Car; +import com.baeldung.storedprocedure.repository.CarRepository; + +@Service +public class CarService { + @Autowired + private CarRepository carRepository; + + public int getTotalCarsByModel(String model) { + return carRepository.getTotalCarsByModel(model); + } + + public int getTotalCarsByModelProcedureName(String model) { + return carRepository.getTotalCarsByModelProcedureName(model); + } + + public int getTotalCarsByModelValue(String model) { + return carRepository.getTotalCarsByModelValue(model); + } + + public int getTotalCarsByModelExplicit(String model) { + return carRepository.GET_TOTAL_CARS_BY_MODEL(model); + } + + public int getTotalCarsByModelEntity(String model) { + return carRepository.getTotalCarsByModelEntiy(model); + } + + public List findCarsAfterYear(Integer year) { + return carRepository.findCarsAfterYear(year); + } +} diff --git a/persistence-modules/spring-data-jpa-4/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-4/src/main/resources/application.properties index 72fc330767..d79c23e0c7 100644 --- a/persistence-modules/spring-data-jpa-4/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa-4/src/main/resources/application.properties @@ -1 +1,5 @@ -spring.jpa.show-sql=true \ No newline at end of file +spring.jpa.show-sql=true +#MySql +spring.datasource.url=jdbc:mysql://localhost:3306/baeldung +spring.datasource.username=baeldung +spring.datasource.password=baeldung \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql b/persistence-modules/spring-data-jpa-4/src/main/resources/car-mysql.sql similarity index 100% rename from spring-boot-modules/spring-boot-jpa/src/main/resources/car-mysql.sql rename to persistence-modules/spring-data-jpa-4/src/main/resources/car-mysql.sql diff --git a/spring-boot-modules/spring-boot-jpa/pom.xml b/spring-boot-modules/spring-boot-jpa/pom.xml deleted file mode 100644 index c7a49c0fca..0000000000 --- a/spring-boot-modules/spring-boot-jpa/pom.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.2.4.RELEASE - - - com.baeldung - jpa - 0.0.1-SNAPSHOT - spring-boot-jpa - spring-boot-jpa - - - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jdbc - - - org.springframework.boot - spring-boot-starter-data-jpa - - - mysql - mysql-connector-java - - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java deleted file mode 100644 index 444f13ff8d..0000000000 --- a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/controller/CarController.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.jpa.controller; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.jpa.entity.Car; -import com.baeldung.jpa.service.CarService; - -@RestController -public class CarController { - @Autowired - private CarService carService; - - @GetMapping(path = "/modelcount") - public long getTotalCarsByModel(@RequestParam("model") String model) { - return carService.getTotalCarsByModel(model); - } - - @GetMapping(path = "/carsafteryear") - public List findCarsAfterYear(@RequestParam("year") Integer year) { - return carService.findCarsAfterYear(year); - } -} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java b/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java deleted file mode 100644 index 62e9a3b067..0000000000 --- a/spring-boot-modules/spring-boot-jpa/src/main/java/com/baeldung/jpa/service/CarService.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.jpa.service; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.jpa.entity.Car; -import com.baeldung.jpa.repository.CarRepository; - -@Service -public class CarService { - @Autowired - private CarRepository carRepository; - - public int getTotalCarsByModel(String model) { - return carRepository.getTotalCarsByModel(model); - } - - public List findCarsAfterYear(Integer year) { - return carRepository.findCarsAfterYear(year); - } -} diff --git a/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties b/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties deleted file mode 100644 index ce149df16b..0000000000 --- a/spring-boot-modules/spring-boot-jpa/src/main/resources/application.properties +++ /dev/null @@ -1,17 +0,0 @@ - -#spring.jpa.hibernate.ddl-auto=validate - -#MySql -spring.datasource.url=jdbc:mysql://localhost:3306/baeldung -spring.datasource.username=baeldung -spring.datasource.password=baeldung -#spring.datasource.driver-class-name=com.mysql.jdbc.Driver -#spring.datasource.platform=mysql -#spring.datasource.initialization-mode=always -#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect - - - -#For debugging -#spring.jpa.show-sql = true - diff --git a/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java b/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java deleted file mode 100644 index d1d6a4bb9f..0000000000 --- a/spring-boot-modules/spring-boot-jpa/src/test/java/com/baeldung/jpa/SpringBootJpaApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.jpa; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SpringBootJpaApplicationTests { - - @Test - void contextLoads() { - } - -}