DAO Support Classes in Spring

To make it easier to work with a variety of data access technologies such as JDBC, JDO and Hibernate in a consistent way, Spring provides a set of abstract DAO classes that one can extend. These abstract classes have methods for providing the data source and any other configuration settings that are specific to the relevant data-access technology.

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the aforementioned persistence technologies fairly easily and it also allows one to code without worrying about catching exceptions that are specific to each technology. 
Popular Tutorials
  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial
• JdbcDaoSupport – superclass for JDBC data access objects. Requires a DataSource to be provided; in turn, this class provides a JdbcTemplate instance initialized from the supplied DataSource to subclasses.
• HibernateDaoSupport – superclass for Hibernate data access objects. Requires a SessionFactory to be provided; in turn, this class provides a HibernateTemplate instance initialized from the supplied SessionFactory to subclasses. Can alternatively be initialized directly via a HibernateTemplate, to reuse the latters settings like SessionFactory, flush mode, exception translator, and so forth.
• JdoDaoSupport – super class for JDO data access objects. Requires a PersistenceManagerFactory to be provided; in turn, this class provides a JdoTemplate instance initialized from the supplied PersistenceManagerFactory to subclasses.
• JpaDaoSupport – super class for JPA data access objects. Requires a EntityManagerFactory to be provided; in turn, this class provides a JpaTemplate

In Spring JDBC Framework there are many DAO support classes which help to reduce the configuration of JdbcTemplate, SimpleJdbcTemplate and NamedParamJdbcTemplate with dataSource object.
For JdbcTemplate:
org.springframework.jdbc.core.support.JdbcDaoSupport 
For SimpleJdbcTemplate:
org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport
For NamedParamJdbcTemplate:
org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport

Following class file without using Dao Support class.

EmployeeDaoImpl.java
package com.dineshonjava.sdnext.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.dineshonjava.sdnext.dao.EmpDao;
import com.dineshonjava.sdnext.domain.Employee;
import com.dineshonjava.sdnext.jdbc.utils.EmployeeMapper;

/**
 * @author Dinesh Rajput
 *
 */
@Component
public class EmployeeDaoImpl implements EmpDao {

 //This code is common for all DAO classes - start here
        @Autowired
 private JdbcTemplate jdbcTemplateObject;
 
 /**
  * @param jdbcTemplateObject the jdbcTemplateObject to set
  */
 public void setJdbcTemplateObject(JdbcTemplate jdbcTemplateObject) {
  this.jdbcTemplateObject = jdbcTemplateObject;
 }
      //This code is common for all DAO classes - End here

@Override
public Employee getEmployee(Integer empid) {
 String SQL = "SELECT * FROM Employee WHERE empid = ?";
 Employee employee = (Employee) jdbcTemplateObject.queryForObject(SQL, new Object[]{empid}, new EmployeeMapper());
    return employee;
}
}
If you have 100 files for DAO classes then you have write the above common code for each of 100 files. So reduce this common code by using the DAO support classes for each templates.
Each template has own Support class.
Following class file with using Dao Support class.

EmployeeDaoImpl.java
package com.dineshonjava.sdnext.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;

import com.dineshonjava.sdnext.dao.EmpDao;
import com.dineshonjava.sdnext.domain.Employee;
import com.dineshonjava.sdnext.jdbc.utils.EmployeeMapper;

/**
 * @author Dinesh Rajput
 *
 */
@Component
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmpDao {
@Override
    public Employee getEmployee(Integer empid) {
    String SQL = "SELECT * FROM Employee WHERE empid = ?";
    Employee employee = (Employee) getJdbcTemplate().queryForObject(SQL, new Object[]{empid}, new EmployeeMapper());
    return employee;
  }
}
Here we see that there is one method name getJdbcTemplate() associated with JdbcTemplate and DataSource. There are no need to configure manually.





No comments:

Post a Comment