SpringMVC-the initial use of JdbcTemplate

Article Directory

JdbcTemplate function

He is the part of the database encapsulated in MVC, and it is also a tool. For the time being, only the situation where the data source is handed over to him is recorded.

A simple introduction

There are generally three steps to using JdbcTemplate

Import package

Insert picture description here

Create connection pool

Create JdbcTemplate, and hand over the connection pool to JdbcTemplate
and then use JdbcTemplate

Insert picture description here

After setting the data source for the jdbcTemplate object, you can use its update method to call the SQL statement for adding, deleting, and modifying. Of course, the connection here is taken in the data source by jdbcTemplate

Record the errors encountered in the middle

  1. The imported mysql driver jar version package needs to be almost the same as the mysql version installed by yourself, otherwise an error may be reported
  2. Sometimes, when we set up jdbcurl, we need to follow the time zone:, serverTimezone=UTCnote that this is a parameter and needs to be ?separated from the path

Use JdbcTemplate by configuring Bean

  1. Configure the connection pool object in the Bean
  2. Configure JdbcTemplate in Bean
  3. Get JdbcTemplate in the method and use

In order to understand the coupling, we need to configure the parameters of the connection pool in the form of key-value pairs in a file, and then use the following EL expression to get

Insert picture description here

Insert picture description here

Use of the Update method

Because the addition, deletion and modification do not need to have a return value, you can use the update method of JdbcTemplate to use the sql statement
as follows: modify and delete operations:

Insert picture description here

Here is to directly test the update method of JdbcTemplate, so we can directly set the properties of the JdbcTemplate object type here


For query operations, in our ordinary web, we need to encapsulate the query results into the entity, which is more troublesome. In MVC, MVC can help us encapsulate the query results into the entity.

Multiple result query

When the result of our query may be more than one,
I need to use the query method of the jdbcTemplate object

Insert picture description here

He has two parameters, the first is the sql statement. The second is the RowMapper interface. Here, we have to pass in an instance of the implementation class of this interface

Here, the instance we passed in is of this class

Insert picture description here



Here, this instance needs to pass in the generic, the generic type, that is, the type of the entity class we need to encapsulate, and then he also needs to pass in a formal parameter, which is the class of the corresponding entity

Insert picture description here
Insert picture description here

This return value is a List collection of corresponding entities

Single result query

The return value of a single result is not a collection. Other methods can be used here.
Use the queryForObject method of JdbcTemplate:

Insert picture description here

He has three parameters
The first one is the SQL statement, the second one is the BeanPropertyRowMapper<泛型>(泛型的类)same as the multiple result query, and the third one is the parameter in the sql statement (single result query generally requires parameters)

Insert picture description here

There is only one return value

Aggregate queries (simple classes where the result is a number)

This is the method name used: queryForObject
but the parameters are not the same.
1: There is no need for the statement in sql. Parameters
2: There is no need to encapsulate the entity. The second parameter is directly put into the bytecode of the class provided by the system.

Insert picture description here



Insert picture description here