异步请求商品管理(商品修改操作)简易版

异步请求商品管理(商品删除操作)简易版

http://t.csdn.cn/SOusH

  1. 修改商品
  1. 回显步骤

在select.html页面table表中添加修改超链接, 请求地址/update.html?id=xxx

<div>
    <h1>商品列表</h1>
    <table border="1" >
        <th>编号</th><th>商品标题</th><th>库存</th><th>价格</th><th>操作</th>
        <tr v-for="(p,i) in arr">
            <td>{{p.id}}</td>
            <td>{{p.title}}</td>
            <td>{{p.num}}</td>
            <td>{{p.price}}</td>
            <!--javascript:void(0) 废掉超链接的跳转功能-->
            <td><a :href="'/update.html?id='+p.id">修改</a>
                <a href="javascript:void(0)" @click="del(p.id)">删除</a>
            </td>
        </tr>
    </table>
</div>
<!-- 引入Vue框架 -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v=new Vue({
        el:"body>div",
        data:{
            arr:[]
        },
        methods:{
            del(id){
                if (confirm("您确定删除此商品吗?")){
                    //发出异步删除请求
                    axios.get("/delete?id="+id).then(function () {
                        //重新加载页面
                        location.reload();
                    })
                }
            }
        },
        created:function () {
            //页面加载完会自动执行此方法
            axios.get("/select").then(function (response){
                //把请求回来的数据给到数组
                v.arr=response.data;

            })
        }
    })
    </script>

创建update.html页面, 然后在页面中准备文本框和按钮, 让文本框和Vue里面的对象双向绑定, 在created方法中向/selectById?id=xxx发出请求 把请求回来的商品对象赋值给Vue里面的对象

<div>
  <h1>修改商品信息</h1>
    <input type="text" v-model="p.id" placeholder="id" readonly>
    <input type="text" v-model="p.title" placeholder="标题">
    <input type="text" v-model="p.num" placeholder="库存">
    <input type="text" v-model="p.price" placeholder="价格">
    <input type="button" value="修改" @click="update()">
    
</div>
<!-- 引入Vue框架 -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v=new Vue({
        el:"body>div",
        data:{
            p:{  id:"", title:"", num:"", price:"",}
        },
        methods:{
            update(){
                axios.post("/update",v.p).then(function () {
                    alert("修改完成!")
                    location.href="/select.html";
                })
            }
        },
      created:function () {
            //localhost:8080/select?id=1
          //location.search 得到 ?id=1;
        axios.get("/selectById"+location.search).then(function (response) {
            console.log(location.search);
            //得到通过id查询到的商品对象 赋值给Vue里面的对象,页面会自动改变
          v.p=response.data;
        })
      }
    })
    </script>

在ProductController中添加selectById方法处理 /selectById请求, 方法中调用mapper里面的selectById方法 返回值为Product对象, 最后把对象返回给客户端

@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;

@RequestMapping("/selectById")
    public Product selectById(int id){
    System.out.println("id = " + id);
    return mapper.selectById(id);
}
}

实现mapper里面的selectById方法

@Mapper
public interface ProductMapper {

    @Select("select * from product where id=#{id}")
    Product selectById(int id);
}

2.修改步骤:

  1. 给修改按钮添加点击事件, 在事件方法中向/update发出异步请求把双向绑定的对象提交服务器
<input type="button" value="修改" @click="update()">

methods:{
            update(){
                axios.post("/update",v.p).then(function () {
                    alert("修改完成!")
                    location.href="/select.html";
                })
            }

在ProductController中添加update方法处理/update请求, 方法中调用mapper的update方法把接收到的Product对象传递到方法中

@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;

@RequestMapping("/update")
    public void update(@RequestBody Product product){
    mapper.update(product);
}
}

实现mapper里面的update方法

@Mapper
public interface ProductMapper {
//执行修改SQL语句
    @Update("update product set title=#{title},num=#{num},price=#{price} where id=#{id}")
    void update(Product product);
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程源三zhang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值