springboot+vue前后端分离(2)——实现简单CRUD

一、环境工具
1、前端
工具:VSCode,
vue-cli、mint-ui构建应用,axios前后端交互。
2、后端
工具:myeclipse,
基于springboot框架搭建项目。
二、项目搭建过程
详见springboot+vue前后端分离(1)

二、流程
1、增加:
(1)前端

    adduser(){
          axios.get("http://localhost:9090/user/add",{params:{name:this.name,password:this.password}})
          .then((res)=>{
            alert(res.data);
          }).catch((erro)=>{alert(erro)});
        },

(2)后端

@GetMapping(path="/add")
	public @ResponseBody String add(@RequestParam String name,@RequestParam String password){
		User n=new User();
		n.setName(name);
		n.setPassword(password);
		userDao.save(n);
		return "新增用户成功!";
	}

流程:前端向后端相应地址发送参数,——》后端接收参数,并创建一个新的用户保存到数据库——》返回一个成功消息的字符串给前端。

2、删除
(1)前端

deleteuser(uid){
          axios.get("http://localhost:9090/user/delete?id="+uid)
          .then((res)=>{
            alert(res.data);
          })
          .catch((erro)=>{alert(erro)})
        },

(2)后端

//删除
	@GetMapping("/delete")
	public @ResponseBody String delete(@RequestParam Integer id){
		userDao.delete(id);
		return "已经删除了此用户";
	}

流程:前端函数,传递用户id——》后端根据用户id删除用户——》返回删除消息字符串,在前端弹出。

3、获取
(1)前端

edituser(uid){
          axios.get("http://localhost:9090/user/get?id="+uid)
          .then((res)=>{this.editusers=res.data})
          .catch((erro)=>{alert(erro)})
        },

(2)后端

@GetMapping("/get")
	public @ResponseBody  User get(@RequestParam int id){
		return userDao.getOne(id);
	}

流程:前端传递用户id——》后端根据id从数据库查询用户,并返回给前端——》前端将用户放到相应对象。
注:后端实体类要加注解@JsonIgnoreProperties({ “handler”,“hibernateLazyInitializer” });控制类要用@RestController。

3、查询
(1)前端

findAll(){
      axios.get('http://localhost:9090/user/findAll')
      .then((res)=>{
        this.users=res.data;
      }).catch((erro)=>{
        alert(erro)
      });
    },

(2)后端

@GetMapping(path="/findAll")
	public @ResponseBody Iterable<User> findAll(){
		Sort sort=new Sort(Sort.Direction.DESC,"id");
		return userDao.findAll(sort);
	}

流程:前端指明要获取数据的路径,——》后端查询返回用户集合。——》前端将数据放入数组。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值