【小项目】Axios 实现前后端交互

Axios 实现前后端交互

提示:本篇基于【小项目】SSM 实现单表 CRUD

一、数据库

准备数据
在这里插入图片描述

二、前端

文件名:Axios.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Axios</title>
  <script src="../js/axios.js"></script>
</head>

<body>

<button onclick="restFul()">测试 RESTful</button>

<button onclick="object()">测试对象传参</button>

<button onclick="postObj()">测试 POST 对象传参</button>

<button onclick="putObj()">测试 PUT 对象传参</button>

</body>
</html>

<script>
    function restFul(){
        let name = '张伟'
        let age = 33
        let url = `http://localhost:8080/student/${name}/${age}`  //此处为反引号,即 Tab 上面的那个按键
        axios.get(url).then(promise => {
            console.log(promise.data)
        })
    }

    function object(){
        let student = {
            id : 1001,
            name : '张伟',
            age : 33
        }

        let url = "http://localhost:8080/student/getStu"
        axios.get(url,{params : student}).then(promise => {
            console.log(promise.data)
        })
    }

    function postObj(){
        let student = {
            id : 1005,
            name : "王五",
            email : 'wangwu@qq.com',
            age : 35
        }

        let url = "http://localhost:8080/student/saveStu"
        axios.post(url,student)
            .then(promise => {
                console.log(promise.data)
            })
    }

    function putObj(){
        let id = 1005
        let student = {
            id : 1005,
            name : "赵四",
            email : 'zs2021@qq.com',
            age : 42
        }

        let url = `http://localhost:8080/student/updateStuById/${id}`
        axios.put(url,student)
            .then(promise => {
                console.log(promise.data)
            })
    }

</script>

三、后端

1.StudentServiceImpl.java

	//更新
    public void updateStu(Student student, Integer whereId){
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("id", whereId);
        studentMapper.update(student, updateWrapper);   //第一个参数为 SET 内容,第二个参数为 WHERE 内容
    }

    //新增
    public void saveStu(Student student){
        studentMapper.insert(student);
    }

    //接收对象参数
    public List<Student> getStu(Student student){
        QueryWrapper queryWrapper = new QueryWrapper(student);
        return studentMapper.selectList(queryWrapper);
    }

    //按照名字年龄查询 RESTful 结构
    public List<Student> getStuByNA(Student student){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("name",student.getName())
                .eq("age", student.getAge());
        return studentMapper.selectList(queryWrapper);
    }

2.StudentController.java

	//修改, RESTful 的名称不要与属性重名,否则会覆盖
    @PutMapping("updateStuById/{whereId}")
    public String updateStu(@RequestBody Student student, @PathVariable Integer whereId){
        studentService.updateStu(student, whereId);
        return "修改成功";
    }

    //新增
    @PostMapping("saveStu")
    public String saveStu(@RequestBody Student student){

        studentService.saveStu(student);
        return "新增成功";
    }

    //接收对象参数
    @GetMapping("getStu")
    public List<Student> getStu(Student student){
        return studentService.getStu(student);
    }

    //按照名字年龄查询 RESTful 结构
    @GetMapping("{name}/{age}")
    public List<Student> getStuByNA(Student student){
        return studentService.getStuByNA(student);
    }

四、测试

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

313YPHU3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值