0

I was debating my self whether to use jdbcTemplate vs apache DBUtils in my project. My application is a spring app, where we are using jdbcTemplate for few requests. But i was thinking about this very lightweight and easy to understand Apache DbUtils.

I know both jdbcTemplate and dbutils are abstraction layer on top of regular JDBC which helps us to avoid broiler plate code.

So My questions are:

1) Any strong reason i should convince myself to migrate everything from jdbcTemplate to dbutils?

2)Any Auto POJO mappers in jdbcTemplate which automaps to java POJO class?In DbUtils i know we can do this like below:

ResultSetHandler<List<Object>> beanListHandler = new BeanListHandler<Object>(Object.class, new BasicRowProcessor(new GenerousBeanProcessor())); I

In jdbcTemplate i know we can have a custom rowMapper where we can explicitly set the properties like below, but is there a way to do auto mapping to POJOS like in dbutils?

@Overides

public Object mapRow(){
     //set to Object by calling setters
}

3)Performance wise will it make any difference using dbutils?(As we are already using jdbcTemplate in other requests)

Can anyone suggest the best to chose from these two and why from your experience?

ging
  • 117
  • 8
  • JdbcTemplate includes regular POJO mappings *by default*; this is the purpose for all those signatures that take a `Class` as a parameter. – chrylis -cautiouslyoptimistic- Mar 17 '19 at 02:52
  • @chrylis Thanks will take a look. any suggestions on other points? – ging Mar 17 '19 at 02:58
  • Stack Overflow is designed to avoid long-winded or open-ended discussions. This question seems headed in that direction. Either reframe this as a specific technical issue to be solved, or move it to the sister site *Software Recommendations Stack Exchange* where you would reframe the question as a list of needs and the criteria by which you would consider a suggested product to successfully meet those needs. Questions asking to compare product X to product Y are off-topic on both sites. So a third avenue is a discussion site such as http://www.JavaRanch.com/ – Basil Bourque Mar 17 '19 at 04:49

1 Answers1

1

If your application is using Spring framework then you should stick to using JdbcTemplate. Working in tandem with Spring framework it offers many benefits, some of the key ones are

  • Container managed lifecycle
  • Goes well with Spring advocated DAO design pattern
  • Spring's transaction management

and not to forget, plenty of material is available on internet for this combination, should you get stuck at any point and try to look for pointers.

What you can do to reduce boiler plate around data extraction (from ResultSet) and creating POJOs though, is, write some generic implementations of RowMapper (or ResultSetExtractor). The implementation should probe the ResultsetMetadata to determine column names and types of returned records and with a bit of Reflection on Class (object of which RowMapper should return) match them to fields or setters, instantiate, fill and return the object.

Gro
  • 1,613
  • 1
  • 13
  • 19