日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何用spring boot写一个注册页面

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何用spring boot写一个注册页面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

環境準備:
java集成開發環境:IDEA
數據庫:Mysql
Maven

最好在安裝有個navicat(數據庫可視化界面)
安裝好上述幾個軟件后
總結下:五步

1、創建新的工程
2、創建建applicatiom.yml
3、創建entity層
4、創建respository層
5、創建Controller層

1、創建新的工程

給定工程名以及包名

2、選中web,jdbc,jpa,mysql四個包依賴


spring boot版本我選擇的是2.2.2

確定有一下四個后,點擊下一步next

2、創建建applicatiom.yml

原項目

新建后applicatiom.yml文件

填入下面配置代碼

spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/學生管理系統?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driverjpa:hibernate:ddl-auto: updateshow-sql: true

其中數據庫:“學生管理系統“需要你提前在數據庫創建好,你也可以改成你要連接的數據庫

3、創建entity層

在xzy.1998.springboot創建同目錄class文件

填入下列代碼

package xyz.k1998.springboot.entity;import javax.persistence.*;@Entity public class Student {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;@Columnprivate String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }

4、創建respository層


填入下列代碼

package xyz.k1998.springboot.entity.repository;import org.springframework.data.jpa.repository.JpaRepository; import xyz.k1998.springboot.entity.Student;public interface StudentRepository extends JpaRepository<Student,Integer> {}

如果有包未導入
直接Alt+Enter即可

第四步結束后主程序運行就可以直接創建一個數據庫的表單
字段什么都是Student的映射
如下圖

現在來一些簡單的增刪改查,利用controller

5、創建Controller層

這就是最后的項目結構了,比較一下是否一致

然后在StudentController里填入下列代碼

package pringboot.controller;import pringboot.entity.Student; import pringboot.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;@RestController public class StudentController {@AutowiredStudentRepository studentRepository;@GetMapping("/student/{id}")public Student getStudent(@PathVariable("id") Integer id){Student student = (Student) studentRepository.findById(id).get();return student;}@GetMapping("/student")public Student insertUser(Student student){Student save = studentRepository.save(student);return save;}}

這樣再次運行主程序,我們就可以向數據庫插入或查詢數據了

在網址輸入

http://localhost:8080/student?username=氪金&&password=123456

返回

在到navicat去刷新看看

就有了新的數據,且id自動給你添加,是不是很方便

查詢的話
輸入

后面填入你需要查的id號即可

看到這你就可以做一個注冊的頁面了
有沒有發現form表單提交和插入輸入的地址是一樣的

這樣我們做一個注冊的靜態頁面

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>注冊</title> </head> <body> <form action="/student"><input name="username"><br><input type="password" name="password"><br><input type="submit"></form></body> </html>


提交后,數據庫就多了一條數據

這樣注冊頁面就算大功告成了

后面寫登錄頁面
添加可跳轉頁面的thymeleaf包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

總結

以上是生活随笔為你收集整理的如何用spring boot写一个注册页面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。