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

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

View File

@@ -0,0 +1,19 @@
#开发环境配置文件
server:
servlet:
context-path: /cms
port: 8080
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_imis2?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: imis2
password: 2015imis2
jpa:
hibernate:
ddl-auto: update
show-sql: true
debug: true

View File

View File

@@ -0,0 +1,4 @@
#控制配置文件调用
spring:
profiles:
active: dev

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录系统</h1>
</body>
</html>

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录系统</h1>
</body>
</html>

View File

@@ -0,0 +1,16 @@
package com.fjy.spring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,28 @@
package com.fjy.spring.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class LoginControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void toLoginPage() throws Exception{
mvc.perform(MockMvcRequestBuilders.get("/index"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("/login"));
}
}

View File

@@ -0,0 +1,41 @@
package com.fjy.spring.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void testHello()throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/test"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Hello Spring"));
}
@Test
public void testDataBase()throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/test/name/root"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("root"));
}
@Test
public void testDataBaseId()throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/test/id/1"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}