We can create a Spring Boot application that uses JDBC as quickly as making its JPA variant without hassle. But no doubt, JDBC applications are much faster than those relying on ORM technology. For many, the idea of a JDBC application evokes unpleasant experiences, memory, or gossip about low-level programming with the JDBC API. With Spring Boot, that is no longer the case. However, there is a minor setback – we will be working with SQL statements. In addition to that, our codebase size could be humongous. Also, some layer components eventually become not readily reusable. Worse and, as a result, the whole codebase became unwieldy.
Create a Spring Boot JDBC Application With Spring Initializr
To create a Spring Boot JDBC application with Spring Initializr, we only need to choose two dependencies – Spring Data JDBC and the corresponding driver for the RDBMS (e.g., MySQL) we intend to use.
What’s inside our pom.xml
Once we extracted the project that the Spring Initializr had generated, we can examine the content of the pom.xml file. In the file, we only have two dependencies that we included via the Spring Initializr.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.turreta.springbootjdbc</groupId> <artifactId>springbootjdbc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootjdbc</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Similar to creating a Spring Boot JPA application with the Spring Initializr, we get the minimum possible number of files, consisting of a Java file, application.properties, and pom.xml. We can ignore the unit test class file in this context.
Configure and Use Spring Boot JDBC Application
Lastly, we modify the empty application.properties file with the following settings.
1 2 | spring.datasource.url=jdbc:mysql://localhost:3306/mysqldb spring.datasource.username=user |
To run queries or updates against the database, we can use either a JdbcTemplate or NamedParameterJdbcTemplate.
1 2 3 4 5 6 7 8 9 10 | @Repository public class PersonRepository { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate; // ... } |
Wasn’t that easy? It wasn’t as hellish as you had imagined.
We may start with using JPA for our Spring Boot application. But it does not mean we’ll stick with JPA forever. We can always migrate to JDBC without hassle, at least at the persistent layer. Then, we will work our way up until our Spring Boot application entirely moves away from JPA.