Blog Banner Image

Spring and Data Access Object (DAO) – Part 1

In this article first we will understand what DAO is, then the DAO module in Spring.

DAO stands for Data Access Object. It’s a design pattern in which a data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanisms. By mapping application calls to the persistence layer, DAOs provide some specific data operations without exposing details of the database. This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of domain-specific objects and data types (the public interface of the DAO), and how these needs can be satisfied with a specific DBMS, database schema, etc. (the implementation of the DAO).

While developing an enterprise application, one of the most important aspects is to deal with the DB like connection management, transaction management etc. Initialization of data access object, resource management and transaction management and exception handling are the main parts of persistence framework. Spring data access framework is provided to integrate with different persistence frameworks like JDBC, Hibernate, JPA, iBatis etc.

Let’s take a deeper look, how JDBC and Spring are setup together to communicate to the database.

  • Setting up the Data Source

Either we can get the data source from JNDI

<bean id=”dataSourceBean” class=”org.springframework.jndi.JndiObjectFactoryBean”>

<property name=”jndiEnvironment”>

<props>

<prop key=”java.naming.initial.factory”>define value</prop>

<prop key=”java.naming.provider.url”>insert URL</prop>

</props>

</property>

<property name=”jndiName” value=”dirName”/>

</bean>

Or we can create the data source in the spring container

<bean id=” dataSourceBean” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>

<property name=”driverClassName” value=”oracle.jdbc.driver.OracleDriver”/>

<property name=”url” value=”jdbc:oracle:thin:@localhost:1521:XE”/>

<property name=”username” value=”username”/>

<property name=”password” value=”password”/>

</bean>

(BasicDataSource can also be used in place of DriverManagerDataSource)

  • Define the DAO bean

<bean id=”daoBean” class=” daoBean” init-method=”init”>

<constructor-arg ref=” dataSourceBean”/>

</bean>

  • Now write the DAO component to access the data source defined above

public class daoBean {

DataSource dataSourceBean;

Connection conn;

public daoBean (DataSource dataSourceBean){

this. dataSourceBean = dataSourceBean;

}

public void init(){

conn= dataSourceBean.getConnection();

}

public void useConnection(){

System.out.println(conn);

}

}

  • Now let’s write a simple client class to see what we are getting in the connection back

public class daoClient {

public static void main(String args[]){

BeanFactory bf=new FileSystemXmlApplicationContext(“applicationContext.xml”);

daoBean daob=( daoBean)bf.,getBean(“daoBean”);

daob.useConnection();

}

In the next part of this article we’ll explore JdbcTemplate and JdbcDaoSupport classes, what they are and how we can use them.