问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Spring Boot整合连接数据库的详细配置教程

创作时间:
作者:
@小白创作中心

Spring Boot整合连接数据库的详细配置教程

引用
CSDN
1.
https://m.blog.csdn.net/weixin_61635597/article/details/142312513

本文将详细介绍如何在Spring Boot项目中整合和连接数据库。通过本文,你将学习到如何创建数据库表、配置Maven依赖、创建Java Bean对象、编写启动程序以及配置数据库连接信息等关键步骤。

十五,Spring Boot 整合连接数据库(详细配置)

准备数据库表

首先,我们需要在MySQL中创建一个名为spring_boot的数据库,并在其中创建一个名为furn的数据表。以下是创建表的SQL语句:

DROP DATABASE if EXISTS spring_boot;
CREATE DATABASE spring_boot;
USE spring_boot;

CREATE TABLE furn (
    id INT(11) PRIMARY KEY auto_increment,
    name VARCHAR(64) not NULL,
    maker VARCHAR(64) not null,
    price DECIMAL(11,2) not null,
    sales INT(11) not null,
    stock INT(11) not null,
    img_path VARCHAR(256) not null
);

INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'北欧风格小桌子','熊猫家居',100,666,7,'assets/images/producth');

INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'简约风格小椅子','熊猫家居',200,666,7,'assets/images/producth');

INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'典雅风格小桌子','蚂蚁家居',100,666,7,'assets/images/producth');

INSERT into furn(`id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path`)
VALUES(null,'温馨风格小桌子','蚂蚁家居',100,666,7,'assets/images/producth');

配置Maven依赖

接下来,我们需要在pom.xml文件中添加相关的Maven依赖。以下是完整的pom.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rainbowsea</groupId>
    <artifactId>springboot_database</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

创建Java Bean对象

使用Lombok插件创建Furn类,该类将作为数据库表furn的映射对象:

package com.rainbowsea.springboot.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;

@Data
@AllArgsConstructor
@NoArgsConstructor
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/1.jpg";
}

编写启动程序

创建Spring Boot应用的启动类:

package com.rainbowsea.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(Application.class, args);
    }
}

配置数据库连接信息

resources目录下创建application.yaml文件,配置数据库连接信息:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring_boot?useSSL=true&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: MySQL123
    driver-class-name: com.mysql.cj.jdbc.Driver

运行测试

在测试类中使用@SpringBootTest注解进行测试。需要注意以下两点:

  1. 使用@SpringBootTest注解时,必须定义好场景启动器,否则会报错。
  2. 测试类所在的包必须与主类所在的包一致,否则也会报错。如果不一致,需要使用@SpringBootTest注解中的classes属性指定主类。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号