用SpringBoot、MyBatis搭建web项目,自定义学生表,并运用以下注解完成增删改查接口,并通过拦截器打印每个接口用时,@RequestMapping、@GetMapping、@PostMapping、@RequestParam、@RequestBody、@PathVariable,运用MyBatis动态标签<if>、<foreach>等完成判断查询、批量插入并获取自增主键id
时间: 2024-01-31 10:04:37 浏览: 90
可以使用Spring Boot和MyBatis搭建一个web项目,并自定义学生表,并运用注解完成增删改查接口。同时,通过拦截器来打印每个接口的用时。
首先,确保你已经配置好了Spring Boot和MyBatis的环境。然后,创建一个名为Student的实体类,包含学生的信息。
```java
public class Student {
private Long id;
private String name;
private Integer age;
// 省略getter和setter方法
}
```
接下来,创建一个名为StudentMapper的接口,用于定义数据库操作的方法。
```java
@Mapper
public interface StudentMapper {
@Insert("INSERT INTO student(name, age) VALUES(#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insert(Student student);
@Delete("DELETE FROM student WHERE id = #{id}")
int deleteById(Long id);
@Update("UPDATE student SET name = #{name}, age = #{age} WHERE id = #{id}")
int update(Student student);
@Select("SELECT * FROM student WHERE id = #{id}")
Student findById(Long id);
@Select("SELECT * FROM student")
List<Student> findAll();
}
```
然后,创建一个名为StudentController的控制器类,用于处理请求。
```java
@RestController
@RequestMapping("/students")
public class StudentController {
private final StudentMapper studentMapper;
public StudentController(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@PostMapping
public Long createStudent(@RequestBody Student student) {
studentMapper.insert(student);
return student.getId();
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable Long id) {
studentMapper.deleteById(id);
}
@PutMapping("/{id}")
public void updateStudent(@PathVariable Long id, @RequestBody Student student) {
student.setId(id);
studentMapper.update(student);
}
@GetMapping("/{id}")
public Student getStudent(@PathVariable Long id) {
return studentMapper.findById(id);
}
@GetMapping
public List<Student> getAllStudents() {
return studentMapper.findAll();
}
}
```
接下来,创建一个名为LoggingInterceptor的拦截器类,用于打印每个接口的用时。
```java
@Component
public class LoggingInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("startTime", System.currentTimeMillis());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
long startTime = (long) request.getAttribute("startTime");
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
String requestURI = request.getRequestURI();
System.out.println(requestURI + " executed in " + elapsedTime + "ms");
}
}
```
最后,配置拦截器和MyBatis动态标签。
```java
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final LoggingInterceptor loggingInterceptor;
public WebConfig(LoggingInterceptor loggingInterceptor) {
this.loggingInterceptor = loggingInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loggingInterceptor);
}
}
@MapperScan("com.example.mapper")
public class MyBatisConfig {
}
```
这样,你就完成了使用Spring Boot和MyBatis搭建web项目,并通过注解完成增删改查接口,并通过拦截器打印每个接口的用时。同时,使用MyBatis的动态标签可以完成判断查询和批量插入并获取自增主键id的功能。
阅读全文