learn02
This commit is contained in:
parent
a63967227a
commit
addf9b30f6
@ -95,7 +95,10 @@
|
|||||||
<artifactId>fastjson2</artifactId>
|
<artifactId>fastjson2</artifactId>
|
||||||
<version>2.0.53</version>
|
<version>2.0.53</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
21
test05/src/main/java/test05/aop/TimeAspect.java
Normal file
21
test05/src/main/java/test05/aop/TimeAspect.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package test05.aop;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@Aspect
|
||||||
|
public class TimeAspect {
|
||||||
|
@Around("execution(* test05.service.*.*(..))")
|
||||||
|
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
long begin = System.currentTimeMillis();
|
||||||
|
Object result = joinPoint.proceed();
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
log.info(joinPoint.getSignature()+"耗时:{}ms", end-begin);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,6 +18,6 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
|
registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**").excludePathPatterns("/userAction/login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package test05.controller;
|
package test05.controller;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Jwt;
|
||||||
import jakarta.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
@ -8,6 +9,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
import test05.pojo.Result;
|
import test05.pojo.Result;
|
||||||
import test05.pojo.User;
|
import test05.pojo.User;
|
||||||
import test05.service.UserService;
|
import test05.service.UserService;
|
||||||
|
import test05.utils.JwtUtils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/userAction")
|
@RequestMapping("/userAction")
|
||||||
@ -23,8 +27,12 @@ public class LoginController {
|
|||||||
@RequestMapping("/login")
|
@RequestMapping("/login")
|
||||||
public Result login(String username, String password, HttpSession session) {
|
public Result login(String username, String password, HttpSession session) {
|
||||||
if (session.getAttribute("user") != null) {
|
if (session.getAttribute("user") != null) {
|
||||||
|
String loginName = ((Map<String, String>)(JwtUtils.parseJwt((String)session.getAttribute("user")).get("data"))).get("name");
|
||||||
|
if (loginName.equals(username)) {
|
||||||
return Result.success(session.getAttribute("user"));
|
return Result.success(session.getAttribute("user"));
|
||||||
}else if (username==null || password==null) {
|
}
|
||||||
|
}
|
||||||
|
if (username==null || password==null) {
|
||||||
return Result.error("login fail");
|
return Result.error("login fail");
|
||||||
}
|
}
|
||||||
Result res = userService.login(new User(username, password));
|
Result res = userService.login(new User(username, password));
|
||||||
@ -34,4 +42,11 @@ public class LoginController {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@RequestMapping("/logout")
|
||||||
|
public Result logout(HttpSession session) {
|
||||||
|
session.removeAttribute("user");
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,16 @@
|
|||||||
package test05.controller;
|
package test05.controller;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import test05.pojo.Result;
|
import test05.pojo.Result;
|
||||||
|
import test05.pojo.Student;
|
||||||
import test05.service.StudentService;
|
import test05.service.StudentService;
|
||||||
|
|
||||||
|
@CrossOrigin(maxAge = 3600)
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/studentAction/")
|
@RequestMapping("/studentAction")
|
||||||
public class studentController {
|
public class studentController {
|
||||||
private final StudentService studentService;
|
private final StudentService studentService;
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -15,10 +18,40 @@ public class studentController {
|
|||||||
this.studentService = studentService;
|
this.studentService = studentService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("get")
|
@RequestMapping("/get")
|
||||||
public Result get() {
|
public Result get(Student student, Integer page, Integer pageSize) {
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
|
if (pageSize == null) {
|
||||||
|
pageSize = 10;
|
||||||
|
}
|
||||||
|
if (page == null) {
|
||||||
|
page = 1;
|
||||||
|
}
|
||||||
|
return studentService.getStudent(student, page, pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
@RequestMapping("/add")
|
||||||
|
public Result add(Student student) {
|
||||||
|
if (student == null || student.getId() != -1) {
|
||||||
|
return Result.error("bad request");
|
||||||
|
}else{
|
||||||
|
return studentService.addStudent(student);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/edit")
|
||||||
|
public Result edit(Student student) {
|
||||||
|
if (student == null || student.getId() == -1) {
|
||||||
|
return Result.error("bad request");
|
||||||
|
}
|
||||||
|
return studentService.updateStudent(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/delete")
|
||||||
|
public Result delete(Student student) {
|
||||||
|
if (student == null || student.getId() <= 0) {
|
||||||
|
return Result.error("bad request");
|
||||||
|
}
|
||||||
|
return studentService.deleteStudent(student);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,14 @@
|
|||||||
|
package test05.exception;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import test05.pojo.Result;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public Result ex(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return Result.error("server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -31,16 +31,22 @@ public class StudentServiceImpl implements StudentService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result addStudent(Student student) {
|
public Result addStudent(Student student) {
|
||||||
return null;
|
|
||||||
|
//TODO:校验数据
|
||||||
|
|
||||||
|
int count = studentMapper.insertStudent(student);
|
||||||
|
return Result.success(count>0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result updateStudent(Student student) {
|
public Result updateStudent(Student student) {
|
||||||
return null;
|
int count = studentMapper.updateStudent(student);
|
||||||
|
return Result.success(count>0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result deleteStudent(Student student) {
|
public Result deleteStudent(Student student) {
|
||||||
return null;
|
int count = studentMapper.deleteStudent(student);
|
||||||
|
return Result.success(count>0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,17 @@
|
|||||||
ORDER BY N_STUDENT_ID DESC
|
ORDER BY N_STUDENT_ID DESC
|
||||||
LIMIT (#{page}-1)*#{pageSize},#{pageSize}
|
LIMIT (#{page}-1)*#{pageSize},#{pageSize}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="selectStudentCount" resultType="int">
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM t_student
|
||||||
|
WHERE #{id} in (N_STUDENT_ID, -1)
|
||||||
|
AND VC_STUDENT_CODE LIKE CONCAT('%', #{code}, '%')
|
||||||
|
AND VC_STUDENT_NAME LIKE CONCAT('%', #{name}, '%')
|
||||||
|
AND #{sex} in (N_SEX, -1)
|
||||||
|
AND #{grade} in (N_GRADE, -1)
|
||||||
|
AND #{major} in (N_MAJOR, -1)
|
||||||
|
AND VC_DETAIL LIKE CONCAT('%', #{detail}, '%')
|
||||||
|
</select>
|
||||||
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
|
||||||
INSERT INTO t_student
|
INSERT INTO t_student
|
||||||
(VC_STUDENT_CODE, VC_STUDENT_NAME, N_SEX, N_GRADE, N_MAJOR, VC_DETAIL)
|
(VC_STUDENT_CODE, VC_STUDENT_NAME, N_SEX, N_GRADE, N_MAJOR, VC_DETAIL)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user