I have so many dynamic queries in my project and there may be 2 ways to build up a dynamic query. I have shown them below,
- Conditional where
@Query("select c from Customer c where " +
" (:name IS NULL OR c.name LIKE %:name%) "+
"and (:email IS NULL OR c.email =:email) "+
"and (:genderIds IS NULL OR c.genderId IN (:genderIds) ) ")
List<Customer> findByParameters(@Param("name") String name
, @Param("email") String email
, @Param("genderIds") List<Integer> genderIds);
- By using If conditions, e.g.
public List<Customer> getCustomersNativeQueryConditional(RequestCustomer request) {
StringBuilder sb = new StringBuilder();
MapSqlParameterSource params = new MapSqlParameterSource ();
sb.append("select * from Customer c where 1=1 ");
Optional.ofNullable(request.getName())
.ifPresent(s->{
sb.append(" AND c.name LIKE :name");
params.addValue("name","%"+s+"%");
});
Optional.ofNullable(request.getEmail())
.ifPresent(s->{
sb.append(" AND c.email =:email ");
params.addValue("email",s);
});
Optional.ofNullable(request.getGenderIds())
.ifPresent(ids->{
sb.append(" AND c.genderId IN (:genderIds) ");
params.addValue("genderIds",ids);
});
return namedPrmJdbcTemplate.query(sb.toString(), params, BeanPropertyRowMapper.newInstance(Customer.class));
}
Note: request object in the second case is a POJO,
public class RequestCustomer {
private String name;
private String email;
private List<Integer> genderIds;
}
What I really wonder is which one of them is much more efficient/better with respect to database and/or coding standards. Which one is supposed to be used in an well-designed application by an experienced developer?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…