JdbcTemplate: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 46: Line 46:
     }
     }


    //
     // Reading data ...
     // Reading data
    //


     @Override
     // Writing data ...
    public Ingredient findOne(String id) {
}
</syntaxhighlight>


        return jdbcTemplate.queryForObject(
==Reading Data==
                "SELECT id, name, type from INGREDIENT WHERE id=?",
                this::mapRowToIngredient, id);
    }


    @Override
<syntaxhighlight lang='java'>
    public Iterable<Ingredient> findAll() {
@Override
public Ingredient findOne(String id) {


        return jdbcTemplate.query(
    return jdbcTemplate.queryForObject(
                "SELECT id, name, type from INGREDIENT",
            "SELECT id, name, type from INGREDIENT WHERE id=?",
                this::mapRowToIngredient);
            this::mapRowToIngredient, id);
    }
}


     //
@Override
    // Writing data
public Iterable<Ingredient> findAll() {
    //
 
     return jdbcTemplate.query(
            "SELECT id, name, type from INGREDIENT",
            this::mapRowToIngredient);
}


    @Override
private Ingredient mapRowToIngredient(ResultSet rs, int rowNumber) throws SQLException {
    public Ingredient save(Ingredient ingredient) {


        jdbcTemplate.update(
    return new Ingredient(
                "INSERT INTO INGREDIENT (id, name, type) VALUES (?, ?, ?)",
            rs.getString("id"),
                ingredient.getId(),
            rs.getString("name"),
                ingredient.getName(),
            Ingredient.Type.valueOf(rs.getString("type")));
                ingredient.getType().toString());
}
</syntaxhighlight>


        return ingredient;
==Writing Data==
    }


    private Ingredient mapRowToIngredient(ResultSet rs, int rowNumber) throws SQLException {
<syntaxhighlight lang='java'>
@Override
public Ingredient save(Ingredient ingredient) {


        return new Ingredient(
    jdbcTemplate.update(
                rs.getString("id"),
            "INSERT INTO INGREDIENT (id, name, type) VALUES (?, ?, ?)",
                rs.getString("name"),
            ingredient.getId(),
                Ingredient.Type.valueOf(rs.getString("type")));
            ingredient.getName(),
    }
            ingredient.getType().toString());
    return ingredient;
}
}
}
</syntaxhighlight>
</syntaxhighlight>
=Reading Data=
=Writing Data=

Revision as of 22:18, 14 October 2018

Internal

Overview

Basic persistence with JDBC is supported by the Spring Framework with JdbcTemplate. JdbcTemplate provides the means by which developers can perform SQL operations against a relational database without the need to retort to verbose JDBC low-level API. With JdbcTemplate, the interaction with the database is reduced to specifying the query and how to map the result of the query to the domain model object.

Spring Persistence Concepts

Spring Persistence Concepts

Spring Boot Support

To add support for JdbcTemplate to a Spring Boot project, add the following starter dependency:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-jdbc')
}

JdbcTemplate needs a database to work with. This is how to add H2 support:

dependencies {
    runtimeOnly('com.h2database:h2')
}

JdbcTemplate-Based Repository

This is a JdbcTemplate-based concrete repository implementation that conceals from the application low-level data access details while exposing a domain model-typed API, represented by the example "IngredientRepository" interface.

@Repository
public class JdbcIngredientRepository implements IngredientRepository {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public JdbcIngredientRepository(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;
    }

    // Reading data ...

    // Writing data ...
}

Reading Data

@Override
public Ingredient findOne(String id) {

    return jdbcTemplate.queryForObject(
            "SELECT id, name, type from INGREDIENT WHERE id=?",
            this::mapRowToIngredient, id);
}

@Override
public Iterable<Ingredient> findAll() {

    return jdbcTemplate.query(
            "SELECT id, name, type from INGREDIENT",
            this::mapRowToIngredient);
}

private Ingredient mapRowToIngredient(ResultSet rs, int rowNumber) throws SQLException {

    return new Ingredient(
            rs.getString("id"),
            rs.getString("name"),
            Ingredient.Type.valueOf(rs.getString("type")));
}

Writing Data

@Override
public Ingredient save(Ingredient ingredient) {

    jdbcTemplate.update(
            "INSERT INTO INGREDIENT (id, name, type) VALUES (?, ?, ?)",
            ingredient.getId(),
            ingredient.getName(),
            ingredient.getType().toString());
     return ingredient;
}
}