删除操作的 CRUD JDBC 模板问题

CRUD JDBC template problem with delete operation

我对 CRUD 删除操作有疑问。当我单击 jsp 文件 <form action="delete?Id=${medicines.id}" method="post"> <input type="submit" value="Delete"> </form>.

中以这种方式编写的删除时

这是我的 DeleteController(如果我在 doGet 中输入一些内容,那么我会从中得到结果,而不是空白页)

@WebServlet("/delete")
public class MedicinesDeleteController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("Id"));
        if(request.getUserPrincipal() != null) {
            MedicinesService query = new MedicinesService();
            query.delete(id);
            response.sendRedirect(request.getContextPath() + "/");
        } else {
            response.sendError(403);
        }
    }   
}

从 MedicinesService 中删除函数(我认为这是我的问题

public void delete(int id) {
    DAOFactory factory = DAOFactory.getDAOFactory();
    MedicinesDAO medicinesDao = factory.getMedicinesDAO();
    Medicines medicines = new Medicines();
    medicines.setId(id);
    medicinesDao.delete(medicines);
}

和 MedicinesDAOImpl

private final static String DELETE_MEDICINES = 
      "DELETE FROM medicines WHERE id_medicines=:id_medicines;";
@Override
public void delete(Medicines medicines) {
    SqlParameterSource namedParameter = new MapSqlParameterSource("Id", medicines.getId());
    template.update(DELETE_MEDICINES, namedParameter);
}

新错误

medicinesDao.delete(medicines); < MedicicinesService:41
template.update(DELETE_MEDICINES, namedParameter); < MedicinesDAOImpl:77
query.delete(id); <MedicinesDeleteController:31


       Type Exception Report

        Message No value supplied for the SQL parameter 'id_medicines': No value registered for key 'id_medicines'

        Description The server encountered an unexpected condition that prevented it from fulfilling the request.

        Exception

        org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'id_medicines': No value registered for key 'id_medicines'
            org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray(NamedParameterUtils.java:355)
            org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:398)
            org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:370)
            org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:317)
            pl.firstaidkit.dao.MedicinesDAOImpl.delete(MedicinesDAOImpl.java:77)
            pl.firstaidkit.dao.MedicinesDAOImpl.delete(MedicinesDAOImpl.java:1)
            pl.firstaidkit.service.MedicinesService.delete(MedicinesService.java:41)
            pl.firstaidkit.controller.MedicinesDeleteController.doPost(MedicinesDeleteController.java:31)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
            pl.firstaidkit.filter.LoginFilter.doFilter(LoginFilter.java:26)
        Note The full stack trace of the root cause is available in the server logs.

最初的问题是,这会导致调用 http get 方法,而 doGet() 方法没有代码来处理相同的问题

 <a href="delete?drugid=${medicines.id}">Delete</a> 

第二个问题是 MapSqlParameterSource 参数名称和查询中的占位符不匹配。