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
注解进行测试。需要注意以下两点:
- 使用
@SpringBootTest
注解时,必须定义好场景启动器,否则会报错。 - 测试类所在的包必须与主类所在的包一致,否则也会报错。如果不一致,需要使用
@SpringBootTest
注解中的classes
属性指定主类。
热门推荐
A股春节放假8天,三大交易所2025年休市安排来了!
新手健身选三分化还是五分化?深入解析优缺点及适用人群
一审的结果二审是否会采纳?解析两审终审制下的审判结果运用
一审认罪认罚又上诉二审如何判决
狂推三年也没火,网易版DNF最终还是停服了
手机相册的照片彻底删除了怎么恢复?删除照片恢复的5种方法
在实习中,如何有效地向上级领导反馈问题和提出建议
苹果电脑和iPad哪个实用?
Y迷宫和T迷宫实验如何选择?
中国专家揭示肺结节病理之谜 肺癌研究获重要突破
同等学力申硕:职场人的最佳选择
公积金封顶上限怎么确定?2024年各地标准详解
如何计算股市的EPS指标?这种计算方法有哪些潜在风险和决策?
思维模型会给你提供一种视角或思维框架,决定看待世界的视角!
上一年度职工月平均工资如何计算
如何自我教育以提高投资技能的股票市场教育?
雨中解封!上海中风险地区清零
《西方现代思想讲义》-刘擎
初级跑者进阶关键:乳酸阈值训练
女孩学拉丁舞的好处有哪些 可以参加高考艺考吗
二手房定金监管是中介还是银行?一文详解定金监管全流程
果园种什么草能抑制杂草
违章建筑认定由什么部门负责?侵占消防通道如何处罚?
12319城建服务热线举报范围全解析
深入了解股票市场的ST标识,含义、影响及投资策略
阴阳互补:夫妻关系中的和谐之道
苹果洋葱煮水喝有什么功效
面部轮廓提升哪种效果最好
为何说春节是蕴含诗意的节日?
如何在股票交易中运用技术分析工具?这种技术分析工具的运用有哪些要点?