tomcat对数据库信息对进行添加操作和修改操作

发布时间:2024年01月09日

先在接口方法中添加相应接口方法:

//添加新产品
    @Insert("insert into db.brand values(null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
    void addBrand(brand brand);

在service中添加对应的方法:

public void addBrand(brand brand){
String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper brandMapper = sqlSession.getMapper(brandMapper.class);
        brandMapper.addBrand(brand);
        sqlSession.commit();
        sqlSession.close();
    }

然后开始写添加新产品的信息jsp

<%--
  Created by IntelliJ IDEA.
  User: quwenhao
  Date: 2023/12/28
  Time: 19:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加品牌</title>
</head>
<body>
<h3>添加品牌</h3>
<form action="/brandAddService" method="post">
    品牌名称:<input name="brandName"><br>
    企业名称:<input name="companyName"><br>
    排序:<input name="ordered"><br>
    描述信息:<textarea rows="5" cols="20" name="description"></textarea><br>
    状态:<input type="radio" name="status" value="0">禁用
         <input type="radio" name="status" value="1">启用<br>

    <input type="submit" value="提交">
</form>

</body>
</html>

完成后设置表单提交给添加的servlet,下面开始写添加的servlet

package org.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import org.example.service.brandService;
import org.example.pojo.brand;

@WebServlet("/brandAddService")
public class brandAddService extends HttpServlet {
    private brandService brandService=new brandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //处理post类型乱码问题
        request.setCharacterEncoding("utf-8");

        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String ordered = request.getParameter("ordered");
        String description = request.getParameter("description");
        String status = request.getParameter("status");

        //封装成brand对象
        brand brand=new brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(Integer.parseInt(ordered));
        brand.setDescription(description);
        brand.setStatus(Integer.parseInt(status));

        //调用Service添加方法
        brandService.addBrand(brand);

        //转发到查询所有
        request.getRequestDispatcher("/brandSelectAllService").forward(request,response);

    }

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

在brand.jsp中我们通过设置了新增按钮来添加信息,<input type="button" value="新增" id="addBrand"><br>。我们可以运用javaScript获取其id为其添加跳转功能

在body标签末尾添加下面代码,添加跳转功能。

<script>
    document.getElementById("addBrand").onclick=function (){
        location.href="/addBrand.jsp";
    }
</script>

下面是修改操作:想对特定某条信息进行修改,需要先根据id查询到特定的信息,然后在进行修改。

接口方法:

//根据id查询
    @Select("select * from db.brand where id=#{id}")
    @ResultMap("brandResultMap")
    brand selectByIdBrand(int id);

    //根据id,修改特定的产品的信息
    @Update("update db.brand set brand_Name=#{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} where id=#{id}")
    void update(brand brand);

service:

public brand brandSelectById(int id){
String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper brandMapper = sqlSession.getMapper(brandMapper.class);
        brand brand = brandMapper.selectByIdBrand(id);

        sqlSession.close();
        return brand;
    }

    public void Update(brand brand){
String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper brandMapper = sqlSession.getMapper(brandMapper.class);
        brandMapper.update(brand);
        sqlSession.commit();
        sqlSession.close();
    }

然后写根据id查询到的特定产品的详细信息的jsp:

<%--
  Created by IntelliJ IDEA.
  User: quwenhao
  Date: 2023/12/28
  Time: 20:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
    <title>修改品牌</title>
</head>
<body>
<h3>修改品牌</h3>
<form action="/updateServlet" method="post">

<%--    隐藏域,提交id--%>
    <input type="hidden" name="id" value="${brand.id}">

    品牌名称:<input name="brandName" value="${brand.brandName}"><br>
    企业名称:<input name="companyName" value="${brand.companyName}"><br>
    排序:<input name="ordered" value="${brand.ordered}"><br>
    描述信息:<textarea rows="5" cols="20" name="description">${brand.description}</textarea><br>
    状态:
    <c:if test="${brand.status==0}">
        <input type="radio" name="status" value="0" checked>禁用
        <input type="radio" name="status" value="1">启用<br>
    </c:if>
    <c:if test="${brand.status==1}">
    <input type="radio" name="status" value="0">禁用
    <input type="radio" name="status" value="1" checked>启用<br>
    </c:if>

    <input type="submit" value="提交">
</form>

</body>
</html>

然后写的是特定产品的详细信息的Servlet:

package org.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

import org.example.pojo.brand;
import org.example.service.brandService;

@WebServlet("/selectByIdService")
public class selectByIdService extends HttpServlet {

    private brandService brandService=new brandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收id
        String id = request.getParameter("id");
        //2.service查询
        brand brand = brandService.brandSelectById(Integer.parseInt(id));
        //3.存储到request中
        request.setAttribute("brand",brand);
        //4.转发
        request.getRequestDispatcher("/selectById.jsp").forward(request,response);
    }

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

我们想调用该servlet根据id查询详细信息,需要先知道该信息的id,所以在跳转到该servlet时应把id传过来。所以brand.jsp中修改信息的跳转链接应该这样写,将#改为下面的链接。

 <td><a href="/selectByIdService?id=${brand.id}">修改</a>

这样我们在打开修改页面的时候,该产品原本的信息会自动填写在文本框中。

修改信息完后,会将该表单提交到updateServlet中,执行修改操作:

package org.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import org.example.pojo.brand;
import org.example.service.brandService;

@WebServlet("/updateServlet")
public class updateServlet extends HttpServlet {
    brandService brandService=new brandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String ordered = request.getParameter("ordered");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        String id=request.getParameter("id");

        brand brand=new brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(Integer.parseInt(ordered));
        brand.setDescription(description);
        brand.setStatus(Integer.parseInt(status));
        brand.setId(Integer.parseInt(id));

        brandService.Update(brand);

        request.getRequestDispatcher("/brandSelectAllService").forward(request,response);
    }

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

这样就可以完成修改操作了

文章来源:https://blog.csdn.net/hguhbh/article/details/135482180
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。