实现数据库查询用户,获取密码,编写了单元测试类
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;
|
||||
}
|
||||
}
|
||||
19
src/main/resources/application-dev.yml
Normal file
19
src/main/resources/application-dev.yml
Normal 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
|
||||
0
src/main/resources/application-prod.yml
Normal file
0
src/main/resources/application-prod.yml
Normal file
0
src/main/resources/application.properties
Normal file
0
src/main/resources/application.properties
Normal file
4
src/main/resources/application.yml
Normal file
4
src/main/resources/application.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
#控制配置文件调用
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
10
src/main/resources/static/login.html
Normal file
10
src/main/resources/static/login.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登录</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>登录系统</h1>
|
||||
</body>
|
||||
</html>
|
||||
10
src/main/resources/templates/login.html
Normal file
10
src/main/resources/templates/login.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登录</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>登录系统</h1>
|
||||
</body>
|
||||
</html>
|
||||
16
src/test/java/com/fjy/spring/ApplicationTests.java
Normal file
16
src/test/java/com/fjy/spring/ApplicationTests.java
Normal 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() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user