BAEL-3737 Put more examples on procedure reference and move the project to persistence-modules/spring-data-jpa-4/
This commit is contained in:
parent
88a4663190
commit
c1124dc92c
@ -23,6 +23,16 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Car> findCarsAfterYear(@RequestParam("year") Integer year) {
|
||||
return carService.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
@ -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)
|
@ -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<Car, Integer> {
|
||||
|
||||
@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<Car> findCarsAfterYear(@Param("year_in") Integer year_in);
|
||||
|
@ -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<Car> findCarsAfterYear(Integer year) {
|
||||
return carRepository.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
@ -1 +1,5 @@
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.show-sql=true
|
||||
#MySql
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
|
||||
spring.datasource.username=baeldung
|
||||
spring.datasource.password=baeldung
|
@ -1,61 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jpa</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-jpa</name>
|
||||
<description>spring-boot-jpa</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -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<Car> findCarsAfterYear(@RequestParam("year") Integer year) {
|
||||
return carService.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
@ -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<Car> findCarsAfterYear(Integer year) {
|
||||
return carRepository.findCarsAfterYear(year);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user