3

I read about Spring framework's DAOSupport classes. But I could not understand the advantages of these DAOSuport classes. In DAOSupport classes we call getXXXTemplate() method to get the specific template object and then use it further for DB access.

Even without extending DAOSupport we can inject XXXTemplate in our class. Rest of this things will remain same.

Then what is advantage of extending DAOSupport class?

EDIT:- Adding example Class extends spring's SimpleJdbcDaoSupport

public class JdbcDao extends SimpleJdbcDaoSupport {
    public int create(Bb obj) {        
       getSimpleJdbcTemplate().update("insert into ..... ")  //insert query
    }

Bean of this class can be defined as :-

<bean id="jdbcDao" class="example.dao.support.JdbcDao">  
  <property name="dataSource"><ref local="dataSource"/></property>
</bean> 

We can create a custom class without extending SimpleJdbcDaoSupport which will have property of type JdbcTemplate

public class MyDAO {
    public myJdbcTemplate; // ant its getter and setter
    public int create(Bb obj) {        
       getMyJdbcTemplate().update("insert into ..... ")  //insert query
    }

It's bean wil be defined as :-

<bean id="jdbcDao" class="MyDAO">  
  <property name="myJdbcTemplate"><ref local="jdbcTemplateBean"/></property>
</bean> 

As you can see both classes do same thing. While extending SimpleJdbcDaoSupport we are injecting DataSource and without it we are injecting directly jdbcTemplate bean. That's it. No more difference. So I do not see any much advantage with this much use of DAOSupport classes. Any additional functionality given by DAOSupport classes ?

Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76

1 Answers1

0

When you use HibernateDAOSupport you can see the difference. 1. Config the transaction to optimize the performance of the application on the applicationContext :

select : read only create/update : read and write.

  1. You use one session only(with getHibernateTemplate() and the sessionFactory)
  2. When we update some data on the database we do only merge the modifications whith the method impleted on HibernateDAOSupport.
  3. There are many method already implemented on the DAOSupport and we can use this to our need.