This tutorial assumes that:

  • YugabyteDB is up and running. Download and install YugabyteDB by following the steps in Quick start.
  • Java Development Kit (JDK) 1.8, or later, is installed. JDK installers for Linux and macOS can be downloaded from OpenJDK, AdoptOpenJDK, or Azul Systems.
  • Apache Maven 3.3 or later, is installed.

Create the Spring Data YugabyteDB app using Spring Boot

The Spring Boot project provides the Spring Initializr utility for generating dependencies for Spring Boot applications.

  1. Navigate to Spring Initializr. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose Maven and Java programming language.

  3. Click Dependencies and select Spring Web and PostgreSQL Driver. Click Generate.

  4. Download the resulting ZIP file, which is an archive of a Spring Boot application that is configured with your choices.

  5. Add the following dependencies to pom.xml of the Spring Boot application:


Create the sample Spring Data YugabyteDB application

  1. Create new files,, and in the base package.

    package com.yugabyte.sdyb.sample;
    @Table(value = "employee")
    public class Employee implements Persistable<String> {
    private String id;
    private String name;
    private String email;
    private Boolean isInsert = true;
    // Add Empty Constructor, Constructor, and Getters/Setters
    public Employee() {}
    public Employee(String id, String name, String email) {
      super(); = id; = name; = email;
    public String getId() {
      return id;
    public void setId(String id) { = id;
    public String getName() {
      return name;
    public void setName(String name) { = name;
    public String getEmail() {
      return email;
    public void setEmail(String email) { = email;
    public boolean isNew() {
      return isInsert;
    package com.yugabyte.sdyb.sample;
    import org.springframework.stereotype.Repository;
    public interface EmployeeRepository extends YsqlRepository<Employee, Integer> {
    Employee findByEmail(final String email);
    package com.yugabyte.sdyb.sample;
    import javax.sql.DataSource;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.transaction.TransactionManager;
    @EnableYsqlRepositories(basePackageClasses = EmployeeRepository.class)
    public class YsqlConfig extends AbstractYugabyteJdbcConfiguration {
        NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) {
            return new NamedParameterJdbcTemplate(dataSource);
        TransactionManager transactionManager(DataSource dataSource) {
            return new YugabyteTransactionManager(dataSource);
  2. A number of options can be customized in the properties file located at src/main/resources/ Given YSQL's compatibility with the PostgreSQL language, the spring.jpa.database property is set to POSTGRESQL and the spring.datasource.url is set to the YSQL JDBC URL: jdbc:postgresql://localhost:5433/yugabyte.

    # ---------------------
    # JPA/Hibernate config.
    # The SQL dialect makes Hibernate generate better SQL for the chosen database. = org.hibernate.dialect.PostgreSQLDialect
    # Hibernate ddl auto (create, create-drop, validate, update).
    # -------------------
    # Data-source config.
    # HikariCP config (pool size, default isolation level).
  3. Create a Spring Boot Application runner to perform reads and writes against the YugabyteDB Cluster.

    package com.yugabyte.sdyb.sample;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.jdbc.core.JdbcTemplate;
    public class DemoApplication implements CommandLineRunner {
    JdbcTemplate jdbcTemplate;
    EmployeeRepository customerRepository;
    public static void main(String[] args) {, args);
    public void run(String... args) throws Exception {
      System.out.println("Connected to the YugabyteDB server successfully.");
      jdbcTemplate.execute("DROP TABLE IF EXISTS employee");
      jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS employee" +
                  "  (id text primary key, name varchar, email varchar)");
      System.out.println("Created table employee");
      Employee customer = new Employee("sl1",
                    "User One",
                    "[email protected]");
      Employee customerFromDB = null;
      customerFromDB = customerRepository.findByEmail("[email protected]");
      System.out.println(String.format("Query returned: name = %s, email = %s",
        customerFromDB.getName(), customerFromDB.getEmail()));

Run your new program

$ ./mvnw spring-boot:run

You should see the following output:

2022-04-07 20:25:01.210  INFO 12097 --- [           main] com.yugabyte.sdyb.demo.DemoApplication   : Started DemoApplication in 27.09 seconds (JVM running for 27.35)
Connected to the YugabyteDB server successfully.
Created table employee
Query returned: name = User One, email = [email protected]