vue+springboot crud

查看

getMyData()
            {
                axios.get('http://localhost:8081/test').then(
                    (response)=>
                    {
                        console.log(response.data)
                    },
                    (error)=>
                    {
                        console.log("error")
                    }
                )
            },

mapper:

@Select("select * from test")
    List<Test> findAll();

controller:

@Resource
    TestMapper TestMapper;

    @GetMapping
    @RequestMapping("/test")
    public List<Test>getT()
    {
        return TestMapper.findAll();
    }

增加(*)=>需要将请求头格式设置为application/json;charset=utf-8

axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
addMyData()
            {
                let user= {
                        id:'123',
                        name:'七七七',
                        sex:'男',
                        age:19,
                        school:'攀大'
                    };
                axios({
                        url:'http://localhost:8081/addUser',
                        method:'post',
                        data:JSON.stringify(user),
                    }).then(
                    (response)=>
                    {
                        console.log(response.data)
                    },
                    (error)=>
                    {
                        console.log(error.message)
                    }
                )
            },

mapper:传过来的对象t中必须要有values后面的属性名和属性值

 @Insert("insert into test(id,name,sex,age,school) values(#{id},#{name},#{sex},#{age},#{school})")
    void addUser(@RequestBody Test t);

controller:@RequestBody代表传入的是一个对象

@GetMapping
    @RequestMapping(path = "/addUser")
    public String addUser(@RequestBody Test t)
    {
        TestMapper.addUser(t);
        return "增加成功";
    }

删除:前端需要用params{}将参数囊括进去

deleteMyData(id)
            {
                axios({
                        url:'http://localhost:8081/deleteById',
                        method:'post',
                        params:
                            {
                               id:id
                            }
                    }).then(
                    (response)=>
                    {
                        console.log(response.data)
                    },
                    (error)=>
                    {
                        console.log(error.message)
                    }
                )
            }

mapper:

@Delete("delete from test where id=#{id}")
    void deleteById(String id);

controller:(controller需要用@RequestParam获取查询参数)

    @GetMapping
    @RequestMapping("/deleteById")
    public String deleteById(@RequestParam String id)
    {
        TestMapper.deleteById(id);
        return "删除成功";
    }

修改

updateMyData(id)
            {
                let user2 =
                    {
                        id:id,
                        name:'哈哈',
                    }
                axios({
                      url:'http://localhost:8081/updateUser',
                      method:'post',
                      data:JSON.stringify(user2),
                     }).then(
                    (res)=>
                    {
                        console.log(res.data)
                    })
            }

mapper:

@Update("update test set name=#{name} where id=#{id}")
    void updateById(@RequestBody Test t);

controller:

@GetMapping
    @RequestMapping(path= "/updateUser")
    public String updateUser(@RequestBody Test t)
    {
        System.out.println(t.toString());
        TestMapper.updateById(t);
        return "修改成功";
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值