I’m learning Spring. I’ve been very impressed with how Spring can automatically bind the values from HTML form fields to the fields to the setter methods in a data holder object. No more one gazillion calls to request.getParamter() and testing for null values on each.
I’m wondering if Spring has anything like this for things going like this in the opposite direction, pulling information out of a database?
I have a record with a large amount of fields coming out of my data layer via a HashMap.
The HashMap keys match the database field names which also match the setters of my data holder object. Is there someway Spring can automagically bind the values from the HashMap into the data holder object or will I still have to do that myself?
Thanks in advance for any information.
Thanks in advance for any info.
You are actually talking about Spring MVC, the web framework built upon the Spring framework. You can use the Spring framework with many other web frameworks (or none at all) so Spring and Spring MVC are terms that should not be used interchangeably.
And that also answers your question: Spring MVC is a Framework for the web layer. It would be an awful approach to make the web layer aware of the underlying persistence technology, so direct support for database technologies would be a huge code smell in a web framework.
That said, Spring provides a lot of Support for many different persistence technologies: JDBC, Hibernate, JPA, JDO, MyBatis and through the Spring Data project also various NoSQL databases. The simple HashMap bindings you are looking for sound like something that could be realized through MyBatis (but I don’t know MyBatis, so I’m not sure about that). But you won’t find an out of the box solution in Spring MVC, and that’s a good thing, for the reasons mentioned above.