Spring Boot provides three common methods for accessing databases: Mybaits, Hibernate, and JDBC provided by Spring Boot. Among them, Spring JDBC is the most basic and low-level implementation of accessing the database in Spring. I will explain each method of manipulating the database in three separate installments. If you are interested, stay tuned for updates

Today, we’ll take a look at how Spring JDBC works with databases. I hope you can understand this through reading this article

How to use Spring JDBC to manipulate mysql databaseCopy the code

First, the actual combat principle

Super handy development aid – Lombok

Lombok, a super handy development aid, will be used in the project demo. Using it will not only save us time, but also greatly reduce the amount of code.

To use lombok, just add the Lombok plugin to IDEA and introduce lombok dependencies.

Commonly used annotations

@getter and @setter: Use Getter and Setter methods generated on properties. ToString: used on a class to implement the ToString method for the corresponding class. EqualsAndHashCode: used on a class, generatedhashCode and equals methods. NoArgsConstructor: Used on a class to generate a constructor that takes no arguments. AllArgsConstructor: Used on a class to generate a constructor that contains all fields in the class. @data: generates setter/getter, equals, canEqual,hashCode, toString, and, if final, will not generate setter methods for that property. Slf4j: used on classes, generatedlogConstants. We'll use it in the demo.Copy the code

Spring common bean annotations

@Component - a generic annotation that defines a generic bean@repository for data operations that defines the dao layer for database operations @service - business services @Controller -- for Controller @restController --Spring Boot custom @Controller annotation for Rest servicesCopy the code

3 Use dependency

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Copy the code

4 Simple JDBC operations

The JDBCTemplate provides methods for adding, deleting, modifying, and querying databases.

query
queryForObject
queryForList
queryForMap
update
execute
Copy the code

Query: In addition to basic query, query methods such as Object, list, and map return values are provided. The update method can add, delete, or modify data respectively. Execute is the basic database execution method.

5 Method Principle

Spring JDBC, the Spring integration of JDBC, makes it easier to use. Behind each operation method, the six STEPS of JDBC are actually performed:

A Load the database driver B Create a link C create a statement D Execute an SQL statement E Process the result set f Close the databaseCopy the code

Spring Boot loads the database driver based on the imported dependencies. The JdbcTemplate action method, on the other hand, contains the last five steps

Steps 4 and 5 are contained in the concrete action class of the invocation. The code calls the action class using action.doinstatement (STMT). Such as query method

Two, actual combat

Ok, so much preparation, next, let’s officially enter the actual combat link ~

Because mysql database is widely used in work and study, so I specially demonstrate how Spring JDBC can operate mysql database. Other databases work similarly. You only need to change the database connection package and address configuration.

So some of you might ask, what databases does it support?

The answer is that it supports almost any relational database operation.

1 Introducing dependencies

The following three dependencies need to be introduced:

Spring-jdbc: spring-boot-starter-JDBC mysql connection: mysql-connector-java Lombok Development assistance: LombokCopy the code

2 Configure database information

Configure the URL, username, and password of the database respectively in the configuration file application.properties.

Tips1:

Spring Boot automatically configures the database driver based on the type of database connection dependency we introduced, so we don’t need to configure the database driver.

Make sure that the User table already exists in the database

3 Create an entity class

As you can see, lombok’s @data and @Builder annotations are used in the User entity class. With these annotations, our entity class is much cleaner

4 Create a DAO layer

1) define the class

Use the @repository annotation to declare the class as a DAO layer bean, and use Lombok’s @slf4J annotation for fast log output

2) injection JdbcTemplate

3) Specific operation

Use the action class in the JdbcTemplate to add and query operations, respectively

Query, which demonstrates two cases where the result is returned as an object and a List respectively. You can try other cases for yourself.

5 Invoke the DAO layer methods

A Spring Boot Boot class that implements the ApplicationRunner interface and overrides its Run method. When the service starts, the run method is run. The methods to add a user and query a list of users are called in the run method.

Tips2:

An interface similar to ApplicationRunner is CommandLineRunner, which you can check out for yourself. At this point, you only need to understand the usage of the ApplicationRunner interface.

6 Start the Project

Of course, if you have any questions, please leave a message and communicate with me

Three, total knot

Lombok, a powerful development aid, can simplify our development

Spring Boot commonly declares bean annotations

3 Spring JDBC data operation mode

Today we demonstrate the principle and method of Spring JDBC database operation, and at the same time also mentioned Spring Boot common declaration bean annotations, the use of Lombok tools, JDBC database operation principle and other content.

In the future, I will continue to talk about Spring Boot’s function points in this principle + field way. You might learn more efficiently that way. It is recommended that after the actual combat, we return to see the principle again, the effect will be better

If you feel like you’ve learned a lot, or reviewed a lot of skill points, follow me and keep learning

This project code has been uploaded to Github ~ if necessary, please refer to it

Github.com/wangjie0919…

Review of previous articles in Spring Boot:

  1. Spring Boot (1) : Feature Overview

  2. Spring Boot (II) : The first Spring Boot project