Advantages of Spring Boot JDBC
Spring JDBC is used to build the Data Access Layer of enterprise applications, it enables developers to connect to the database and write SQL queries and update data to the relational database. The code related to the database has to be placed in DAO classes. SpringJDBC provides a class called JdbcTemplate to handle the database-related logic. JdbcTemplate is an abstraction layer on top of JDBC technology. The JdbcTemplate internally uses the Jdbc code but provides an API, so developers don’t have to write the Boiler Plate code.
Why should we use Spring Boot JDBC?
Spring boot JDBC is similar to spring JDBC in terms of features, rather spring boot JDBC is different in implementation. As Spring boot follows convention over configuration it is simpler to implement JDBC in spring boot.in order
Advantage of using SpringJDBC over Traditional JDBC API
- Spring provides org.springframework.jdbc.core.JdbcTemplate class in the JDBC package which allows releasing of the database connection automatically.
- JdbcTemplate provides a great exception handling mechanism that is more specific to deal with the database, it converts the standard JDBC SQLExceptions into RuntimeExceptions which are generic and more informative, allowing developers to better identify the error.
- The RowMapper or ResultSetExtractor is an interface typically used by the JdbcTemplate to map one row per basis of rows of the ResultSet, which allows to translates the SQL result direct into an Object or a list of Objects.
- JdbcTemplate provides methods to write the SQL queries directly, thereby saving a lot of work and the API layer avoids the use of boilerplate code in the JDBC program.
Some Advantages of Spring Boot JDBC over Spring JDBC
JDBC Using Spring Boot
JDBC Using Spring
|Only one spring-boot-starter-jdbc dependency is required to make available all the Spring modules we need to perform JDBC operations.||Multiple dependencies like spring context and spring JDBC must be included in order to perform JDBC operations.|
|In spring boot we do not have to explicitly register template beans PlatformTransactionManager, JdbcTemplate, NamedParameterJdbcTemplate spring boot will create it for you automatically if not created.||The template beans PlatformTransactionManager, JdbcTemplate, NamedParameterJdbcTemplate must be registered in order to use JDBC in your application.|
|It auto-initializes data source bean if not mentioned explicitly and you can set the spring.datasource.initialize to false if you don’t want to use the bean.||In spring JDBC, it is required to create a database bean either using XML or javaconfig.|
|Any database initialization script like deleting or creating a table in the .sql file gets executed automatically in spring boot JDBC.||Any database script in the .sql files needs to be given in the configuration file for it to work in spring JDBC.|