文章目录
- 一、要求
- 二、知识总结
- 三、项目结构
- 四、完整参考代码
- 五、接口测试设计及运行结果
- StudentController.java中的对应函数接口测试
- getAllStudent()
- getStudentById(Long id)
- getStudentByMulCondition(@RequestBody Map<Object,Object> mp)
- getStudentByDynam(@RequestBody Map<Object,Object> mp)
- addStudent_1(Student student)
- addStudent_2(@RequestBody Student student)
- addStudentBatch(@RequestBody List<Student> student)
- updateStudentByDynam(@RequestBody Map<Object,Object> mp)
- deleteStudentById(Long id)
- deleteStudentByIds(@RequestBody Long[] ids)
一、要求
在SpringBoot 中使用 Mybatis ,并完成以下操作:
◼ 创建 一个SpringBoot 项目,项目名 ” week11_
学号 ” ;
◼ 使用 Mybatis 框架 ,也可以用MyBatisPlus 框架 ;
◼ 访问 myschool 数据库;
◼ 对student 表进行操作,向 student 插入自己的一条记录, 自行设计完成CRUD操作;
◼ 创建相应的Pojo层、Mapper层、Service 层、 Controller层;
◼ 在各层中,创建针对student 表操作的类或接口;
◼对studentController 设置一级和二级对外访问虚拟路径;
◼ 启动服务器, 测试控制器中的方法,直接将CRUD操作结果在后台打印出来;
◼ 将运行结果截图,保存在word 文档中
二、知识总结
常用注解作用解释
@Autowired
该注解加在类成员变量上,表示让 Spring 完成 Bean 自动装配(对象注入)的工作。
@Service
该注解用于service实现类上,标记当前类是一个Service类,同时该类会被Spring框架管理
@Controller
- SpringMVC中,只需要在一个类的前面加上"@Controller"注解,该类就变成了一个控制器类,也成为一个Servlet类,不需要再使用继承HttpServlet基类的方式了;
- 一个控制器类里,可以实现多个方法;
- 作用:修饰类,一个类被它修饰,就成了控制器类,负责接收和处理 HTTP 请求,
可以返回页面和数据
@RestController
@Controller+@ResponseBody 的组合注解
作用:修饰类,一个类被它修饰,就成了控制器类(@Controller),只返回给用户数据,默认将返回的对象数据转换为 json 格式(@ResponseBody);
@RequestMapping
- 该注解可以定义一个方法的对外访问路径,处理get请求还是处理post请求,默认是两种请求都支持;
- RequestMapping可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径,形成多级访问路径。如http://localhost:8080/Count/add
- 一个控制器方法可以配置多个对外访问路径
- 作用:负责 URL 的路由映射,建立请求 URL 和处理请求方法之间的对应关系,可以修饰类或类中的方法。
- 常用属性参数,定义 HTTP 的请求映射规则
– Value :请求的 URL 路径,可以使用通配符
– Method: HTTP 请求方法,如 GET,POST - 该注解加在类上时:
– 作为请求 URL 的第一级访问目录;
– 可以按照模块化管理 URL 地址:
— 例如: 账户模块:
/account/add
/account/update
/account/delete …
订单模块:
/order/add
/order/update
/order/delete - 该注解加在方法上时, 映射每个方法的路由
– 请求 URL 的第二级访问目录
– value:用于指定请求的 URL
– method:用于指定请求的方式
@GetMapping 等 RestFul 风格的注解
@GetMapping,@PostMapping 等,来代替 @RequestMapping 注解
如@PostMapping(value = "/updateStudentByDynam")
相当于
@RequestMapping(value = "/updateStudentByDynam",method =RequestMethod.POST)
页面向控制器传递参数的注解
@RequestParam
- 作用:将请求参数绑定到控制器的方法参数上,接收的参数来自http请求体或请求的url的QueryString;
- 当请求的参数名称与Controller的业务方法参数名称一致时,该注解可以省略,当两边参数名不一致时,需要使用该注解可以实现参数间的映射;
@RequestBody
该注解主要用来接收前端传递给后端的 json 字符串中的数据(请求体中的数据的)。一般用来处理“application/json”“application/xml”等类型的数据,使用@RequestBody 接收数据时,一般都用 POST 方式进行提交。
- 应 用1 : 用于获取请求体内容。直接使用得到是 key=value&key=value…键值对结构的数据。
- 应用2:前端页面使用 ajax 技术,将数据封装成 json 格式进行传递,控制器方法中使用该注解将 json 数据转成实体类来接收。
– 注:Ajax 即“A synchronous J avascript A nd X ML ”(异步 JavaScript 和XML),是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部分网页的技术。
@PathVaraible
作用:处理动态URL,URL的值可以作为控制器中处理方法的参数,主要用于RestFul风格中
@RequestBody @RequestParam @PathVaraible 三个注解的区别![在这里插入图片描述](https://img-blog.csdnimg.cn/750b17497e804e41b1e5cfe8742dde9c.png)
页面向控制器传递参数
SpringMVC 中,页面可以向控制器传递以下多种类型的参数:
- 基本类型和 String 类型
- POJO 类型参数,即实体类
- 数组,集合类型参数
- json 格式数据
控制器向页面传递参数
Spring MVC 中,控制器可以向页面:
- 返回字符串作为视图名称,进行页面跳转
- 不返回参数,直接调用业务层的类
- 返回视图和数据
- 向页面返回 json 格式的数据
接口测试工具的基本使用方法
接口调试工具有很多种,功能上基本相同,具体的选择看个人喜好
我在测试时用的是Apipost(官网链接:https://www.apipost.cn/)在官网下载解压安装后就可直接使用
接口测试工具概述
对于一个API来说,输入的请求(Request)包括 URL、method、Request Cookies、Request Headers和Request Body;收到请求后,API会回复响应(Response),包括Response Headers和Response Body。接口测试工具可以很好的 模拟浏览器并向API发送Request请求,同时可以接收API发回的Response响应。接口测试工具能覆盖绝大多数HTTP接口测试场景,方便开发人员在开发过程中去调试接口。
接口测试工具基本用法
测试接口时,需要选择请求方法,填入URL地址等,具体见下图:
(1 ) get 请求的测试及返回值:
(2)post 请求的测试及返回值界面
(3)使用 ajax 进行 post 请求的测试及返回值界面
(4)put请求的使用及返回值界面
(5)以下是delete请求的使用及返回值界面
具体的接口测试设计及运行结果在运行结果
注意点及经验分享
Lombok插件安装
如果没有安装过Lombok插件的话需要现在IDEA中安装该插件:
Lombok 是一种 Java 常用工具,可以使它来简化实体类的代码。它通过注解来自动生成属性的get,set和构造方法,具体如下图所示。
创建SpringBoot项目
1、使用Spring Initializr新建一个SpringBoot项目
打开File–New–Project
如图进行对应设置:
注意这里的Server URL默认为start.spring.io,如果这个用不了的话可以尝试点击右边的齿轮修改为https://start.springboot.io
2、配置所需依赖
点击next后在该页面选择如图所示的依赖进行添加:
3、点击finish创建SpringBoot项目
4、参考“三、项目结构”部分内容创建对应的包和类即可
mapper文件找不到问题的解决
关于mapper文件的配置上如果有问题的话,在项目运行时可能会报类似Invalid bound statement (not found): com.example.mapper.....
的错误
解决方法:
1、检查springboot的…application.java中是否有加@MapperScan(basePackages = "com.example.mapper")
注解
2、检查…Mapper.java文件是否有@Mapper注解
3、检查配置文件(application.yml)中是否有
mybatis:
mapper-locations: classpath:com/exmaple/mapper/*.xml #指定sql配置文件的位置
4、检查pom.xml文件中<build>标签中是否有配这个resources
<!-- 将java目录下的xml文件打包-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
</includes>
</resource>
</resources>
接口测试时不同的接口参数对应的json格式测试数据参考
Get方法Long类型
如:
可使用这两种方法:
1、在url中直接传参:localhost:8080/student/getStudentById?id=3
2、url为localhost:8080/student/getStudentById,在form-data中设置id ,3
Post方法Map类型
如:
使用json格式数据传参:
如:
{
"sname": "test0",
"dept": "test666",
"age":0
}
数据库对象数据类型
如:
也是使用json格式数据传参
List集合类型
如:
要使用如下格式:
注意最外边是中括号[ ]
[
{
"sname": "testBatch1",
"dept": "testBatch1",
"age":6
},
{
"sname": "testBatch2",
"dept": "testBatch2",
"age":7
}
]
POST方法Long类型
如:
1、若不加@RequestBody的话需要使用form-data提交
2、若加上@RequestBody的话需要使用raw中写数字提交(啥括号都不用,只用一个数字)
POST方法数组类型
如:
使用如下格式:
三、项目结构
四、完整参考代码
实现User相关的类和接口和Student相关的类和接口类似就不贴了
com.example.pojo包
com.example.pojo.Student.java
使用了lombok插件简化代码
package com.example.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @projectName: week11_xxx
* @package: com.example.pojo
* @className: Student
* @author: GCT
* @description: TODO
* @date: 2022/11/8 21:02
* @version: 1.0
*/
@Data /*自动生成 set get toString方法*/
@NoArgsConstructor //自动生成无参构造函数
@AllArgsConstructor
public class Student {
Long id;
String sname;
String dept;
int age;
}
com.example.mapper包
com.example.mapper.StudentMapper.java
interface类型文件
package com.example.mapper;
import com.example.pojo.Student;
import com.example.pojo.User;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
/**
* @projectName: week11_20201003016
* @package: com.example.mapper
* @className: StudentMapper
* @author: GCT
* @description: TODO
* @date: 2022/11/8 21:03
* @version: 1.0
*/
@Mapper
public interface StudentMapper {
// 直接使用@Select()注解
// @Select("SELECT * FROM users")
// public List<User> getAllUserMap();
// 使用SQL配置文件
public List<Student> getAllStudentMap();
public Student getStudentById(Long id);
@MapKey("id") //这个不加也行 虽然IDEA可能会标红线,但运行时不会出错
public Map<Object,Object> getStudentByMulCondition(Map<Object,Object> mp);
@MapKey("id") //这个不加也行 虽然IDEA可能会标红线,但运行时不会出错
public Map<Object,Object> getStudentByDynam(Map<Object,Object> mp);
// 该方法插入一条记录,带参数,更新操作一定要提交事务
public int addStudent(Student student);
public int addStudentBatch(List<Student> student);
public int updateStudentByDynam(Map<Object,Object> mp);
// 根据id删除记录
public int deleteStudentById(Long id);
// 根据多个id删除多条记录
public int deleteStudentByIds(Long[] ids);
}
com/example/mapper/StudentMapper.xml
<?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语句返回List<Map<String,Object>,可以不使用实体类,直接用Map数据类型,更加简单,简化程序
-->
<select id="getAllStudentMap" resultType="Student">
SELECT * FROM student
</select>
<!--
3.SQL语句带一个参数
parameterType:指定SQL语句接收的参数类型
-->
<select id="getStudentById" resultType="Student" parameterType="Long">
SELECT * FROM student WHERE id=#{0}
</select>
<!--
4.SQL语句带多个参数,用Map封装,parameterType=Map,返回一条记录,按key取参数值
#在获取参数时可防止SQL注入攻击,应尽量使用#;模糊查询时,使用$
-->
<select id="getStudentByMulCondition" resultType="map" parameterType="Map">
SELECT * FROM student WHERE sname='${sname}' AND dept='${dept}' AND age='${age}'
</select>
<!--
6.动态SQL语句,实现动态查询
<:小于
>:大于
where标签:当有查询条件时,就使用where命令,当没有查询条件时,就不用where命令
if test:判断是否对某个字段进行查询
-->
<!-- MyBatis会处理多出来的and-->
<select id="getStudentByDynam" resultType="Map" parameterType="Map">
SELECT * FROM student
<where>
<if test="sname!=null">
sname=#{sname}
</if>
<if test="dept!=null">
and dept=#{dept}
</if>
<if test="age!=null">
and age=#{age}
</if>
</where>
</select>
<!-- 添加数据
5. delete、insert、update操作没有resultType属性,默认返回int型
parameterType=Map,表示参数类型为Map,用Map封装参数
#表示在获取参数时可防止SQL注入攻击,应尽量使用#;模糊查询时,使用$
INSERT INTO users SET username=,这种SQL语句是MmySql特有的扩展功能
-->
<insert id="addStudent" parameterType="Student">
<!-- <selectKey keyProperty="id" order="AFTER" resultType="int">-->
<!-- select LAST_INSERT_ID()-->
<!-- </selectKey>-->
INSERT INTO student SET sname=#{sname},dept=#{dept},age=#{age}
</insert>
<insert id="addStudentBatch" parameterType="Student">
insert into
student(sname,dept,age)
values
<foreach collection="list" item="student" separator=",">
(#{student.sname},#{student.dept},#{student.age})
</foreach>
</insert>
<!--
7.动态修改update语句
set标签:当有更新字段时,就使用set命令,当没有更新字段时,
语句为UPDATE course SET id=NULL WHERE id=NULL
-->
<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>
<!-- 根据id删除记录-->
<!-- 注意传参类型改成long后这里parameterType也要改-->
<delete id="deleteStudentById" parameterType="Long">
DELETE FROM student WHERE id=#{id}
</delete>
<!-- 批量删除 -->
<delete id="deleteStudentByIds" parameterType="Long[]" >
DELETE FROM student WHERE id IN
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>
com.example.service包
com.example.service.StudentService.java
interface类型文件
package com.example.service;
import com.example.pojo.Student;
import org.apache.ibatis.annotations.MapKey;
import java.util.List;
import java.util.Map;
public interface StudentService {
public List<Student> getAllStudentMap();
public Student getStudentById(Long id);
public Map<Object,Object> getStudentByMulCondition(Map<Object,Object> mp);
public Map<Object,Object> getStudentByDynam(Map<Object,Object> mp);
// 该方法插入一条记录,带参数,更新操作一定要提交事务
public int addStudent(Student student);
public int addStudentBatch(List<Student> sudent);
public int updateStudentByDynam(Map<Object,Object> mp);
// 根据id删除记录
public int deleteStudentById(Long id);
// 根据多个id删除多条记录
public int deleteStudentByIds(Long[] ids);
}
com.example.service.impl包
com.example.service.impl.StudentServiceImpl.java
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.util.List;
import java.util.Map;
/**
* @projectName: week11_xxx
* @package: com.example.service.impl
* @className: StudentServiceImpl
* @author: GCT
* @description: TODO
* @date: 2022/11/8 21:03
* @version: 1.0
*/
@Service //注意记得加@Service注解 :把这个类交给sping框架管理(bean),不然在controller中@Autowire会找不到该类!
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getAllStudentMap() {
return studentMapper.getAllStudentMap();
}
@Override
public Student getStudentById(Long id) {
return studentMapper.getStudentById(id);
}
@Override
public Map<Object, Object> getStudentByMulCondition(Map<Object, Object> mp) {
return studentMapper.getStudentByMulCondition(mp);
}
@Override
public Map<Object, Object> getStudentByDynam(Map<Object, Object> mp) {
return studentMapper.getStudentByDynam(mp);
}
@Override
public int addStudent(Student student) {
return studentMapper.addStudent(student);
}
@Override
public int addStudentBatch(List<Student> student) {
return studentMapper.addStudentBatch(student);
}
@Override
public int updateStudentByDynam(Map<Object, Object> mp) {
return studentMapper.updateStudentByDynam(mp);
}
@Override
public int deleteStudentById(Long id) {
return studentMapper.deleteStudentById(id);
}
@Override
public int deleteStudentByIds(Long[] ids) {
return studentMapper.deleteStudentByIds(ids);
}
}
com.example.controller包
com.example.controller.StudentController.java
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.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @projectName: week11_xxx
* @package: com.example.controller
* @className: StudentController
* @author: GCT
* @description: TODO
* @date: 2022/11/8 21:02
* @version: 1.0
*/
@RestController //=@ResponseBody+@Controller
@RequestMapping(value = "/student")
public class StudentController {
@Autowired
private StudentService studentService;
//1.Get请求,直接向页面返回json数据
// 查找所有学生信息
@RequestMapping(value = "/getAllStudent",method = RequestMethod.GET)
public List<Student> getAllStudent(){
// localhost:8080/student/getAllStudent
List<Student> studentList = studentService.getAllStudentMap();
System.out.println("test");
return studentList;
}
//根据学生id查找对应学生信息
@RequestMapping(value = "/getStudentById",method =RequestMethod.GET)
public Student getStudentById(Long id){
// localhost:8080/student/getStudentById?id=3
// 或者localhost:8080/student/getStudentById 在form-data中设置id 3
Student studentById = studentService.getStudentById(id);
System.out.println(studentById);
return studentById;
}
//根据传入参数返回符合对应条件的学生信息
@RequestMapping(value = "/getStudentByMulCondition",method =RequestMethod.POST)
@ResponseBody
public Map<Object,Object> getStudentByMulCondition(@RequestBody Map<Object,Object> mp){
// 需要在传入的参数前面加上@RequestBody将传入的json数据封装成对应的Map类型数据
// @RequestBody 用在方法参数上面,用来将请求参数绑定到request body中,
// 通过HttpMessageConverter封装为具体的JavaBean。
// 通俗点讲就是你在一个参数上加上该注解,spring就会将request body中的json/xml对象解析成该参数类型的Javabean对象。
System.out.println("mp:");
System.out.println(mp);
Map<Object, Object> studentByMulCondition = studentService.getStudentByMulCondition(mp);
System.out.println(studentByMulCondition);
// Could not write JSON: java.lang.Long cannot be cast to java.lang.String; nested exception is com...报错解决:
// 原来使用的是Map<String,Object>,但id为long类型,所以会报错,解决方法:将String类型改为Object类型!!!
return studentByMulCondition;
}
// 根据传入的不定数量的数个参数返回符合条件的学生信息
@RequestMapping(value = "/getStudentByDynam",method =RequestMethod.POST)
public Map<Object,Object> getStudentByDynam(@RequestBody Map<Object,Object> mp){
// 注意这里也要加@RequestBody来将传入的json数据封装成Map类型
// 注意sql语句中and的处理
System.out.println("mp: ");
System.out.println(mp);
Map<Object, Object> studentByDynam = studentService.getStudentByDynam(mp);
System.out.println(studentByDynam);
return studentByDynam;
}
// 往数据库中新增学生信息(处理页面表单中提交的数据)
//2.POST请求,处理页面表单中提交的数据,向数据库增加数据
@RequestMapping(value = "/addStudent_1",method = RequestMethod.POST)
public int addStudent_1(Student student) {
//将页面表单传过来的参数自动封装成student对象,然后存入数据库
// localhost:8080/student/addStudent_1
// {
// "sname":"tsetJson",
// "dept":"testJson",
// "age":2
// }
// 或者使用form-data模拟表单提交
System.out.print(student);
int num=studentService.addStudent(student);
// Long id = student.getId();
// System.out.println(id);
System.out.println(num);
return num;
}
//往数据库中新增学生信息(处理页面使用ajax提交的json数据)
//3.使用@RequestBody,处理页面使用ajax提交的json数据,向数据库增加数据
// 这种方法在接口测试时需要使用原始json格式数据进行测试,使用表单形式数据会报错
@RequestMapping(value = "/addStudent_2")
public int addStudent_2(@RequestBody Student student) {
//将页面传过来的参数自动封装成student对象,然后存入数据库
System.out.print(student);
int num=studentService.addStudent(student);
// Long id = student.getId();
// System.out.println(id);
System.out.println(num);
return num;
}
//批量向数据库中新增学生信息
@RequestMapping(value = "/addStudentBatch",method =RequestMethod.POST)
public int addStudentBatch(@RequestBody List<Student> student){
// 注意要传入list类型的json数据:
// [
// {
// },
// {
// }
// ]
// 传入后会自动封装为List<Student>类型
System.out.println("student: ");
System.out.println(student);
int row = studentService.addStudentBatch(student);
System.out.println(row);
return row;
}
//根据传入不定数量的参数更新对应学生的对应信息
@RequestMapping(value = "/updateStudentByDynam",method =RequestMethod.POST)
public int updateStudentByDynam(@RequestBody Map<Object,Object> mp){
// 注意这里也要加@RequestBody来将传入的json数据封装成Map类型
System.out.println("mp: ");
System.out.println(mp);
int row = studentService.updateStudentByDynam(mp);
System.out.println(row);
return row;
}
// 根据id删除学生记录
@RequestMapping(value = "/deleteStudentById",method =RequestMethod.POST)
// public int deleteStudentById(@RequestBody Long id){
public int deleteStudentById(Long id){
// 不加@RequestBody的话需要使用form-data提交
// 加上@RequestBody的话需要使用raw中写数字提交(啥括号都不用,只用一个数字)
System.out.println("id: "+id);
return studentService.deleteStudentById(id);
}
// 根据多个id删除多条学生记录
@RequestMapping(value = "/deleteStudentByIds",method =RequestMethod.POST)
public int deleteStudentByIds(@RequestBody Long[] ids){
// 这里也要加@RequestBody将传入的数据封装成Long[]类型
// []
System.out.println(Arrays.toString(ids));
return studentService.deleteStudentByIds(ids);
}
}
com.example.Week11xxxApplication.java
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//别忘了加这个注解扫描mapper层中的所有类,不然会找不到对应的mapper文件
@MapperScan(basePackages = "com.example.mapper")
public class Week1120201003016Application {
public static void main(String[] args) {
SpringApplication.run(Week1120201003016Application.class, args);
}
}
src/main/resources文件夹
这里面只用到了application.yml这一个配置文件
src/main/resources/application.yml
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命令
pom.xml
<?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_xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>week11_xxx</name>
<description>week11_xxx</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,不然会报Invalid bound statement (not found): com.example.mapper.....-->
<!-- 遇到这个报错:-->
<!-- 1、检查springboot的application中是否有加@MapperScan(basePackages = "com.example.mapper")注解-->
<!-- 2、检查...Mapper.java文件是否有@Mapper注解-->
<!-- 3、检查配置文件(application.yml)中是否有-->
<!-- mybatis:-->
<!-- mapper-locations: classpath:com/exmaple/mapper/*.xml #指定sql配置文件的位置-->
<!-- 4、检查pom.xml文件中<build>标签中是否有配这个resources-->
<!-- 将java目录下的xml文件打包-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
五、接口测试设计及运行结果
跳转到知识总结查看接口测试工具基本使用参考StudentController.java中的对应函数接口测试
getAllStudent()
查找所有学生信息
接口测试设计及结果:
后台输出结果:
getStudentById(Long id)
根据学生id查找对应学生信息
接口测试设计及结果:
后台打印结果:
getStudentByMulCondition(@RequestBody Map<Object,Object> mp)
根据传入参数返回符合对应条件的学生信息
接口测试设计及结果:
后台打印结果:
getStudentByDynam(@RequestBody Map<Object,Object> mp)
== 根据传入的不定数量的数个参数返回符合条件的学生信息==
接口测试设计及结果:
后台打印结果:
addStudent_1(Student student)
往数据库中新增学生信息(处理页面表单中提交的数据)
接口测试设计及结果:
后台打印结果:
数据库新增数据显示:
addStudent_2(@RequestBody Student student)
往数据库中新增学生信息(处理页面使用ajax提交的json数据)
接口测试设计及结果:
后台打印结果:
数据库新增数据显示;
addStudentBatch(@RequestBody List student)
批量向数据库中新增学生信息
接口测试设计及结果:
后台打印结果:
数据库新增数据显示:
updateStudentByDynam(@RequestBody Map<Object,Object> mp)
据传入不定数量的参数更新对应学生的对应信息
接口测试设计及结果:
后台打印结果:
数据库更新数据显示:
更新前:
更新后:
deleteStudentById(Long id)
根据id删除学生记录
接口测试设计及结果:![在这里插入图片描述](https://img-blog.csdnimg.cn/00fa408005f9440caafa8191cb065b4f.png)
后台打印结果:
数据库删除数据显示:
删除前:
删除后:
deleteStudentByIds(@RequestBody Long[] ids)
根据多个id删除多条学生记录
接口测试设计及结果:
后台打印结果:
数据库删除数据显示:
删除前:
删除后: