Call Stored Procedure using Spring StoredProcedure

This tutorial will show you how we can call stored procedure using Spring StoredProcedure. I assume you have a basic idea on stored procedure what it does and what are the advantages of using stored procedure.

I am going to build the project using both maven and gradle build tools.

Related Posts:

Sometimes we need to call a stored procedure while a single SQL statement is unable to fulfil our requirements because, it permits to execute multiple SQL statements and it also allows us to execute these SQL statements in a transactional unit in an efficient manner to avoid any inconsistency sometimes occurs during the execution of multiple SQL statements.

Prerequisites

Eclipse 2019-12, At least JDK 1.8, Maven 3.6.3, Gradle 6.4.1, Spring Boot 2.3.1

Setup Project

You need to create a project in Eclipse. The name of the project is spring-stored-procedure-storedprocedure.

If you are creating gradle based project then use below build.gradle script.

buildscript {
	ext {
		springBootVersion = '2.3.1.RELEASE'
	}
	
    repositories {
    	mavenLocal()
    	mavenCentral()
    }
    
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
	mavenLocal()
    mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
	implementation("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
	implementation('mysql:mysql-connector-java:8.0.17')
	
	//required for jdk 9 or above
	runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359')
}

If you are creating maven based project then use below pom.xml file:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>spring-stored-procedure-storedprocedure</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.17</version>
		</dependency>
		
		<!--required only if jdk 9 or higher version is used-->
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.4.0-b180830.0359</version>
		</dependency>
	</dependencies>

    <build>
        <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>at least 8</source>
					<target>at least 8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Database Configuration

In order to use database we need to configure database with Spring Boot application. We will create src/main/resources/application.properties file with the below configurations.

#datasource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/roytuts
spring.datasource.username=root
spring.datasource.password=root

As we are using standard naming conventions for database configurations, so we don’t need to create any bean of DataSource type and Spring will automatically provide one for us.

Create Model

Model class is a plain old Java object which will represent a row in the table.

package com.roytuts.spring.stored.procedure.storedprocedure.model;

public class Student {

	private Integer studentId;
	private String studentName;
	private String studentDob;
	private String studentEmail;
	private String studentAddress;

	public Student() {
	}

	public Student(Integer studentId, String studentName, String studentDob, String studentEmail,
			String studentAddress) {
		this.studentId = studentId;
		this.studentName = studentName;
		this.studentDob = studentDob;
		this.studentEmail = studentEmail;
		this.studentAddress = studentAddress;
	}

	//getters and setters

}

Mapper Class

We are using Spring JDBC API, so we need a row mapper class that will map each column of the table with the Java field.

The column name you will see later when we will talk about table creation.

package com.roytuts.spring.stored.procedure.storedprocedure.rowmapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.roytuts.spring.stored.procedure.storedprocedure.model.Student;

public class StudentRowMapper implements RowMapper<Student> {

	@Override
	public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
		Student student = new Student();
		student.setStudentId(rs.getInt("student_id"));
		student.setStudentName(rs.getString("student_name"));
		student.setStudentDob(rs.getString("student_dob"));
		student.setStudentEmail(rs.getString("student_email"));
		student.setStudentAddress(rs.getString("student_address"));
		return student;
	}

}

Call Stored Procedures

Next we will call two stored procedures using Spring StoredProcedure API. One stored procedure will fetch single Student details and another stored procedure will fetch all Students details.

The below class is a DAO or Repository class that interacts with the underlying database.

package com.roytuts.spring.stored.procedure.storedprocedure.dao;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.stereotype.Repository;

import com.roytuts.spring.stored.procedure.storedprocedure.model.Student;
import com.roytuts.spring.stored.procedure.storedprocedure.rowmapper.StudentRowMapper;

@Repository
public class StudentDao {

	@Autowired
	private DataSource dataSource;

	private final String DB_NAME = "roytuts";
	private final String PROC_GET_STUDENT = "get_student";
	private final String PROC_GET_ALL_STUDENTS = "get_all_students";

	public Student getStudentById(int studentId) {
		StudentProcedure studentProcedure = new StudentProcedure();
		Student student = studentProcedure.execute(studentId);
		return student;
	}

	private class StudentProcedure extends StoredProcedure {
		public StudentProcedure() {
			super(dataSource, DB_NAME + "." + PROC_GET_STUDENT);
			declareParameter(new SqlParameter("in_student_id", Types.INTEGER));
			declareParameter(new SqlOutParameter("out_student_name", Types.VARCHAR));
			declareParameter(new SqlOutParameter("out_student_dob", Types.VARCHAR));
			declareParameter(new SqlOutParameter("out_student_email", Types.VARCHAR));
			declareParameter(new SqlOutParameter("out_student_address", Types.VARCHAR));
			compile();
		}

		public Student execute(int studentId) {
			Map<String, Object> result = super.execute(studentId);
			// get Student object
			Student student = new Student();
			// simple the studentId
			student.setStudentId(studentId);
			// set Name, must be same as out param in procedure
			student.setStudentName((String) result.get("out_student_name"));
			// set Date of Birth, must be same as out param in procedure
			student.setStudentDob((String) result.get("out_student_dob"));
			// set Email, must be same as out param in procedure
			student.setStudentEmail((String) result.get("out_student_email"));
			// set Address, must be same as out param in procedure
			student.setStudentAddress((String) result.get("out_student_address"));
			return student;
		}
	}

	public List<Student> getAllStudents() {
		StudentProcedure2 procedure2 = new StudentProcedure2();
		Map<String, Object> map = procedure2.execute();
		@SuppressWarnings("unchecked")
		List<Student> students = (List<Student>) map.get("students");
		return students;
	}

	private class StudentProcedure2 extends StoredProcedure {
		public StudentProcedure2() {
			super(dataSource, DB_NAME + "." + PROC_GET_ALL_STUDENTS);
			declareParameter(new SqlReturnResultSet("students", new StudentRowMapper()));
			compile();
		}

		public Map<String, Object> execute() {
			// no IN parameter so passing empty HashMap
			Map<String, Object> results = super.execute(new HashMap<>());
			return results;
		}
	}

}

Create MySQL Table

Create a table called student under roytuts database in MySQL server.

CREATE TABLE `student` (
  `student_id` int unsigned NOT NULL AUTO_INCREMENT,
  `student_name` varchar(30) NOT NULL,
  `student_dob` varchar(10) NOT NULL,
  `student_email` varchar(80) NOT NULL,
  `student_address` varchar(250) NOT NULL,
  PRIMARY KEY (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

To test our application right away we need some data. So we are going to dump some sample data into the above table.

insert  into `student`(`student_id`,`student_name`,`student_dob`,`student_email`,`student_address`) values (1,'Sumit','01-01-1980','[email protected]','Garifa'),
(2,'Gourab','01-01-1982','[email protected]','Garia'),
(3,'Debina','01-01-1982','[email protected]','Salt Lake'),
(4,'Souvik','01-01-1992','[email protected]','Alipore'),
(5,'Liton','01-01-1990','[email protected]','Salt Lake');

Create Stored Procedures

We also need to create stored procedures to fetch student details. We will create two separate stored procedure in the MySQL server under roytuts database.

The below procedure fetches single student record:

DELIMITER $$
CREATE
    PROCEDURE `roytuts`.`get_student`(IN in_student_id INTEGER,
	    OUT out_student_name VARCHAR(30),
	    OUT out_student_dob VARCHAR(10),
	    OUT out_student_email VARCHAR(80),
	    OUT out_student_address VARCHAR(255))
    BEGIN
	SELECT student_name, student_dob, student_email, student_address
	INTO out_student_name, out_student_dob, out_student_email, out_student_address
	FROM student WHERE student_id = in_student_id;
    END$$
DELIMITER ;

You can execute or call the above stored procedure in MySQL server in the following way:

CALL get_student(1,@name,@dob,@email,@address)

Now get the result by executing below SELECT statement:

SELECT @name,@dob,@email,@address

Result:

@name	@dob	    @email	          @address
Sumit	01-01-1980	[email protected]	  Garifa

The below procedure fetches all students from the table:

DELIMITER $$
CREATE
    PROCEDURE `roytuts`.`get_all_students`()
    BEGIN
	SELECT * FROM student;
    END$$
DELIMITER ;

Execute using CALL get_all_students() command.

Output:

spring stored procedure storedprocedure

Spring REST Controller

We would like to expose two endpoints to get the results of the stored procedures.

package com.roytuts.spring.stored.procedure.storedprocedure.rest.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.roytuts.spring.stored.procedure.storedprocedure.dao.StudentDao;
import com.roytuts.spring.stored.procedure.storedprocedure.model.Student;

@RestController
public class StudentRestController {

	@Autowired
	private StudentDao dao;

	@GetMapping("/student/{id}")
	public ResponseEntity<Student> getStudent(@PathVariable Integer id) {
		System.out.println("id: " + id);
		Student student = dao.getStudentById(id);

		return new ResponseEntity<Student>(student, HttpStatus.OK);
	}

	@GetMapping("/students")
	public ResponseEntity<List<Student>> getAllStudents() {
		List<Student> students = dao.getAllStudents();

		return new ResponseEntity<List<Student>>(students, HttpStatus.OK);
	}

}

Main Class

A class with main method and @SpringBootApplication is enough for deploying Spring Boot application into Tomcat server.

package com.roytuts.spring.stored.procedure.storedprocedure;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringStoredProcedureApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringStoredProcedureApp.class, args);
	}

}

Testing the Application

Now when your application is up and running you can hit the following two URLs to get the results of the stored procedures.

URL: http://localhost:8080/student/1

Response:

{"studentId":1,"studentName":"Sumit","studentDob":"01-01-1980","studentEmail":"[email protected]","studentAddress":"Garifa"}

URL: http://localhost:8080/students

Response:

[{"studentId":1,"studentName":"Sumit","studentDob":"01-01-1980","studentEmail":"[email protected]","studentAddress":"Garifa"},{"studentId":2,"studentName":"Gourab","studentDob":"01-01-1982","studentEmail":"[email protected]","studentAddress":"Garia"},{"studentId":3,"studentName":"Debina","studentDob":"01-01-1982","studentEmail":"[email protected]","studentAddress":"Salt Lake"},{"studentId":4,"studentName":"Souvik","studentDob":"01-01-1992","studentEmail":"[email protected]","studentAddress":"Alipore"},{"studentId":5,"studentName":"Liton","studentDob":"01-01-1990","studentEmail":"[email protected]","studentAddress":"Salt Lake"}]

Source Code

Download

Thanks for reading.

Related posts

Leave a Comment