Spring Boot data access
Spring Boot data access overview
Spring Data is an open source framework provided by spring to simplify database access and support cloud services. It is an umbrella project, which contains a large number of data access solutions for relational and non relational databases. Its design purpose is to enable us to use various data access technologies quickly and simply. Spring Boot adopts the integrated Spring Data local unified processing data access layer by default. By adding a large number of automatic configurations, various data access templates xxxxtemplate and unified Repository interfaces are introduced to simplify the operation of the data access layer.
Spring Data provides various types of database support. Spring Boot integrates and manages the databases supported by Spring Data and provides various dependent initiators.
name | describe |
---|---|
spring-boot-starter-data-jpa | Initiator of Spring Data JPA and Hibernate |
spring-boot-start-data-mongodb | Initiators for MongoDB and Spring Data MongoDB |
spring-boot-start-data-neo4 | Neo4j diagram database and spring data launcher |
spring-boot-start-redis | Redis key value data store is the initiator of Spring Data Redis and Jedis clients |
Spring Boot does not provide MyBatis scenario dependency, but the MyBatis development team adapts to Spring Boot and provides MyBatis Spring Boot starter dependency initiator to realize data access operation.
Spring Boot integrates MyBatis
Basic environment construction
Because the development of Spring Boot framework is very convenient, it is very simple to realize the integration of Spring Boot and data access layer framework, mainly by introducing the corresponding dependent initiator and setting database related parameters.
1. Data preparation
In MySQL, create a database named spring bootdata, and create two tables t in the database_ Article and t_comment and insert several test data in advance
springbootdata.sql
create database springbootdata; use springbootdata; drop table if exists `t_article`; create table `t_article` ( `id` int(20) not null auto_increment comment 'article id', `title` varchar(200) default null comment 'Article title', `content` longtext comment 'Article content', primary key (`id`) ) engine=InnoDB auto_increment=2 default charset=utf8; insert into `t_article` values ('1','Spring Boot Basic introduction','From introduction to proficient explanation...'); insert into `t_article` values ('2','Spring Cloud Basic introduction','From introduction to proficient explanation...'); drop table if exists `t_comment`; create table `t_comment` ( `id` int(20) not null auto_Increment comment 'comment id', `content` longtext comment 'Comment content', `author` varchar(200) default null comment 'Comment author', `a_id` int(20) default null comment 'Associated articles id', primary key (`id`) ) engine=InnoDB auto_increment=3 default charset=utf8; insert into `t_comment` values ('1','123','tom1','1'); insert into `t_comment` values ('2','123','tom2','1'); insert into `t_comment` values ('3','123','tom3','1'); insert into `t_comment` values ('4','123','tom4','1'); insert into `t_comment` values ('5','123','tom5','2');
2. Create a project and introduce the appropriate initiator
1) Create a Spring Boot project. Create a Spring Boot project named chapter03, select the Mysql and MyBatis Dependencies in the SQL module in Dependencies, and complete the project creation according to the following prompts.
2) Write the entity class corresponding to the database. Create a package named com.example.chapter03 in the chapter03 project, and write the entity classes Comment and Article corresponding to the database table in the package
Comment.java
package com.example.chapter03; public class Comment { private Integer id; private String content; private String author; private Integer aId; //Omit property getter and setter methods //Omit the toString() method }
Article.java
package com.example.chapter03; import java.util.List; public class Article { private Integer id; private String title; private String content; private List<Comment> commentList; //Omit property getter and setter methods //Omit the toString() method }
3. Write configuration file
1) Configure the database connection in the application.properties configuration file.
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456
2) Data source type selection configuration. The tomcat.jdbc data source is used by default in Spring Boot 1.x, and the hikari data source is used by default in Spring Boot 2.x. Additional configuration is required if other data sources are used.
Here you choose to use Alibaba's Druid data source. Add the Druid data source launcher in the pom.xml file.
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
The dependent Druid Spring Boot starter introduced above is also the Druid data source initiator adapted by Alibaba to cater to the Spring Boot project. When the initiator is introduced in pom.xml, no additional configuration is required. The Spring Boot project will automatically identify and configure the Druid data source.
It should be noted that some operating parameters have been initialized in the Druid data source initiator configured above. If the operating parameters of the third-party Druid need to be modified during development, they must be modified in the global configuration file.
application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.initialSize=20 spring.datasource.minIdle=10 spring.datasource.maxActive=100
Modified the type of Druid data source, the number of initial connections, the minimum number of idle connections and the maximum number of connections
Note: after configuration, you will find that the shading of spring.datasource.initialSize, spring.datasource.minIdle and spring.datasource.maxActive properties is yellow, because there are no default properties corresponding to these parameters in the data source automatic configuration class org.springframework.boot.autoconfigure.jdbc.DataSourceProperties provided by Spring Boot, Therefore, the attribute values of these settings cannot be recognized and effective. To do this, we need to write a custom configuration class to inject the properties in the configuration file into the Druid data source properties.
Create a package named com.example.chapter03.config in the chapter03 project, and create a custom configuration class under the package to inject the attribute value of the Druid data source
DataSourceConfig.java
package com.example.chapter03.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource getDruid(){ return new DruidDataSource(); } }