The Spring JDBC module is responsible for accessing the operational database. It is convenient and fast to use the Spring framework’s own JdbcTemplate in small and medium-sized projects, and a little encapsulation is completely comparable to MyBatis framework.

Spring Data JDBC and Spring Data JPA in the Spring ecosystem are enhancements to native JDBC, which provides the ability to query databases and map them to entities, and has many similarities with MyBatis. There will be a new chapter on Spring Data.

  1. Introducing Maven dependencies

Integrating Spring JDBC simply introduces in pom.xml:

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

JDBC connection pool and database driver:

<! -- HIkari JDBC connection pool -->
<dependency>
	<groupId>com.zaxxer</groupId>
	<artifactId>HikariCP</artifactId>
	<version>${version}</version>
</dependency>
<! Mysql connection driver -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>${version}</version>
</dependency>
Copy the code
  1. Configuring a Data Source

Edit the application.yml file

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver # database connection driver
    username: ${MYSQL_USERNAME:engrz} Database connection user name
    password: ${MYSQL_PASSWORD:passwd2021} Database connection password
    url: jdbc:mysql://${MYSQL_HOST:mysqlhost}:${MYSQL_PORT:3306}/database? characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allow Database connection url
Copy the code

Spring Boot supports hikari, DBCP2, and druid. To set connection pool parameters, see Spring Boot Data Properties.

  1. Use the JdbcTemplate

After configuring the data source, you can use the JdbcTemplate directly in your project and Spring will inject it automatically.

@Autowired
private JdbcTemplate jdbcTemplate;
Copy the code

JdbcTemplate is a common method

Int UPDATE (String SQL) Perform new or update, returns the number of affected records

int[] batchUpdate(String… SQL) batch execute new or update, return the number of affected records

Void execute(String SQL) Executes an SQL statement

List query(String SQL, RowMapper RowMapper) Queries and returns a result set. A RowMapper is a mapped entity class that finds attribute names based on column names and assigns values

List

> queryForList(String SQL) Query and return a set of results, placing each piece of data in a Map

Map

queryForMap(String SQL) queryForMap(String SQL) Puts query results in a Map. This method is only applicable when one data is returned
,>

T queryForObject(String SQL, Class requiredType) places the query result in an object, only when a single piece of data is returned

SqlRowSet queryForRowSet(String SQL) A query returns a RowSet object, which encapsulates a common method and dynamically returns a mapping object.

All of the above have overloaded methods, suitable for different scenarios of query, the use of simple will not detail. See the documentation for more methods and instructions: JdbcTemplate


Unless otherwise noted, all of them are written by Sieges. Link to this article: engr-z.com/88.html