If, instead, you are already using, maybe you don't know a really nice object i discovered some time ago: the BeanPropertySqlParameterSource
The classic way to go in a query (or in an update) is to set the params one by one;
let's take this Person bean as example (id field omitted in this example):
public class Person { private String name; private String lastname; private Date birthdate; private Language motherTongue; /* Getters and Setters */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } public Language getMotherTongue() { return motherTongue; } public void setMotherTongue(Language motherTongue) { this.motherTongue = motherTongue; } }
and the Language class referenced in it:
class Language { int id; int description; /* Getters and Setters */ public int getId() { return id; } public void setId(int id) { this.id = id; } public int getDescription() { return description; } public void setDescription(int description) { this.description = description; } }
(with Language values like this: {[1 , English] , [2 , Italian], etc...}
Let's say i've filled all the fields in the Person object, and i want to insert it into a database.
Its instance is named "person". Normally i would go with:
String sql = "INSERT INTO person_table " + " ( name, lastname, birthday, id_language) " + " VALUES " + " ( :name, :lastname, :birthdate, :idLanguage) "; MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("name", person.getName(), Types.VARCHAR); params.addValue("lastname", person.getLastname(), Types.VARCHAR); params.addValue("birthdate", person.getBirthdate(), Types.DATE); params.addValue("idLanguage", person.getLanguage().getId(), Types.NUMERIC); return namedJdbcTemplate.update(sql, params);
Imagine a big bean with a lot of fields, with every field passed manually... it's not really a good way to go.
With the BeanPropertySqlParameterSource object instead, IF we are taking all the fields from one object (and his inner objects), we can do like this:
String sql = "INSERT INTO person_table " + " ( name, lastname, birthday, id_language) " + " VALUES " + " ( :name, :lastname, :birthdate, :language.id) "; BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(person); return namedJdbcTemplate.update(sql, params);
Not bad uh ? It will retrieve the specified fields (and types) through Reflection, including the fields value in the inner objects contained in the main bean. As you can note, the notation is OGNL-like, with the getter of the inner object starting with a dot and a lower case letter, stripping "get" from the method name:
:language.idmeans
person.getLanguage().getId();
PRO:
- less code;
- auto-decoding field Type through Reflections, avoiding user errors;
- changes on query or bean automatically managed, without the needs of change param settings
CONS:
- less customization possibilities
Enjoy
Thanks. I had no idea that BeanPropertySqlParameterSource supported OGNL-like nesting. Very helpful.
ReplyDeleteHey, do you know how to deal with situation when you like to use OGNL-like notation for situation like :schema_name.TABLE. When you only like to map schema_name?
ReplyDeleteI am another customer of this site so here I saw various articles and posts posted by this site,I curious more energy for some of them trust you will give more information further.
ReplyDeleteAI
This is a great post I saw thanks to sharing. I really want to hope that you will continue to share great posts in the future.
ReplyDeletehrdf claimable training
This is a great motivational article. In fact, I am happy with your good work. They publish very supportive data, really. Continue. Continue blogging. Hope you explore your next post
ReplyDelete360DigiTMG big data analytics training in malaysia
You should talk it's shocking. Your blog survey would extend your visitors. I was fulfilled to find this site.I expected to thank you for this phenomenal read!!
ReplyDeletewhat is the difference between analysis and analytics
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
H3K7OR
شركة تسليك مجاري بالهفوف UzYu40fSEf
ReplyDelete