赞
踩
在 resources 目录下 static 中 新建
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <h1>商品管理系统(异步版)</h1>
- <a href="/list.html">商品列表</a>
- </body>
- </html>
在 resources 目录下 static 中 新建
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <div>
- <table border="1">
- <caption>商品列表</caption>
- <tr>
- <th>id</th>
- <th>标题</th>
- <th>价格</th>
- <th>库存</th>
- <th>操作</th>
- </tr>
- <tr v-for="p in arr">
- <td>{{p.id}}</td>
- <td>{{p.title}}</td>
- <td>{{p.price}}</td>
- <td>{{p.num}}</td>
- <!--javascript:void(0) 废掉超链接的跳转功能-->
- <!--需要跳转的话就不废掉超链接-->
- <!--属性里面出现变量 必须使用属性绑定,也就是再属性前面加:
- 加完:后属性的值就变成javaript代码了-->
- <td>
- <a :href="'/update.html?id='+p.id" >修改</a>
- <a href="javascript:void(0)" @click="del(p.id)">删除</a>
- </td>
- </tr>
- </table>
-
- </div>
-
- <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
- <script src="https://cdn.staticfile.org/vue/2.2.2/vue.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 (response) {
- alert("删除成功!")
- location.reload();//刷新页面,删除后的表格会有空的位置,需要刷新
- })
- }
-
- }
- },
- created:function(){
- //此方法是vue对象的一个生命周期方法,创建vue对象时,此方法会自动调用,
- //通常情况下把刚刚进入页面需要做的事情,写在此方法中
- //发出异步请求获取数据
- axios.get("/select").then(function (response) {
- //服务器响应的数据为装着多个商品信息的数组
- v.arr = response.data;
- })
- }
- })
- </script>
- </body>
- </html>
在 resources 目录下 static 中 新建
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <div>
- <h1>修改商品页面</h1>
- <!--只读 readonly-->
- <input type="text" v-model="p.id" placeholder="id" readonly>
- <input type="text" v-model="p.title" placeholder="title">
- <input type="text" v-model="p.price" placeholder="price">
- <input type="text" v-model="p.num" placeholder="num">
- <input type="button" value="修改" @click="update()">
- </div>
- <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
- <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
- <script>
- let v = new Vue({
- el:"body>div",
- data:{
- p:{id:"",title:"",price:"",num:""}
- },
- methods:{
- update(){
- axios.post("/update",v.p).then(function (response) {
- alert("修改完成");
- location.href="/list.html";//回到列表页面
- })
- }
- },
- created:function () {
- //通过修改商品的id查询商品的所有信息
- //location.search = '?id=5'
- axios.get("/selectById"+location.search).then(function (response) {
- //把通过id查询到的商品对象复制给vue里面的变量,页面会自动发生改变
- v.p=response.data;
- })
- }
- })
- </script>
- </body>
- </html>
在entity包下新建
- package cn.detu.boot08.entity;
-
- public class Product {
- private Integer id;
- private String title;
- private Double price;
- private Integer num;
-
- @Override
- public String toString() {
- return "Product{" +
- "id=" + id +
- ", title='" + title + '\'' +
- ", price=" + price +
- ", num=" + num +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public Double getPrice() {
- return price;
- }
-
- public void setPrice(Double price) {
- this.price = price;
- }
-
- public Integer getNum() {
- return num;
- }
-
- public void setNum(Integer num) {
- this.num = num;
- }
- }
在controller包下新建
- package cn.detu.boot08.controller;
-
- import cn.detu.boot08.entity.Product;
- import cn.detu.boot08.mapper.ProductMapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.List;
-
- @RestController
- public class ProductController {
- @Autowired(required = false)
- ProductMapper mapper;
-
- @RequestMapping("/select")
- public List<Product> select(){
- //把查询到的list集合直接响应给客户端
- return mapper.select();
- }
-
- @RequestMapping("delete")
- public void delete(int id){
- System.out.println("id = " + id);
- mapper.deleteById(id);
- }
-
- @RequestMapping("/update")
- public void update(@RequestBody Product product){
- mapper.update(product);
- }
-
- @RequestMapping("/selectById")
- public Product selectById(int id){
- System.out.println("id = " + id);
- return mapper.selectById(id);
- }
在mapper包下新建
- package cn.detu.boot08.mapper;
-
- import cn.detu.boot08.entity.Product;
- import org.apache.ibatis.annotations.*;
-
- import java.util.List;
-
- @Mapper
- public interface ProductMapper {
- @Insert("insert into product values(null,#{title},#{price},#{num})")
- void insert(Product product);
- @Select("select * from product")
- List<Product> select();
- @Delete("delete from product where id = #{id}")
- void deleteById(int id);
- @Select("select * from product where id = #{id}")
- Product selectById(int id);
- @Update("update product set title=#{title},price=#{price},num=#{num} where id = #{id}")
- void update(Product product);
- }
商品列表
修改商品页面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。