Jay Zhang
Jay Zhang

Reputation: 629

Generic Implementation of MappingSqlQuery in Spring

Currently, when we use MappingSqlQuery to get the resultset from DB, we do something like below:

Extends MappingSqlQuery

In the above example, we have a class LastNameAndAgeQuery extends from MappingSqlQuery, and we applied mapRow method. However, if I want to make some changes in query, let's say I want to add one more info of employee, their birthday. In this way, I have to add:

employee.setBirthday(resultSet.getInt("birthday"));

in maprow method and in the employee class, I have to add field Birthday and write set and get method.

Is there a generic implementation that if I want to modify query, all I need to do is modify query itself, I don't need to change the maprow method, and add field in employee class. Or a better way to do this.

Thank you in advance.

Upvotes: 1

Views: 861

Answers (2)

Vishnu
Vishnu

Reputation: 1009

The only way if you want to continue to keep jdbcTemplate is to go in for generic row mappers like use a map or use the queryForMap api's, so you can modify the query to have more columns and access the map to read its content.

But do keep in mind that concrete row mappers increase readability and easy to maintain.

Upvotes: 1

gerrytan
gerrytan

Reputation: 41123

You might want to look at ORM (Object Relational Mapping) solution such as Hibernate or JPA (Java Persistence API). Typically using ORM you only need to manipulate the POJO (Plain Old Java Object) that represents your table row, and the ORM API will translate that into SQL

Upvotes: 1

Related Questions