This is regarding code quality and coding standards with respect to Spring Ecosystem. Here there are two methods:
isRecordExists
is using a class level static constantString
query. (Definitely suggestable for old way where we use prepared statements)isRecordExists2
is using method levelString
query
Which method is better (isRecordExists
or isRecordExists2
) in context with Spring Boot?
@Repository
public class SpringRepository {
@Autowired
private NamedParameterJdbcTemplate namedParamJdbcTemplate;
private static final String QUERY = " select * from Dual where created_date=:date";
public void isRecordExists(Date date){
/**
* using constant for sql Query String at class level
*/
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("date", date);
namedParameterJdbcTemplate.queryForObject(QUERY, parameters, Integer.class);
}
public void isRecordExists2(Date date){
String sqlQuery=" select * from Dual where created_date=:date";
/**
* using sql Query at method level
*/
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("date", date);
namedParameterJdbcTemplate.queryForObject(sqlQuery, parameters, Integer.class);
}
select *
, you can justselect TOP 1
which will be a lot more efficient. \$\endgroup\$