All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sparta.springwebutils.jdbc.SpartaNamedParameterJdbcTemplate Maven / Gradle / Ivy

/*
 * Sparta Software Co.
 * 2017
 */
package org.sparta.springwebutils.jdbc;

import java.util.Map;
import java.util.Optional;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

/**
 * Extended operations for NamedParameterJdbcTemplate.
 *
 * @author Carlos Eduardo Endler Genz – Sparta Java Team
 * 

* History: * - Mar 11, 2015 - Carlos Eduardo Endler Genz * - Oct 21, 2021 - Daniel Conde Diehl - Formatting */ public class SpartaNamedParameterJdbcTemplate extends NamedParameterJdbcTemplate { /** * constructor by datasource * * @param dataSource data source to initialize */ public SpartaNamedParameterJdbcTemplate(DataSource dataSource) { super(dataSource); } /** * Constructor by jdbcOperations * * @param jdbcOperations jdbc operations to initialize */ public SpartaNamedParameterJdbcTemplate(JdbcOperations jdbcOperations) { super(jdbcOperations); } /** * Query given SQL to create a prepared statement from SQL and a list * of arguments to bind to the query, mapping a single result row to a * Java object via a RowMapper. * * @param sql SQL query to execute * @param paramSource container of arguments to bind to the query * @param rowMapper object that will map one object per row * @param The return object type * @return the single mapped object (optionally) * @throws org.springframework.dao.EmptyResultDataAccessException if the query does not return exactly one row, or does not return exactly * one column in that row * @throws org.springframework.dao.DataAccessException if the query fails */ public Optional queryForOptionalObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) throws DataAccessException { Optional result; try { final T obj = super.queryForObject(sql, paramSource, rowMapper); result = Optional.ofNullable(obj); } catch (EmptyResultDataAccessException e) { result = Optional.empty(); } return result; } /** * Query given SQL to create a prepared statement from SQL and a list * of arguments to bind to the query, mapping a single result row to a * Java object via a RowMapper. * * @param sql SQL query to execute * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param rowMapper object that will map one object per row * @param The return object type * @return the single mapped object (optionally) * @throws org.springframework.dao.EmptyResultDataAccessException if the query does not return exactly one row, or does not return exactly * one column in that row * @throws org.springframework.dao.DataAccessException if the query fails */ public Optional queryForOptionalObject(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException { Optional result; try { final T obj = super.queryForObject(sql, paramMap, rowMapper); result = Optional.ofNullable(obj); } catch (EmptyResultDataAccessException e) { result = Optional.empty(); } return result; } /** * Query given SQL to create a prepared statement from SQL and a list * of arguments to bind to the query, mapping a single result row to a * Java object via a RowMapper. * * @param sql SQL query to execute * @param paramSource parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param requiredType class to map * @param The return object type * @return the single mapped object (optionally) * @throws org.springframework.dao.EmptyResultDataAccessException if the query does not return exactly one row, or does not return exactly * one column in that row * @throws org.springframework.dao.DataAccessException if the query fails */ public Optional queryForOptionalObject(String sql, SqlParameterSource paramSource, Class requiredType) throws DataAccessException { Optional result; try { final T obj = super.queryForObject(sql, paramSource, requiredType); result = Optional.ofNullable(obj); } catch (EmptyResultDataAccessException e) { result = Optional.empty(); } return result; } /** * Query given SQL to create a prepared statement from SQL and a * list of arguments to bind to the query, expecting a result object. *

The query is expected to be a single row/single column query; the returned * result will be directly mapped to the corresponding object type. * * @param sql SQL query to execute * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param requiredType the type that the result object is expected to match * @param The return object type * @return the result object of the required type (optionally) * @throws org.springframework.dao.EmptyResultDataAccessException if the query does not return exactly one row, or does not return exactly * one column in that row * @throws org.springframework.dao.DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForObject(String, Class) */ public Optional queryForOptionalObject(String sql, Map paramMap, Class requiredType) throws DataAccessException { Optional result; try { final T obj = super.queryForObject(sql, paramMap, requiredType); result = Optional.ofNullable(obj); } catch (EmptyResultDataAccessException e) { result = Optional.empty(); } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy