postman可以很好的模拟浏览器并向API发送Request请求,所以一起来下载一下…
Postman下载地址
下载好后,注册账户,就可以开始用了,具体使用方法后面测试时讲
Jsp和serlvet是开发动态web的一门技术,特别擅长开发B/S架构的程序,jsp和html文件相似。但是,SpringBoot不推荐使用JSP,所以如果想在SpringBoot中使用JSP,需要自己做一些配置。
Servlet本质上就是Java类,但要遵循Servlet规范进行编写,它的创建、调用、销毁都由Servlet容器进行管理(如Tomcat,Jetty, WebLogic Server, JBoss等)。
在企业级Web项目开发中,标准的三层架构包括:表现层、业务层、数据访问层(持久层)。三层架构中,每一层各司其职,其中:
MVC
@Controller
//作用:修饰类,一个类被它修饰,就成了控制器类,负责接收和处理 HTTP 请求,可以返回页面和数据;
@RestController (@Controller+@ResponseBody 的组合注解)
//作用:修饰类,一个类被它修饰,就成了控制器类,只返回给用户数据,默认将返回的对象数据转换为 json 格式
@RequestMapping
//作用:负责 URL 的路由映射,建立请求 URL 和处理请求方法之间的对应关系,可以修饰类或类中的方法
@RequestParam
//作用:将请求参数绑定到控制器的方法参数上,接收的参数来自http请求体或请求的url的QueryString
@RequestBody
//该注解主要用来接收前端传递给后端的 json 字符串中的数据(请求体中的数据的)。
@PathVaraible
//作用:处理动态URL,URL的值可以作为控制器中处理方法的参数,主要用于RestFul风格中
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myschool?serverTimezone=Hongkong?characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 密码
mybatis:
mapper-locations: classpath:com/exmaple/mapper/*.xml #指定sql配置文件的位置
type-aliases-package: com.example.pojo #指定实体类所在的包名
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出SQL命令
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>week11_20201000000</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>week11_20201000000</name>
<description>week11_20201000000</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml
src/main/resources
**.*
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
package com.example.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor //自动生成无参构造函数
@AllArgsConstructor
public class Student {
private Long id;
private String sname;
private String dept;
private int age;
}
package com.example.mapper;
import com.example.pojo.Student;
import java.util.List;
import java.util.Map;
public interface StudentMapper {
//1
public List<Student> getAllStudentMap();
//2
public Student getStudentById(Long id);
//3更新用户信息
public int updateStudentByDynam(Map<Object,Object> mp);
//4
public int addStudent(Student student);
//5
public int deleteStudentById(Long id);
}
<?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.example.mapper.StudentMapper">
<!--1 -->
<select id="getAllStudentMap" resultType="Student">
SELECT * FROM student
</select>
<!--2 -->
<select id="getStudentById" resultType="Student" parameterType="Long">
SELECT * FROM student WHERE id=#{0}
</select>
<!--3 -->
<update id="updateStudentByDynam" parameterType="Map">
update student
<set>
<if test="sname!=null">
sname=#{sname},
</if>
<if test="dept!=null">
dept=#{dept},
</if>
<if test="age!=null">
age=#{age},
</if>
id=#{id}
</set>
where id=#{id}
</update>
<!--4 -->
<insert id="addStudent" parameterType="Student">
INSERT INTO student SET sname=#{sname},dept=#{dept},age=#{age}
</insert>
<!--5 -->
<delete id="deleteStudentById" parameterType="Long">
DELETE FROM student
where id=#{id}
</delete>
</mapper>
package com.example.service;
import com.example.pojo.Student;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public interface StudentService {
//1
public List<Student> getAllStudentMap();
//2
public Student getStudentById(Long id);
//3更新用户信息
public int updateStudentByDynam(Map<Object,Object> mp);
//4
public int addStudent(Student student);
//5
public int deleteStudentById(Long id);
}
package com.example.service.impl;
import com.example.mapper.StudentMapper;
import com.example.pojo.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
//根据id查询
@Override
public Student getStudentById(Long id){
return studentMapper.getStudentById(id);
}
//根据id修改用户信息
@Override
public int updateStudentByDynam(Map<Object,Object> mp) {
return studentMapper.updateStudentByDynam(mp);
}
@Override
public int deleteStudentById(Long id){
return studentMapper.deleteStudentById(id);
}
//查询用户
public List<Student> getAllStudentMap() {
return studentMapper.getAllStudentMap();
}
public int addStudent(Student student) {
return studentMapper.addStudent(student);
}
}
package com.example.controller;
import com.example.pojo.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/getAllStudent",method = RequestMethod.GET)
public List<Student> getAllStudent(){
List<Student> studentList = studentService.getAllStudentMap();
System.out.println("test");
return studentList;
}
@RequestMapping(value = "/findStudentById",method =RequestMethod.GET)
public Student findStudentById(Long id) throws IOException {
Student studentById = studentService.getStudentById(id);
System.out.println(studentById);
return studentById;
}
@RequestMapping(value = "/addStudent",method = RequestMethod.POST)
public int addStudent(Student student) {
System.out.print(student);
int num=studentService.addStudent(student);
System.out.println(num);
return num;
}
@RequestMapping(value = "/updateStudentByDynam",method =RequestMethod.POST)
public int updateStudentByDynam(@RequestBody Map<Object,Object> mp){
System.out.println("mp: ");
System.out.println(mp);
int row = studentService.updateStudentByDynam(mp);
System.out.println(row);
return row;
}
@RequestMapping(value = "/deleteStudentById",method =RequestMethod.POST)
public int deleteStudentById(Long id){
System.out.println("id: "+id);
return studentService.deleteStudentById(id);
}
}
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages ="com.example.mapper")
public class Week1120201000000Application {
public static void main(String[] args) {
SpringApplication.run(Week1120201000000Application.class, args);
}
}
选择+输入+Key
点击Send
查看运行结果
用getAllStudent()查一下
{
"id":20201005591,
"sname": "updatetest",
"dept": "updatetest"
}
最后,部分代码参考自大佬的软件工程综合实践课程第十一周作业( SpringBoot整合Mybatis完成CRUD操作并使用接口调试工具对接口进行测试)