XV, Spring Boot integration to connect to the database (detailed configuration)
@
- XV, Spring Boot integration to connect to the database (detailed configuration)
- Finally:
JDBC + HikariDataSource (Spring Boot built-in database)
HikariDataSource: Currently on the market is a very good data source, is the Spring Boot2 default data source.
Demonstrates how Spring Boot can accomplish MySQL operations with jdbc + HikariDataSource.
Prepare the data table we need to test.
# establish furns_ssm
DROP DATABASE if EXISTS spring_boot
CREATE DATABASE spring_boot
USE spring_boot
# establish家居表 data sheet
CREATE TABLE furn (
id INT(11) PRIMARY KEY auto_increment, -- id
name VARCHAR(64) not NULL, -- furniture name
maker VARCHAR(64) not null, -- company
`price` DECIMAL(11,2) not null, -- prices
`sales` INT(11) not null, -- sales volume
`stock` INT(11) not null, -- property or cash held in reserve
`img_path` VARCHAR(256) not null -- Photo Path
-- take note of:Not single quotes.
);
SELECT * from furn;
INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'Scandinavian style small table','Panda Home',100,666,7,'assets/images/producth')
INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'Minimalist Small Chair','Panda Home',200,666,7,'assets/images/producth')
INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'Elegant Style Small Table','Ant Home',100,666,7,'assets/images/producth')
INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'Warm style small table','Ant Home',100,666,7,'assets/images/producth')
Import the relevantjar
Dependency.
<?xml version="1.0" encoding="UTF-8"? >;xml version="1.0" encoding="UTF-8"?
<project xmlns="/POM/4.0.0"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0." >
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>springboot_database</artifactId>
<version>1.0-SNAPSHOT</version>
<! -- Importing SpringBoot parent project - prescribed writeups - >
<parent>
<groupId> </groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
<! -- Import web project scenario launcher: will automatically import all dependencies [libraries/jars] of jar packages related to web development -- <!
<! -- Later on, we will explain what dependencies spring-boot-starter-web introduces -- > <!
<dependencies>
<dependencies>
<groupId> </groupId>
<artifactId>spring-boot-starter-web</artifactId>.
</dependency>
<! --introducing lombok-->
<dependency>
<groupId> </groupId>
<artifactId>lombok</artifactId>
</dependency>.
<! -- For database development, introduce data-jdbc starter spring boot's own database connection pool.
HikariDataSource -- >
<! -- Note:
The jdbc HikariDataSource data source imported by spring boot.
1. HikariDataSource data source
2. jdbc data link
3. tx transaction
4. Note: spring boot does not know what database your project wants to operate on.
So you need to specify the database you want and tell spring boot which database you want to connect to.
-->
<dependency>
<groupId> </groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>.
<! --< /dependency> <!
1. Introduce the mysql driver, here we are introducing 8.0.26. 2.
2. The version of the mysql driver should be the same as the actual mysql version installed.
3. our spring-boot mysql Differential Arbitration version is <>8.0.26</> 4. the mysql driver should be the same as the actual installed mysql version.
4. The mysql driver version can also be specified in the properties configuration file.
-->
<dependency>
<groupId> mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<! -- <version>8.0.26</version>-->.
</dependency>
<! -- How to develop springboot test class, we need to introduce spring-boot-starter-test -->
</dependency>; <!
<groupId> </groupId>
<artifactId>spring-boot-starter-test</artifactId>.
</dependency>.
</dependencies>.
</project>
Note: The version of MySQL we are importing here
<! --<! 1, introduce the mysql driver, here we introduced 8.0.26 2. The version of the mysql driver should be the same as the version of mysql actually installed. 3. our spring-boot mysql Differential Arbitration version is <>8.0.26</> 4. the mysql driver should be the same as the actual installed mysql version. 4. The mysql driver version can also be specified in the properties configuration file. --> <dependency>; <groupId> mysql</groupId> <artifactId>mysql-connector-java</artifactId> <! -- <version>8.0.26</version>-->. </dependency>
Creates a bean object that maps the datasheet furn to Java. Here we use the lombok plugin for this.
package ;
import ;
import ;
import ;
import ;
@Data
@AllArgsConstructor
@NoArgsConstructor // utilization lombok Plug-in for auto-configuration
public class Furn {
private Integer id;
private String name;
private String maker;
private BigDecimal price;
private Integer sales;
private Integer stock;
private String imgPath = "assets/images/product-image/";
}
Write the startup program:
package ;
import ;
import ;
import ;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ioc = (, args);
}
}
We need to be under the class pathresources
Create a file named file that writes information about the connected databases.
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_boot?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: MySQL123
driver-class-name:
Run the test:
Here we write the test program and note the use:@SpringBootTest
There are two caveats to annotations:
- In Spring Boot, using the @SpringBootTest annotation, you must define a scenario starter, and if you don't, you will get an error.
- The class tested with @SpringBootTest must be in the same package as the actual main class, otherwise it will also report an error. If the package is not the same, then you need to use @SpringBootTest's
classes
Class.
Run the test:
Finally:
"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."