实现数据库查询用户,获取密码,编写了单元测试类

This commit is contained in:
F嘉阳
2018-01-30 15:47:30 +08:00
commit 91aba6a592
21 changed files with 818 additions and 0 deletions

View 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);
}
}

View 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;
}
}

View 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();
}
}

View 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 + '\'' +
'}';
}
}

View File

@@ -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);
}

View 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;
}
}