mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-WebAPi.git
synced 2026-04-01 23:14:51 +00:00
feature: 新增demo以学习用
refactor(api): 重构api的mapper为xml
This commit is contained in:
parent
de2c8412dc
commit
cf8403e5ed
12 changed files with 128 additions and 3 deletions
|
|
@ -13,6 +13,7 @@ public class WebapiApplication {
|
||||||
String home(){
|
String home(){
|
||||||
return "Welcome to Cloverta's WebAPI✨, please visit <a href='https://cloverta.top'>https://cloverta.top</a> for more information.";
|
return "Welcome to Cloverta's WebAPI✨, please visit <a href='https://cloverta.top'>https://cloverta.top</a> for more information.";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(WebapiApplication.class, args);
|
SpringApplication.run(WebapiApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.cloverta.webapi.controller;
|
||||||
|
|
||||||
|
import com.cloverta.webapi.mapper.StudentMapper;
|
||||||
|
import com.cloverta.webapi.model.Student;
|
||||||
|
import com.cloverta.webapi.service.StudentService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/demo")
|
||||||
|
public class DemoController {
|
||||||
|
private StudentService studentService;
|
||||||
|
@Autowired
|
||||||
|
public void setStudentService(StudentService studentService) {
|
||||||
|
this.studentService = studentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/fetch")
|
||||||
|
public List<Student> fetch() {
|
||||||
|
return studentService.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,10 +5,12 @@ import com.cloverta.webapi.restservice.GithubStars;
|
||||||
import com.cloverta.webapi.service.GithubStarsService;
|
import com.cloverta.webapi.service.GithubStarsService;
|
||||||
import com.cloverta.webapi.service.GreetingService;
|
import com.cloverta.webapi.service.GreetingService;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/github")
|
||||||
public class GithubStarsController {
|
public class GithubStarsController {
|
||||||
|
|
||||||
private final GithubStarsService githubStarsService;
|
private final GithubStarsService githubStarsService;
|
||||||
|
|
@ -17,7 +19,7 @@ public class GithubStarsController {
|
||||||
this.githubStarsService = githubStarsService;
|
this.githubStarsService = githubStarsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/github/stars")
|
@GetMapping("/stars")
|
||||||
public GithubStars getGithubStars(@RequestParam(value = "user", required = true) String user) {
|
public GithubStars getGithubStars(@RequestParam(value = "user", required = true) String user) {
|
||||||
try {
|
try {
|
||||||
return githubStarsService.getGithubStars(user);
|
return githubStarsService.getGithubStars(user);
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ public class GreetingController {
|
||||||
|
|
||||||
private final GreetingService greetingService;
|
private final GreetingService greetingService;
|
||||||
|
|
||||||
// 显式声明构造方法(Spring 4.3+ 可省略 @Autowired)
|
|
||||||
public GreetingController(GreetingService greetingService) {
|
public GreetingController(GreetingService greetingService) {
|
||||||
this.greetingService = greetingService;
|
this.greetingService = greetingService;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.cloverta.webapi.handler;
|
||||||
|
|
||||||
import com.cloverta.webapi.exception.GithubException;
|
import com.cloverta.webapi.exception.GithubException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
|
@ -12,11 +13,13 @@ public class GlobalExceptionHandler {
|
||||||
@ExceptionHandler(GithubException.class)
|
@ExceptionHandler(GithubException.class)
|
||||||
public String handleGithubException(GithubException ex, HttpServletRequest request) {
|
public String handleGithubException(GithubException ex, HttpServletRequest request) {
|
||||||
request.setAttribute("javax.servlet.error.message", ex.getMessage());
|
request.setAttribute("javax.servlet.error.message", ex.getMessage());
|
||||||
|
request.setAttribute("javax.servlet.error.status_code", HttpStatus.BAD_REQUEST.value());
|
||||||
return "forward:/error";
|
return "forward:/error";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public String handleGeneralException(Exception ex, HttpServletRequest request) {
|
public String handleGeneralException(Exception ex, HttpServletRequest request) {
|
||||||
|
request.setAttribute("javax.servlet.error.status_code", HttpStatus.BAD_REQUEST.value());
|
||||||
request.setAttribute("javax.servlet.error.message", ex.getMessage());
|
request.setAttribute("javax.servlet.error.message", ex.getMessage());
|
||||||
return "forward:/error";
|
return "forward:/error";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import java.util.List;
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ApiMapper {
|
public interface ApiMapper {
|
||||||
|
|
||||||
@Select("SELECT * FROM api_list")
|
|
||||||
List<Api> findAll();
|
List<Api> findAll();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/main/java/com/cloverta/webapi/mapper/StudentMapper.java
Normal file
12
src/main/java/com/cloverta/webapi/mapper/StudentMapper.java
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.cloverta.webapi.mapper;
|
||||||
|
|
||||||
|
import com.cloverta.webapi.model.Student;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface StudentMapper {
|
||||||
|
|
||||||
|
List<Student> findAll();
|
||||||
|
}
|
||||||
41
src/main/java/com/cloverta/webapi/model/Student.java
Normal file
41
src/main/java/com/cloverta/webapi/model/Student.java
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.cloverta.webapi.model;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
public String id;
|
||||||
|
public String name;
|
||||||
|
public String email;
|
||||||
|
public String phone;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPhone() {
|
||||||
|
return phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhone(String phone) {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.cloverta.webapi.restservice;
|
||||||
|
|
||||||
|
public record Student(String id, String name, String phone, String email) {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.cloverta.webapi.service;
|
||||||
|
|
||||||
|
import com.cloverta.webapi.mapper.StudentMapper;
|
||||||
|
import com.cloverta.webapi.model.Student;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StudentService {
|
||||||
|
private final StudentMapper studentMapper;
|
||||||
|
public StudentService(StudentMapper studentMapper) {
|
||||||
|
this.studentMapper = studentMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Student> findAll() {
|
||||||
|
return studentMapper.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/main/resources/mapper/ApiMapper.xml
Normal file
9
src/main/resources/mapper/ApiMapper.xml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.cloverta.webapi.mapper.ApiMapper">
|
||||||
|
<select id="findAll" resultType="Api">
|
||||||
|
SELECT * FROM api_list;
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
9
src/main/resources/mapper/StudentMapper.xml
Normal file
9
src/main/resources/mapper/StudentMapper.xml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.cloverta.webapi.mapper.StudentMapper">
|
||||||
|
<select id="findAll" resultType="Student">
|
||||||
|
SELECT * FROM student;
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
Loading…
Reference in a new issue