如何用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層
填入下列代碼
如果有包未導入
直接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包
總結
以上是生活随笔為你收集整理的如何用spring boot写一个注册页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吃鸡电脑配置要求最低的?
- 下一篇: IDEA创建包不是树形