Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Native Queries With Spring Boot and NamedParameterJdbcTemplate

DZone 's Guide to

Native Queries With Spring Boot and NamedParameterJdbcTemplate

Let's see an example of how to use NamedParameterJdbcTemplate.

Free Resource

Do you need to run a native query with Spring Boot? Should you consult a database of a legacy application? Do you feel that if you execute a native query you save the mapping of many tables in your database?

Well, for these cases, my favorite way is to use NamedParameterJdbcTemplate. Next, we will see an example of how to use it. In our case, we must obtain the quantity of orders from a given customer.

First, we must define the POJO (DTO) that will obtain the result of the query:

public class NativeQueryDTO {
    private String name;
    private int orderCount;
...
    //Getters - Setters   
}

Then we must create the repository, which contains the method that returns the required information:

@Component
public class NativeRepository {
    @Autowired 
    private NamedParameterJdbcTemplate jdbcTemplate;

    public NativeQueryDTO countCustomerOrder(Long id){        
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("customerId", id);

        String sql = " select c.name, count(o) as orderCount  "
                   + " from customers c, orders o "
                   + " where c.id = o.customer_id and c.id = :customerId "
                   + " group by c.name ";

        return (NativeQueryDTO)jdbcTemplate.queryForObject(
            sql, parameters, BeanPropertyRowMapper.newInstance(NativeQueryDTO.class));  
    }
}

The previous code uses the queryForObject method that executes the query, and the necessary parameters are passed with the object of type MapSqlParameterSource. To obtain a correct mapping as a third parameter, we must send a BeanPropertyRowMapper with the class mapping. Because we are using BeanPropertyRowMapper, we do not need to implement a RowMapper.

Finally, in the official documentation of Spring, they recommend the implementation of a customized RowMapper in case of needing better performance.

public class NativeQueryDTOMapper implements RowMapper<NativeQueryDTO> {
    @Override
    public NativeQueryDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
        NativeQueryDTO dto = new NativeQueryDTO();
        dto.setName(rs.getString("name"));
        dto.setOrderCount(rs.getInt("orderCount"));
        return dto;
    }  
}

Then use it as follows:

return (NativeQueryDTO)jdbcTemplate.queryForObject(
            sql, parameters, new NativeQueryDTOMapper());  

We have seen that it is very easy to use native queries with Spring Boot, and I hope that this example was helpful. Until next time!

Topics:
springboot ,native query ,namedparameterjdbctemplate ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}