实现数据库查询用户,获取密码,编写了单元测试类
This commit is contained in:
12
src/main/java/com/fjy/spring/Application.java
Normal file
12
src/main/java/com/fjy/spring/Application.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.fjy.spring;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
29
src/main/java/com/fjy/spring/controller/LoginController.java
Normal file
29
src/main/java/com/fjy/spring/controller/LoginController.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public String toLoginPage(){
|
||||
return "login";
|
||||
}
|
||||
|
||||
@PostMapping("/login/dologin")
|
||||
public boolean doLogin(TbUser tbUser){
|
||||
if (userService.doLoginService(tbUser.getColname(),tbUser.getColpassword())){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
35
src/main/java/com/fjy/spring/controller/TestController.java
Normal file
35
src/main/java/com/fjy/spring/controller/TestController.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.repository.TbUserRepository;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@Autowired
|
||||
private TbUserRepository tbUserRepository;
|
||||
|
||||
@GetMapping("/test")
|
||||
public String TestHello(){
|
||||
return "Hello Spring";
|
||||
}
|
||||
|
||||
@GetMapping("/test/name/{name}")
|
||||
public String TestDataBase(@PathVariable("name") String name){
|
||||
//TbUser user = (TbUser)tbUserRepository.findById(id).get();
|
||||
TbUser user = (TbUser)tbUserRepository.findByColname(name).get();
|
||||
return user.getColpassword();
|
||||
}
|
||||
|
||||
@GetMapping("/test/id/{id}")
|
||||
public String TestDataBaseId(@PathVariable("id") Integer id){
|
||||
TbUser user = (TbUser)tbUserRepository.findById(id).get();
|
||||
return user.toString();
|
||||
}
|
||||
}
|
||||
77
src/main/java/com/fjy/spring/domain/TbUser.java
Normal file
77
src/main/java/com/fjy/spring/domain/TbUser.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package com.fjy.spring.domain;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class TbUser {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer coluserid;
|
||||
|
||||
private String colname;
|
||||
private String colpassword;
|
||||
private String colemail;
|
||||
private String colstudentno;
|
||||
private String colrealname;
|
||||
|
||||
public Integer getColuserid() {
|
||||
return coluserid;
|
||||
}
|
||||
|
||||
public void setColuserid(Integer coluserid) {
|
||||
this.coluserid = coluserid;
|
||||
}
|
||||
|
||||
public String getColname() {
|
||||
return colname;
|
||||
}
|
||||
|
||||
public void setColname(String colname) {
|
||||
this.colname = colname;
|
||||
}
|
||||
|
||||
public String getColpassword() {
|
||||
return colpassword;
|
||||
}
|
||||
|
||||
public void setColpassword(String colpassword) {
|
||||
this.colpassword = colpassword;
|
||||
}
|
||||
|
||||
public String getColemail() {
|
||||
return colemail;
|
||||
}
|
||||
|
||||
public void setColemail(String colemail) {
|
||||
this.colemail = colemail;
|
||||
}
|
||||
|
||||
public String getColstudentno() {
|
||||
return colstudentno;
|
||||
}
|
||||
|
||||
public void setColstudentno(String colstudentno) {
|
||||
this.colstudentno = colstudentno;
|
||||
}
|
||||
|
||||
public String getColrealname() {
|
||||
return colrealname;
|
||||
}
|
||||
|
||||
public void setColrealname(String colrealname) {
|
||||
this.colrealname = colrealname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TbUser{" +
|
||||
"coluserid=" + coluserid +
|
||||
", colname='" + colname + '\'' +
|
||||
", colpassword='" + colpassword + '\'' +
|
||||
", colemail='" + colemail + '\'' +
|
||||
", colstudentno='" + colstudentno + '\'' +
|
||||
", colrealname='" + colrealname + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.fjy.spring.repository;
|
||||
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TbUserRepository extends JpaRepository<TbUser,Integer> {
|
||||
|
||||
public Optional<TbUser> findByColname(String name);
|
||||
|
||||
}
|
||||
22
src/main/java/com/fjy/spring/service/UserService.java
Normal file
22
src/main/java/com/fjy/spring/service/UserService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.fjy.spring.service;
|
||||
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.repository.TbUserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private TbUserRepository tbUserRepository;
|
||||
|
||||
|
||||
public boolean doLoginService(String name,String password){
|
||||
TbUser user = (TbUser)tbUserRepository.findByColname(name).get();
|
||||
if (password.equals(user.getColpassword())){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user