Spring JDBC Development – A Practical Approach
Spring JDBC Development – A Practical Approach
The general architecture of an application includes a few layers, as presented into the picture below:
18 груд 2017
3181
Інші статті
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Last Modified technique?
How to implement Slowly Changing Dimensions(SCD) Type 2 in Spark?
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Append technique?
Why MongoDB don't fetch all the matching documents for the query fired
How to solve the issue of full disk utilization in HDFS Namenode
Can We Use HDFS as Back-up Storage?
How to do Indexing in MongoDB with Elastic Search? Part 1
How to do Indexing in MongoDB with Elastic Search? Part 2
How to store data on browser using NoSQL IndexedDB?
How to Apply MBTI in HR: Motivation for every day. Groups of People & their Motivations
The general architecture of an application includes a few layers, as presented into the picture below:
As an overview of this structure, it includes:
We would like to show how to write DAOs with the help of Spring.
The role of the JDBC is to allow the uniform interaction from the application to the database. JDBC is using the drivers, one for each database. The drivers translate the commands of the JDBC API to the commands of the database system.
One may wonder why to use Spring JDBC, as long as we already have ORM? There are a reasons for this, to enumerate a few of them:
In this case, why plain JDBC, as you may know it, is not enough? There are also a few reasons:
The DAO design pattern involves the matching between a business object and a database table. We may presume that we start with the following interface and with the following database table:
Our goal is to persist a Book object from memory to the BOOK table from the database. In order to work with plain JDBC in such a case, the code may look like this:
Having a look at the work we need to do using plain JDBC, we can point out the following sequence:
Interested in improving your Spring programming skills? Check out our trainings: Spring Framework for Application Development, Spring Core and Spring Databases.
Catalin Tudose
Java and Web Technologies Expert
As an overview of this structure, it includes:
- The presentation layer: web-browsers, mobile applications, standalone applications – it shows the data and interacts with the user
- The business service layer, it implements the business logic. Business objects are used by the business service. They have to be synchronized with the persistence layer.
- The data access layer ensures synchronization between business objects and the persistence layer. For each domain entity, we should create a separate DAO (Data Access Object) class.
We would like to show how to write DAOs with the help of Spring.
The role of the JDBC is to allow the uniform interaction from the application to the database. JDBC is using the drivers, one for each database. The drivers translate the commands of the JDBC API to the commands of the database system.
One may wonder why to use Spring JDBC, as long as we already have ORM? There are a reasons for this, to enumerate a few of them:
- Spring JDBC provides flexibility; it is using all RDBMS possibilities.
- Spring JDBC is transparent – everything is under the control, while ORM is creating the SQL commands by itself.
- Spring JDBC provides higher performance.
- Last, but not least, as long you still have control, there will be no magic behind your work with the database.
In this case, why plain JDBC, as you may know it, is not enough? There are also a few reasons:
- Plain JDBC still requires manual exception handling.
- Plain JDBC involves manual transaction management.
- Plain JDBC does not provide mapping of data to the objects.
- And there is a big amount of service code.
The DAO design pattern involves the matching between a business object and a database table. We may presume that we start with the following interface and with the following database table:
Our goal is to persist a Book object from memory to the BOOK table from the database. In order to work with plain JDBC in such a case, the code may look like this:
Having a look at the work we need to do using plain JDBC, we can point out the following sequence:
- Define connection parameters
- Open the connection
- Specify the statement
- Prepare and execute the statement
- Iterate through the results
- Do the work for each iteration
- Process any exception
- Handle transactions
- Close the connection
Interested in improving your Spring programming skills? Check out our trainings: Spring Framework for Application Development, Spring Core and Spring Databases.
Catalin Tudose
Java and Web Technologies Expert