spring boot 實現最基礎的登陸註冊功能。
阿新 • • 發佈:2018-11-11
spring boot 實現最基礎的登陸註冊功能:
首先先看程式碼:資料庫:
先說自己遇到的坑:第一:資料庫的配置是複製之前的資料庫,導致自己在另一個數據庫中查詢資料,這點大家在application.properties這個裡面注意下 這句
spring.datasource.url=jdbc:mysql://localhost:3306/testspring?useUnicode=true&characterEncoding=utf8&useSSL=false
第二:在註冊頁面開始自己設定為無返回的,導致自己頁面無法跳轉。
第三:在Repository裡注意資料庫查詢的格式。
controller 層:
package com.example.demo222.controller; import com.example.demo222.entity.Login; import com.example.demo222.service.LoginService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.List; @RestController //@Controller public class MyController { @Autowired LoginService loginService; ////這裡是首先進入登陸頁面,然後再進行其他的操作 @RequestMapping("/") public ModelAndView index(){ ModelAndView mv = new ModelAndView("login"); return mv; } @RequestMapping("/registers") public ModelAndView zhuce(){ ModelAndView mv = new ModelAndView("register"); return mv; } // @PostMapping("/login") // public ModelAndView login(@RequestParam("username")String username, @RequestParam("password") String password){ // ModelAndView success = new ModelAndView(); // if(username.equals("admin")&&password.equals("123")) // success.setViewName("success"); // else success.setViewName("404"); // return success; // } // @GetMapping("/login") // public ModelAndView login(@RequestParam("name")String name, @RequestParam("password") String password){ // ModelAndView success = new ModelAndView(); // if(name.equals("admin")&&password.equals("123")) // success.setViewName("success"); // else success.setViewName("404"); // return success; // } //登陸功能 @PostMapping("/login") public ModelAndView Login(@RequestParam("username") String username,@RequestParam("userpassword") String userpassword){ List<Login> login= loginService.findByUsernameAndUserpassword(username,userpassword); // for (Login l:loginService.findAll()) { //// System.out.println(l.getUsername()+":"+l.getUserpassword()); //// } System.out.println(username+":"+userpassword); //System.out.println(login); ModelAndView success = new ModelAndView(); if(login.size()>0){ success.setViewName("success"); } else { success.setViewName("404"); } return success; } //註冊功能 @PostMapping("/register") public ModelAndView register(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("password2") String password2){ ModelAndView success = new ModelAndView(); //使用者或密碼為空的條件判斷 if(username.isEmpty()||password.isEmpty()||password2.isEmpty()){ success.setViewName("register"); success.addObject("tip1","使用者或密碼不能為空"); return success; } //兩次密碼不一樣的判斷條件 if(!password.equals(password2)){ success.setViewName("register"); success.addObject("tip2","兩次密碼不一樣"); return success; } //判斷是否取到使用者,如果沒有就儲存在資料庫中 List<Login> us=loginService.findByUsername(username); if(us.size()==0){ //List<Login> register=loginService.save(username,password); Login registers=new Login(); registers.setUserpassword(password); registers.setUsername(username); loginService.save(registers); success.setViewName("success"); } else { success.setViewName("404"); } return success; } } 然後是 respository層:
package com.example.demo222.repository; import com.example.demo222.entity.Login; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Component; import java.util.List; @Component public interface MyRepository extends JpaRepository<Login,Integer>, JpaSpecificationExecutor<Login>{ //@Query(value = "select * from book b where b.name=?1", nativeQuery = true) //@Query(value ="select l.id,l.username,l.userpassword from Login l where l.username=:name and l.userpassword=:password") //登陸 @Query(value = "select * from login where username=?1 and userpassword=?2",nativeQuery = true) List<Login> findByUsernameAndUserpassword(String username, String userpassword); List<Login> findAll(); //註冊 @Query(value = "select * from login where username=?1", nativeQuery = true) List<Login> findByUsername(String username); }
接下來是實體類:
package com.example.demo222.entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class Login {
private String username;
private String userpassword;
private int id;
@Basic
@Column(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "userpassword")
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Login login = (Login) o;
return Objects.equals(username, login.username) &&
Objects.equals(userpassword, login.userpassword);
}
@Override
public int hashCode() {
return Objects.hash(username, userpassword);
}
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
service層:
下來是部分前臺頁面:
Thymeleaf表單繫結Spring的Model物件中的Attribute
整個專案原始碼: https://download.csdn.net/download/qq_40979622/10720191
大家有啥意見或者建議可以和我討論[email protected]