项目实战:修改水果库存系统特定库存记录

1、在edit.html修改库存页面添加点击事件




    
    Title
    
    
    
    


编辑水果库存信息

名称:
单价:
库存:
备注:

2、在edit.js中添加update = function ( ) { } 功能

2.1、common.js

function $(key){
    if(key){
        if(key.startsWith("#")){
            key = key.substring(1)
            return document.getElementById(key)
        }else{
            let nodeList = document.getElementsByName(key)
            return Array.from(nodeList)
        }
    }
}

2.2、edit.js 

let queryString = window.location.search.substring(1)
if(queryString){
    var fid = queryString.split("=")[1]

    window.onload=function(){
        loadFruit(fid)
    }

    loadFruit = function(fid){
        axios({
            method:'get',
            url:'edit',
            params:{
                fid:fid
            }
        }).then(response=>{
            debugger
            let fruit = response.data.data
            $("#fid").value=fruit.fid
            $("#fname").value=fruit.fname
            $("#price").value=fruit.price
            $("#fcount").value=fruit.fcount
            $("#remark").value=fruit.remark
        })
    }

    update=function(){
        let fid = $("#fid").value
        let fname = $("#fname").value
        let price = $("#price").value
        let fcount = $("#fcount").value
        let remark = $("#remark").value
        axios({
            method:'post',
            url:"update",
            data:{
                fid:fid,
                fname:fname,
                price:price,
                fcount:fcount,
                remark:remark
            }
        }).then(response=>{
            if(response.data.flag){
                window.location.href="index.html"
            }
        })
    }
}

3、封装读取json字符串方法创建RequestUtil工具类

package com.csdn.fruit.util;
import jakarta.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
public class RequestUtil {
    public static Object readObject(ServletRequest request, Class clazz) throws IOException {
        BufferedReader br = request.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        String str = null;
        while ((str = br.readLine()) != null) {
            stringBuilder.append(str);
        }
        str = stringBuilder.toString();
        return GsonUtil.fromJson(str, clazz);
    }
}

4、修改FruitDaoImpl类中的updateFruit方法中sql语句

  • 因为之前写的是只能通过 fname 修改 fcount
  • 现在想要通过 fid 可以修改所有的属性值,所以需要修改sql语句
package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.mymvc.dao.BaseDao;
import java.util.List;
public class FruitDaoImpl extends BaseDao implements FruitDao {
    @Override
    public void addFruit(Fruit fruit) {
        String sql = "insert into t_fruit values (0,?,?,?,?)";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());
    }
    @Override
    public void delFruit(String fname) {
        String sql = "delete from t_fruit where fname=?";
        super.executeUpdate(sql, fname);
    }
    //通过 fid 可以修改所有的属性值
    @Override
    public void updateFruit(Fruit fruit) {
        String sql = "update  t_fruit set fname=?,price=?,fcount=?,remark=? where fid = ?";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());
    }
    @Override
    public List getFruitList() {
        return super.executeQuery("select * from t_fruit");
    }
    @Override
    public Fruit getFruitByFname(String fname) {
        return load("select * from t_fruit where fname = ?", fname);
    }
    @Override
    public Fruit getFruitByFid(Integer fid) {
        return load("select * from t_fruit where fid=?", fid);
    }
}

5、编写Controller层UpdateServlet

package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.dto.Result;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.util.RequestUtil;
import com.csdn.fruit.util.ResponseUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    private FruitDao fruitDao = new FruitDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);
        fruitDao.updateFruit(fruit);
        ResponseUtil.print(resp, Result.OK());
    }
}

项目实战:修改水果库存系统特定库存记录_第1张图片

你可能感兴趣的:(#,Tomcat-Servlet,javascript,axios,Servlet,onclick)