Let's talk
Reach out, we'd love to hear from you!
This article deep dives into what Data Access Object (DAO) is in the Java-based Spring Boot Framework. The motive is to simplify technical niceties and break down the crux in a way that can be well suited to the interested layman. Stay with us!
Explained: DAO or Data Access Object in Java
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.
The isolation enabled by DAOs 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).
DAOs in Java can be helpful in situations where you don’t want to leverage the full power of the persistence layer or for cases where you need to make little changes to the underlying database. DAOs prevent developers from having to know SQL (the standard query language for databases), which can make development and debugging much faster and easier.
How Do DAOs support integration?
Need to integrate your enterprise application with a different persistence layer, but you don’t know where to begin? Java provides a comprehensive and modular framework for data access-Spring Boot, coupled with DAO-that makes integrating with different persistent layers easy.
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 objects, resource management and transaction management and exception handling are the main parts of the 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 set up 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.