Introduction to Spring Boot and JDBCTemplate: JDBC Template
This tutorial will cover a way to simplify the code with JDBC using the Spring JDBC Template.
Join the DZone community and get the full member experience.Join For Free
As with any programming language, Java has several tools to make easy integration between the language and the database. There are several tools such as Hibernate, Eclipse Link, JPA specification, and so on. However, the ORM brings several issues, and sometimes it does not sense to use it and then use it a Java Communication layer or JDBC. This tutorial will cover a way to simplify the code with JDBC using the Spring JDBC Template.
Mapping frameworks such as ORM reduces a lot of boilerplate, reduces the duplicated code, avoids bugs, and doesn’t reinvent the wheel. However, an Object-relational impedance mismatch that brings a set of conceptual and technical difficulties is often encountered when an RDBMS is being served by an application program written in an object-oriented programming language.
A solution might use JDBC, but it increases the complexity to handle data and Java. How can an application reduce this verbosity with JDBC? Spring JDBCTemplate is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC API but eliminates a lot of problems with JDBC API.
Starting With Spring Initializr
This sample application will use JDBCTemplate with Spring to use two databases: PostgreSQL to run the application and H2 in the test scope. For all Spring applications, you should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the setup for you. This example requires the JDBC API, Spring MVC, PostgreSQL driver, and H2 Database dependencies.
The next step is the Car entity, with five fields: id, name, city, model, and color. In this project, a POJO as an anemic model is more than enough to handle it. A rich model guarantees the object’s rules and avoids any encapsulation issue, thus, a bulletproof API. However, it only fits in complex projects. Highlighting: 'it depends' is always a good answer when we talk about software architecture.
To have tight integration between Java and ResultSet of JDBC, Spring JDBC has a RowMapper interface. A developer can either create a custom class or use BeanPropertyRowMapper, which reduces the boilerplate; however, the entity should have a public getter and setter; it might get an encapsulation issue besides it provides convenience rather than high performance. For best performance, consider using a custom RowMapper implementation.
The entity is ready; let’s talk about the Data Access Object, DAO; every time there is a vast or complex query to handle, there is a discussion about where the SQL query should go. Briefly, when the script is hardcoded, it is clearer what the script is doing, but when the command is tremendous, it becomes challenging to read and understand. Thus, we can move it to read it from properties.
Once the reader is familiar with the query in the code, we’ll explore the query option in a properties file. We’ll have a class to hold this duty, and it will have tight integration with Spring Configuration.
The CarDAO will do the CRUD operation once the findAll has pagination support. It uses NamedParameterJdbcTemplate to class with a basic set of JDBC operations, allowing named parameters rather than traditional ‘?’ Placeholders. To avoid connection leak, Spring has the Transactional annotation to control the transaction in each method where we defined in our code.
The code is ready; let’s test it. Yeap, a TDD technique, has a philosophy to start with the test and then create the code. But it is not the article’s goal. In the test scope, we’ll generate a DBMS in memory with H2. Spring has several features to allow us to test smoothly. Thanks to Spring, we can use H2 in the test without impact the driver that will operate on production.
In this tutorial, we introduced Spring JDBC and its operation; we talked a little about the mapping trade-offs and the place to store the queries in addition to the testing and configuration resources. Spring brings several features that increase developer productivity. In the second part, we will talk a little about Spring MVC and its works with the database.
Opinions expressed by DZone contributors are their own.