相关推荐recommended
Spring Boot MySQL数据库的使用
作者:mmseoamin日期:2024-04-27

目录

简介Spring Boot

Spring Boot的优点

Spring Boot连接数据

1.添加依赖

1.2开启连接数据库

1.2.1 如果没有开启数据库运行程序的时候会出现这样的报错这就是没有连接数据库,所以我们开启数据库即可使用。

1.2.2 我的名字是MySQL110所以一会用命令字符开启数据库的时候用的就是这个

1.2.3 net start MySQL110 开启数据库

1.2.4 mysql -uroot -p123456 使用密码登录数据库注意-p后面是你安装数据库时候的密码

1.2.5 create database 数据库名;  注意使用别人代码的时候创建的数据库要查看配置文件里面的数据库名字不如创建了数据库程序还是找不到数据库

1.2.6 use springboot;  创建好数据库之后要启用对应的数据

1.2.7  create table 表名 (id int,name varchar(50),telephome varchar(50));创建表

1.3 在Spring Boot 中使用数据库

1.3.1 再次启动程序

1.3.2 使用Spring Boot 添加数据到数据库

1.3.3 使用Spring Boot 删除数据

1.3.4 使用Spring Boot 查询数据

1.3.5 使用Spring Boot 修改数据 

1.4文件连接报错

1.注意这样的报错这样的报错来源于文件路径不对所以说要配置对应的文件路径才行

2.配置对应的文件位置把爆红的删除就行了

1.5 端口号被占用

1.5.1所以我们要使用终端来清除端口

1.以管理员身份启动

2.输入 netstat -ano | findstr 8080 查看正在使用的端口

3. tasklist | findstr 5800

4. taskkill -f -t -im java.exe 运行清理端口的程序

1.6 MySQL数据库管理系统,下面是一些常用的MySQL语句

1.创建数据库

2. 删除数据库

4. 创建表

5. 删除表

6. 插入数据


简介Spring Boot

Spring Boot是一个用与开发和构建基于Spring的框架的Java应用于程序的开源框架,它简化Spring应用程序的初始配置和开发过程,提供了一种快速构建可独立运行、生产就绪的Spring应用程序的方式。

Spring Boot的优点

1.简化配置

2.快速启动

3.自动装配

4.无配置XML文件

5.提供了一组便捷的功能和开发工具,如嵌入式数据库、HTTP客户端、安全认证、监控等。

相比传统的Spring框架相比Spring Boot的优点变得很多。

Spring Boot连接数据库

1.添加依赖

在连接数据库之前我需要配置连接数据库的依赖等第一次使用整套的代码爆红是没有配置依赖的我们配置好依赖加载好依赖就好了。

Spring Boot MySQL数据库的使用,第1张

我们打开文件选择设置找到构建、执行、部署,构建工具Maven。

Spring Boot MySQL数据库的使用,第2张

更改Maven主路径为自己使用的依赖包与用户设置文件和本地仓库,本地仓库可以随便创建一个文件放置就行了。

Spring Boot MySQL数据库的使用,第3张

在用户设置文件和本地仓库无法更改路径的时候需要点击重写才能配置路径。Spring Boot MySQL数据库的使用,第4张

用户设置文件配置。Spring Boot MySQL数据库的使用,第5张

本地仓库随便建一个文件夹然后放进去就行了。Spring Boot MySQL数据库的使用,第6张

配置好是这样的。Spring Boot MySQL数据库的使用,第7张

加载好了之后就不会出现爆红了。Spring Boot MySQL数据库的使用,第8张

注意如果配置了依赖pom.xml还是报错的话那就是没有下到对应的依赖,这是因为依赖文件下载的地方在国外所以网不好的时候下载不了,所以我们要在settings.xml中添加阿里巴巴的文件库。

Spring Boot MySQL数据库的使用,第9张

1.2开启连接数据库

1.2.1 如果没有开启数据库运行程序的时候会出现这样的报错这就是没有连接数据库,所以我们开启数据库即可使用。

Spring Boot MySQL数据库的使用,第10张

我们先查看数据库的名字。

Spring Boot MySQL数据库的使用,第11张

1.2.2 我的名字是MySQL110所以一会用命令字符开启数据库的时候用的就是这个。
Spring Boot MySQL数据库的使用,第12张

注意我们打开数据的时候记得用管理员的身份打开不然会这样。

Spring Boot MySQL数据库的使用,第13张

1.2.3 net start MySQL110 开启数据库

Spring Boot MySQL数据库的使用,第14张

1.2.4 mysql -uroot -p123456 使用密码登录数据库注意-p后面是你安装数据库时候的密码

Spring Boot MySQL数据库的使用,第15张

1.2.5 create database 数据库名;  注意使用别人代码的时候创建的数据库要查看配置文件里面的数据库名字不如创建了数据库程序还是找不到数据库

Spring Boot MySQL数据库的使用,第16张

springboot就是这个程序使用的数据库名称。

1.2.6 use springboot;  创建好数据库之后要启用对应的数据

Spring Boot MySQL数据库的使用,第17张

1.2.7  create table 表名 (id int,name varchar(50),telephome varchar(50));创建表

1.查看表 show tables;

2.查看表里面的数据 select * from 表名; 

Spring Boot MySQL数据库的使用,第18张

1.3 在Spring Boot 中使用数据库

1.3.1 再次启动程序

就发现不会报错了已经能使用了。

Spring Boot MySQL数据库的使用,第19张

1.3.2 使用Spring Boot 添加数据到数据库

注意端口号和网站。

Spring Boot MySQL数据库的使用,第20张

然后在浏览器输入 localhost:8080/save。

Spring Boot MySQL数据库的使用,第21张

这边已经添加成功我们查看数据库信息。

Spring Boot MySQL数据库的使用,第22张

这边已经显示到了数据添加成功。

1.3.3 使用Spring Boot 删除数据

在浏览器使用 localhost:8080/delete/35 注意35是是数据库中的id

Spring Boot MySQL数据库的使用,第23张

在数据库查看数据。

Spring Boot MySQL数据库的使用,第24张

1.3.4 使用Spring Boot 查询数据

Spring Boot MySQL数据库的使用,第25张

在浏览器输入 localhost:8080/getCustomer/33 注意33是数据库id。

Spring Boot MySQL数据库的使用,第26张

1.3.5 使用Spring Boot 修改数据 

Spring Boot MySQL数据库的使用,第27张

在浏览器输入 localhost:8080/update 。

Spring Boot MySQL数据库的使用,第28张

在数据库查看数据

Spring Boot MySQL数据库的使用,第29张

id33的张三就变成了简凌凌了。

1.4文件连接报错

1.注意这样的报错这样的报错来源于文件路径不对所以说要配置对应的文件路径才行

Spring Boot MySQL数据库的使用,第30张

2.配置对应的文件位置把爆红的删除就行了

Spring Boot MySQL数据库的使用,第31张

Spring Boot MySQL数据库的使用,第32张

1.5 端口号被占用

当运行的时候报错为

Description:

Web server failed to start. Port 8080 was already in use .

的时候就是因为端口8080这个端口号被占用了。

 

Spring Boot MySQL数据库的使用,第33张

1.5.1所以我们要使用终端来清除端口
1.以管理员身份启动

输入 netstat -ano 检查端口号。

Spring Boot MySQL数据库的使用,第34张

2.输入 netstat -ano | findstr 8080 查看正在使用的端口

Spring Boot MySQL数据库的使用,第35张

3. tasklist | findstr 5800。

Spring Boot MySQL数据库的使用,第36张

4. taskkill -f -t -im java.exe 运行清理端口的程序

Spring Boot MySQL数据库的使用,第37张

现在就可以使用了呢。

Spring Boot MySQL数据库的使用,第38张

1.6 MySQL数据库管理系统,下面是一些常用的MySQL语句

1.创建数据库

CREATE DATABASE 数据库名称;

Spring Boot MySQL数据库的使用,第39张

2. 删除数据库

   

   DROP DATABASE 数据库名称;

Spring Boot MySQL数据库的使用,第40张

3. 使用数据库

   

   USE 数据库名称;

Spring Boot MySQL数据库的使用,第41张

4. 创建表

    create table 表名(id int,name varchar(50),telephome varchar(50));

Spring Boot MySQL数据库的使用,第42张

5. 删除表

   DROP TABLE table_name;

 

Spring Boot MySQL数据库的使用,第43张

6. 插入数据


   INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Spring Boot MySQL数据库的使用,第44张