[JavaWeb] Explanation of the use of JdbcTemplate

1. Jdbc review

The dbc core has six steps, but it is too cumbersome to write like this every time, so the first, second, and sixth steps are encapsulated into the JdbcUtil tool class, and the JdbcUtil tool class is optimized:

insert image description here
①Jdbc core six steps

This is the most basic six steps of Jdbc.
Among them, the first step is to register the driver, and automatic registration will be realized in MySQL3.0, but it is better to know how to write it yourself.
The sixth step is to release resources. Because resources need to be released after each connection, it is encapsulated into a tool class to reduce code redundancy.

②Learning of connection pool

The study of the two open source connection pools is essentially a further optimization of the second step (obtaining connections) in the core six steps. Similarly, it is encapsulated into the tool class JdbcUtil.

③ Pre-compiled learning

The problem of sql injection and the introduction of precompilation, to put it bluntly, are the optimization of steps 3 and 4 of the six core steps.
Precompilation is safer and more efficient than conventional methods.
And today I want to learn a small framework JdbcTemplate, to be precise, it is a module in the big framework of spring.
Template, the meaning of template, is a template of Jdbc.
Its appearance is another optimization of steps 3, 4, and 5 of the six core steps, and at the same time further simplifying the code, it will be more convenient to use.

2. Addition, deletion and modification of JdbcTemplate

Tossing and turning, in fact, it is nothing more than adding, deleting, modifying and checking.
insert image description here

① Create a JdbcTemplate object
Create a JdbcTemplate object directly, and you will find that the constructor parameters need to be passed in to the data source.
Encapsulate a method to obtain a data source in the tool class Jdbcutil written by yourself, and then obtain it directly.

②Operation on the data table itself: execute()
execute, which means execution, and this method is used for the operation of the data table itself (addition, deletion, modification and query).
We create a data table student with 3 columns: id, name, age.
Of course, in Java, the data table itself is rarely directly manipulated, and it is generally used to manipulate the data in the table.

③ Addition, deletion and modification of data in the table: update()
update, meaning modification, for data modification in the data table (both additions, deletions and modifications are modifications here), this method is used. We add a new piece of data to the Student table.
The update method has two parameters: one is the sql statement, and the other is the variable parameter (that is, the "?" in the precompilation of the sql statement).
This method makes precompilation more concise, and we don't need to set the specific value of each "?".
If we use the conventional method, we need to write it like this:
insert image description here
use the tool class JdbcUtil to get the connection, use precompilation, and then need to set the value of "?" in the precompilation one by one.
Therefore, through comparison, it can be found that the JdbcTemplate template makes the code more concise, and its bottom layer is actually such an encapsulation.

2. Query of JdbcTemplate

For the addition, deletion, modification and query of data in tables and tables, query is undoubtedly the most important existence.
This is also easy to understand. We often need to log in to various apps and websites. The essence is to query data, and the frequency of modifying, canceling and creating accounts is undoubtedly much less, and the query is much more complicated. Let us draw a picture to analyze:

insert image description here
There are three cases of query results, and each case has its corresponding API in Java:

①Single row and single column: The result of queryForObject()
query is a piece of data, so the return value used in Java is Objcet.
Now write the corresponding code:
insert image description here
The queryForObject() method has three parameters:
the sql statement, the class object corresponding to the queried data type, and the parameters in precompilation.
The first example queries the amount of data, so there are only the first two parameters.

②Single row and multiple columns:
The result of queryForMap() query is a row of data, so the return value used in Java is the Map collection.
The Map collection is composed of key and vlaue, where the column name of this row is equivalent to the key value, and the data of this row is equivalent to value.
The code is written as follows:
insert image description here

There are two parameters in queryForMap:
sql statement, and parameters in precompilation.

③Multiple rows and multiple columns: the queryFroList()
query results in multiple rows of data, so the return value used in Java is a list collection, which is equivalent to loading many Maps in the list collection.
Among them, multiple rows and single columns belong to multiple rows and multiple columns.
The code is written as follows:
insert image description here
the return value is a lot of maps, just traverse the list and print the maps by yourself.
The above is the query operation for the data table.
Then I found that using JdbcTemplate greatly simplifies step 5 of the core 6 steps (processing results).
In the past, it was necessary to process the result set, that is, ResultSet. It was very cumbersome to write an iterator and then obtain the specific values ​​in it.
Now JdbcTemplate encapsulates it, which is very convenient to use.
Let’s review yesterday’s code first, taking quaryForList as an example:
insert image description here
①The use of JdbcTemplate
Directly a queryForList() method can get the desired result.
The enhanced for loop can directly traverse and print.

②The use of conventional methods
To be precise, the bottom layer of JdbcTemplate is a similar encapsulation, so we can directly call the queryForList() method.
Through the writing of these two methods, the convenience of JdbcTemplate can be better reflected. In addition, there is another Api about query that has not been learned.
That is the use of the query() method.
In Java, a JavaBean can be used to represent a data table:
insert image description here

The following relationship is satisfied between Java and the data table:

  • The class name is equivalent to the data table name.
  • Member variables are equivalent to data table column names (fields).
  • Each User object can be used to represent a row of data in the table.
  • queryForMap() is equivalent to a User object.
  • queryForList() is equivalent to a List collection with many User objects.

Well, after understanding this relationship, let's write the code:

JdbcTemplate query operation
insert image description here
①query method

This method has three parameters: sql statement, rowMapper and args.
The sql statement is easy to understand. Needless to say, args refers to the "?" in the sql statement. In the example, it is a full table query, so there is no args.
The most important thing is this rowMapper.

②rowMapper interface
row means "row", and Mapper means mapper.
To put it bluntly, this interface is to map the data of a row in the data table into a JavaBean object.
The JavaBean in the above example is the User class.
This interface is very similar to the call processor in the dynamic proxy:
when the template calls the query, the rowMapper will call its own mapRow() method.
Interface call method, interface-oriented programming, that is, the method rewritten by its implementation class (in the example, it is represented by an anonymous inner class, and lambda can also be used)

③Traverse the list collection,
among which the String method can be rewritten in User to print directly.
Ok, after writing the code, run the test.
insert image description here
A User object corresponds to a row of data in the data table user.
In the above quary method, the anonymous inner class is used to represent the rowMapper.
In fact, there is an implementation class defined in JavaTemplate, which can make the quary method simpler.
insert image description here

BeanPropertyRowMapper is an implementation class in RowMapper, just use it directly.
There is no need to rewrite the mapRow method in the rowMapper interface.
The technology used at the bottom layer is reflection, so it needs to pass in the Class object in the corresponding JavaBean.

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/125222196