通過HTML網頁對mysql數據庫進行增刪改查(CRUD實例)

首先我們得了解一下大致的架構 ,如下:

我們采用自底向上的方式進行開發,

一、先寫mysql數據庫

二、再寫java后端(Spring MVC架構)(這個是什么東西不懂不要緊,跟著步驟做就行了)

三、最后寫前端頁面(HTML)

一、 Mysql數據庫部分

我們要通過網頁對數據庫進行開發,那么我們需要先準備數據庫。

為了方便開發,直接用navicat來創建數據庫,名字叫做crud,字符集為utf8

接著在數據庫中建立數據表,我們以學生信息為例,建立一個名字叫做student的數據表?

字段列表如下:

?順便向數據庫中添加一些數據

這樣,我們第一部分就做好了,點支煙獎勵一下自己~~

二、 編寫java后端代碼

1.打開IDEA,?新建spring項目

?勾選web依賴,就勾這個就好了

?點擊完成后,如果報錯就打開這個鏈接:

IDEA創建springboot項目時提示https://start.spring.io初始化失敗_暮晨丶的博客-CSDN博客

?2.編寫pom.xml文件

我們先找到這個“dependencies”標簽

?在這個標簽內添加依賴坐標。

這個文件就是項目用到的外部依賴,我們分析一下:

連接mysql數據庫我們需要添加驅動:

        <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>

為了簡化交互我們還需要添加mybatis的依賴坐標

        <dependency><!--Springboot和MyBatis整合得ar包坐標--><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency>

還有和前端交互用?的thymeleaf依賴坐標

        <dependency><!--和前端交互用的--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

還有一些雜七雜八好用的依賴

        <dependency><!--德魯伊數據源坐標--><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><dependency><!--注解開發自動導入的依賴   @Data那種 --><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

最后結果:?

?

然后刷新maven pom文件就編寫好了?

3.建包(3+1=4個包)

MVC架構:controller、service、mapper

存放實體類的包:pojo

?4.在pojo下建立實體類,類的屬性要和數據表的字段對應

在pojo下創建一個 Student類,類上面添加三個注解,這三個注解的作用分別是

添加:get set方法、有參構造方法、無參構造方法

?5. 配置數據庫連接信息,通過yml文件(里面的縮進要格外注意,縮進一定要和我寫的一樣)

#數據庫連接信息
spring:datasource:username: rootpassword: 2633url: jdbc:mysql://localhost:3306/crud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource

?結果:

?5. 編寫mapper層

(1)在mapper中創建接口 名字為StudetMapper

(2)在接口中添加注解 @Mapper

?(3)?在接口中編寫增刪改查的方法

?6.編寫SQL

(1)先裝一個 mybatis的插件

?

(2)在下面這個路徑中先建立一個mapper目錄,再創建一個StudentMapper.xml文件?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.crud.mapper.StudentMapper"></mapper>

?好了之后屏幕會有藍色頭繩的小鳥

?點擊小鳥,就會跳轉到StudentMapper接口,這時我們可以看到接口方法報錯了

?(3)在mapper標簽中編寫sql語句

把鼠標光標放到紅色的地方,按快捷鍵 alt + 回車 就可以創建寫sql的地方了,然后再標簽里邊編寫sql

(4)連接數據庫

?之后就不會報錯了。

繼續編寫SQL語言,重復返回接口按ALT+回車+回車編寫全部的SQL

?直到這里沒有報錯,并且有紅色頭繩小鳥

?7.在application.yml文件中添加mybatis信息

#配置Mybatis映射路徑
mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.StudentDemo.pojo

?配置端口為80

#配置端口
server:port: 80

結果

?

?8. 編寫service層

(1)在類外面添加@Service注解

(2)在類中添加

    @AutowiredStudentMapper studentMapper;

(3)調用studentMapper的接口方法

結果截圖:?

9.編寫controller層和前端頁面

注:到這里是我認為最難的部分,如果前面沒有做單元測試的話 會有可能報錯,所以單元測試很重要

(1)在controller中創建StudentController類 ,在類外面注解為@Controller

(2)在類中添加注解@Autowired? 調用StudentService

?StudentController代碼

package com.example.crud.controller;import com.example.crud.pojo.Student;
import com.example.crud.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;import java.util.List;@Controller
public class StudentController {@AutowiredStudentService studentService;@GetMapping("/toindex")public String toindex(){return "index";}//查詢所有頁面@GetMapping("/findStudentList")public String findStudentList(Model model){List<Student> studentList=studentService.findAllStudent();//傳進去的是一個鍵值對model.addAttribute("studentList",studentList);//傳進前端的東西//返回值==html文件名return "findStudentList";}//跳轉到添加頁面@GetMapping("/toaddStudent")public String toaddStudent(){//返回值為文件名return "addStudent";}//真正執行添加@PostMapping("/addStudent")public String addStudent(Student student){studentService.addStudent(student);//跳轉到哪里(文件名)return "redirect:/findStudentList";}@GetMapping("/toupdateStudent/{id}")public String toupdateStudent(@PathVariable("id")String id, Model model){//先找到被修改的對象Student student=studentService.findStudentById(id);//將對象保存到model中model.addAttribute("student",student);//html文件名return "updateStudent";}@PostMapping("/updateStudent")public String updateStudent(Student student){//獲取當前頁面學生信息,傳入按照id修改學生信息的Service,進行信息修改studentService.updateStudent(student);return "redirect:/findStudentList";}@GetMapping("/deleteStudent/{id}")public String deleteStudent(@PathVariable("id")String id){studentService.deleteStudent(id);return "redirect:/findStudentList";}
}

?結果截圖

(3)編寫首頁index.html

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/findStudentList}">查詢信息</a>
</body>
</html>

?(4)編寫查詢所有頁面 findStudentList.html

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<!--/*@thymesVar id="student" type="com.example.crud.pojo.Student"*/-->
<!--/*@thymesVar id="studentList" type="com.example.crud.controller"*/-->
<head><meta charset="UTF-8"><title>查詢所有</title>
</head>
<body>
<table border="1"><tr><!--列--><th>學號</th><th>名字</th><th>性別</th><th>年齡</th><th>籍貫</th><th>操作</th></tr><tr th:each="student:${studentList}"><td th:text="${student.getId}"></td><td th:text="${student.getName()}"></td><td th:text="${student.getSex()}"></td><td th:text="${student.getAge()}"></td><td th:text="${student.getAddress()}"></td><td><a  role="button" th:href="@{/toupdateStudent/${student.getId()}}">修改</a><a  role="button" th:href="@{/deleteStudent/${student.getId()}}">刪除</a></td></tr>
</table>
<div ><a  role="button" th:href="@{/toaddStudent}">添加員工</a><a  role="button" th:href="@{/toindex}">返回首頁</a>
</div>
</body>
</html>

?

?

? (5)編寫修改頁面?

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<!--/*@thymesVar id="student" type="com.example.crud.pojo.Student"*/-->
<head><meta charset="UTF-8"><title>修改頁面</title>
</head>
<body>
<div ><form th:action="@{/updateStudent}" method="post"><div ><label >ID</label><input type="text" name="id" th:value="${student.getId()}" class="form-control" placeholder="請輸入該學生的id"></div><div class="form-group"><label >姓名</label><input type="text" name="name" th:value="${student.getName()}" class="form-control" placeholder="請輸入修改后的姓名"></div><div class="form-group"><label >性別</label><input type="text" name="sex" th:value="${student.getSex()}" class="form-control" placeholder="請輸入修改后的性別"></div><div class="form-group"><label >年齡</label><input type="text" name="age" th:value="${student.getAge()}" class="form-control" placeholder="請輸入修改后的年齡"></div><div class="form-group"><label >地址</label><!--/*@thymesVar id="student" type="com.example.crud.pojo.Student"*/--><input type="text" name="address" th:value="${student.getAddress()}" class="form-control" placeholder="請輸入修改后的地址"></div><button type="submit" class="btn btn-default">保存</button><a role="button" th:href="@{/findStudentList}">查詢員工</a><a  role="button" th:href="@{/toindex}">返回首頁</a></form>
</div>
</body>
</html>

?截圖

?

?(6) 編寫添加學生信息頁面

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>添加頁面</title></head>
<body>
<div  ><form th:action="@{/addStudent}" method="post"><div><br><label >ID</label><input  type="text" name="id"  placeholder="請輸入該員工的id"></div><div><label >姓名</label><input type="text" name="name"  placeholder=""></div><div><label >性別</label><select name="sex"><option value ="">null</option><option value ="男">男</option><option value ="女">女</option></select></div><div><label >年齡</label><input type="text" name="age"  placeholder=""></div><div><label >地址</label><input  type="text" name="address"  placeholder=""></div><br><button type="submit">點擊添加</button><a class="myButton" th:href="@{/findStudentList}">查詢員工</a><a class="myButton" th:href="@{/toindex}">返回首頁</a></form></div></body>
</html>

? ? ? ? ? 這樣 整個項目就寫完了

???????三、測試運行

成功開啟服務截圖:

?開啟服務失敗截圖:

?解決辦法有兩個:

1.修改端口號 ,在yml文件中?

2. 殺死當前80端口的進程

win+r 進入命令行 輸入

netstat -ano

?輸入

taskkill /pid 17156 -f

?

?這樣就可以繼續啟動服務了

啟動完成后打開瀏覽器,輸入 127.0.0.1 回車

?這樣就訪問到了 index.html頁面

?

?點擊查詢信息,就訪問到了 findStudentList.html頁面的內容,在頁面上展示了數據庫中的記錄

?

點擊修改? 跳轉到修改頁面 updateStudent.html

?

?點擊刪除 直接刪除數據庫中的內容

點擊 添加 跳轉到添加頁面 addStudent.html

?這樣就完成了CRUD實例的編寫,重點在Controller 和 交互前端交互的部分 有很多值得深究的地方,時間精力有限,不能一一解釋,而且存在諸多BUG,有很多值得優化的地方,mybatis可以寫的更好

1.并發控制的問題(一個線程把數據刪除了,另一個線程點擊了修改,這樣就會報錯)

2.添加一個空記錄,然后把這個空記錄進行刪除(可以再前端頁面用一個正則表達式解決)

3.添加一條重復的記錄

????????僅記錄學習。。。。。不足之處還需要大哥批評指正

項目源代碼icon-default.png?t=M85Bhttp://鏈接:https://pan.baidu.com/s/1oXVY-eD0ypziKzPcaKGAmg?pwd=1234 提取碼:1234

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/165228.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/165228.shtml
英文地址,請注明出處:http://en.pswp.cn/news/165228.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決:Gitee + PicGo配置圖床失敗

解決&#xff1a;Gitee PicGo配置圖床失敗 PicGo安裝插件的時候選擇&#xff1a;gitee-uploader&#xff0c;不要選擇gitee&#xff01; 在Gitee新建的圖床倉庫中設置一個images文件夾&#xff0c;用來保存上傳的圖片&#xff0c;但是要注意在PicGo中的path中要寫上路徑/img…

數據庫基礎入門 — SQL運算符

我是南城余&#xff01;阿里云開發者平臺專家博士證書獲得者&#xff01; 歡迎關注我的博客&#xff01;一同成長&#xff01; 一名從事運維開發的worker&#xff0c;記錄分享學習。 專注于AI&#xff0c;運維開發&#xff0c;windows Linux 系統領域的分享&#xff01; 本…

linux的基礎命令

文章目錄 linux的基礎命令一、linux的目錄結構&#xff08;一&#xff09;Linux路徑的描述方式 二、Linux命令入門&#xff08;一&#xff09;Linux命令基礎格式 三、ls命令&#xff08;一&#xff09;HOME目錄和工作目錄&#xff08;二&#xff09;ls命令的參數1.ls命令的-a選…

基于yolov2深度學習網絡的喝水行為檢測系統matlab仿真

目錄 1.算法運行效果圖預覽 2.算法運行軟件版本 3.部分核心程序 4.算法理論概述 4.1、YOLOv2網絡原理 4.2、基于YOLOv2的喝水行為檢測 5.算法完整程序工程 1.算法運行效果圖預覽 2.算法運行軟件版本 matlab2022a 3.部分核心程序 clc; clear; close all; warning off;…

PPT思維導圖怎么做?這2個思維導圖工具墻裂推薦!

在日常學習和工作中&#xff0c;我們常常會面臨需要處理大量信息的情況&#xff0c;這時候&#xff0c;一種叫做思維導圖的工具可能會成為你的救星。 不同于傳統的線性記錄方式&#xff0c;思維導圖以其獨特的視覺表現力和結構化的信息處理方式&#xff0c;使得人們能夠更加有…

Flutter學習(四)如何取消listview的越界效果

背景 在flutter的開發過程中&#xff0c;ListView是很常見的一個組件&#xff0c;但是&#xff0c;由于ListView的某些自帶的體驗&#xff0c;導致不太好的用戶體驗。例如ListView中&#xff0c;滑動到頂部或者底部的時候&#xff0c;再次滑動&#xff0c;會有越界的效果&…

同步和異步

同步和異步是處理任務時的兩種不同方式。 同步是指一個進程在執行某個請求的時候&#xff0c;如果該請求需要一段時間才能返回信息&#xff0c;那么這個進程會一直等待下去&#xff0c;直到收到返回信息才繼續執行下去。這種方式下&#xff0c;任務是按照順序一個一個執行的&am…

2023年亞太地區數學建模大賽 問題A

采果機器人的圖像識別技術 中國是世界上最大的蘋果生產國&#xff0c;年產量約為3500萬噸。與此同時&#xff0c;中國也是世界上最大的蘋果出口國&#xff0c;全球每兩個蘋果中就有一個&#xff0c;全球超過六分之一的蘋果出口自中國。中國提出了一帶一路倡議&#xff08;BRI&…

Cache學習(2):Cache結構 命中與缺失 多級Cache結構 直接映射緩存

1 Cache名詞解釋 命中&#xff08;hit&#xff09;&#xff1a; CPU要訪問的數據在Cache中有緩存缺失&#xff08;miss&#xff09;&#xff1a; CPU要訪問的數據在Cache中沒有緩存Cache Size&#xff1a;Cache的大小&#xff0c;代表Cache可以緩存最大數據的大小Cache Line&a…

快速在WIN11中本地部署chatGLM3

具體請看智譜倉庫github&#xff1a;GitHub - THUDM/ChatGLM3: ChatGLM3 series: Open Bilingual Chat LLMs | 開源雙語對話語言模型 或者Huggingface:https://huggingface.co/THUDM/chatglm3-6b 1. 利用Anaconda建立一個虛擬環境&#xff1a; conda create -n chatglm3 pyt…

2023全球數字貿易創新大賽-人工智能元宇宙-11-12,數據二十條,數字產業化和產業數字化

目錄 長沙千博信息技術有限公司-手語翻譯 數據二十條 數字產業化和產業數字化

java--static的應用知識:單例設計模式

1.什么是設計模式(Design pattern) ①一個問題通常有n中解法&#xff0c;其中肯定有一種解法最優的&#xff0c;這個最優的解法被人總結出來了&#xff0c;稱之為設計模式。 ②設計模式有20多種&#xff0c;對應20多種軟件開發中會遇到的問題。 2.單例設計模式 確保一個類只…

Linux安裝與配置Maven

案例中Linux版本為CentOS7.9&#xff0c;安裝目錄為 /root/software/ 1、使用 wget 命令從官網下載安裝包&#xff08;https://maven.apache.org/download.cgi&#xff09; wget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz2、解壓…

搭建線上jvm監控

這里寫目錄標題 Springboot項目配置maven依賴application.properties添加監控JVM的配置類啟動springboot項目 Prometheus配置配置grafana Springboot項目配置 maven依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring…

mac mysql連接中斷重新啟動辦法

遇到如圖所示問題&#xff0c;可以用下面的命令重啟mysql服務 sudo /usr/local/mysql/support-files/mysql.server start

詳解分布式微服務架構

目錄 一、微服務簡介 1、分布式微服務架的誕生 2、微服務架構與SOA架構的區別 3、微服務框架引來的問題 二、服務通信 RESTful API&#xff1a; 消息隊列&#xff08;如RabbitMQ、Kafka&#xff09;&#xff1a; gRPC&#xff1a; GraphQL&#xff1a; Service Mesh&…

Vector - CANoe - Vector Hardware Manager以太網

前面的文章中有介紹過基于Network based mode和channel base mode的環境配置&#xff0c;不過我們都是使用比較舊的辦法&#xff0c;在我使用了一段時間Vector Hardware Manager配置之后發現這個更加好用結合之前的配置方法&#xff0c;使用起來也更加的靈活&#xff0c;今天就…

HTML的學習

知己知彼百戰不殆 打算學習一下javascript 所以先從基礎的html語言開始 其實就是頭部 和身體 頭部控制整個 html的語言 title等 <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"width…

61 權限提升-RedisPostgre令牌竊取進程注入

目錄 演示案例:Redis數據庫權限提升-計劃任務PostgreSQL數據庫權限提升Windows2008&7令牌竊取提升-本地Windows2003&10進程注入提升-本地pinjector進程注入工具針對-win2008以前操作系統pexec64 32進程注入工具針對-win2008及后操作系統- (佛系) 涉及資源: postgersql是…

面試送分題!“商品分類瀏覽”如何測試?

電商項目無論是工作中&#xff0c;還是面試中&#xff0c;都是一個高頻出現的詞。 面試官非常熱衷提問關于電商項目的問題。例如商品分類怎么測試&#xff1f;購物車怎么測試&#xff1f;訂單怎么測試&#xff1f;優惠券怎么測試&#xff1f;支付怎么測試&#xff1f;等等。 …