修改-回显数据

:::info 完成一个根据id进行查询数据的功能
所以直接命名为SelectById :::

流程图

image.png

编写BrandMapper.java

  1. package com.taotao.mapper;
  2. import com.taotao.pojo.Brand;
  3. import org.apache.ibatis.annotations.ResultMap;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.util.List;
  6. /**
  7. * create by 刘鸿涛
  8. * 2022/3/29 16:12
  9. */
  10. public interface BrandMapper {
  11. //查询所有
  12. List<Brand> selectAll();
  13. //添加、新增数据
  14. void add(Brand brand);
  15. //修改-回显数据
  16. Brand selectById(int id);
  17. }

编写BrandMapper.xml

:::info 这里注意应用resultmap :::

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.taotao.mapper.BrandMapper">
  6. <!-- 查询所有功能-->
  7. <select id="selectAll" resultMap="brandResultMap">
  8. select *
  9. from tb_brand
  10. </select>
  11. <resultMap id="brandResultMap" type="brand">
  12. <result column="brandName" property="brand_name"></result>
  13. <result column="companyName" property="company_name"></result>
  14. </resultMap>
  15. <!-- 添加功能-->
  16. <insert id="add" >
  17. insert into tb_brand
  18. values (#{id},#{brand_name},#{company_name},#{ordered},#{description},#{status});
  19. </insert>
  20. <!-- 修改-回显数据-->
  21. <select id="selectById" resultMap="brandResultMap">
  22. select * from tb_brand where id = #{id};
  23. </select>
  24. </mapper>

编写BrandService.java

  1. package com.taotao.service;
  2. import com.taotao.mapper.BrandMapper;
  3. import com.taotao.pojo.Brand;
  4. import com.taotao.util.SqlSessionfactoryUtils;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import java.util.List;
  8. /**
  9. * create by 刘鸿涛
  10. * 2022/3/29 16:59
  11. */
  12. @SuppressWarnings({"all"})
  13. public class BrandService {
  14. SqlSessionFactory factory = SqlSessionfactoryUtils.getSqlSessionFactory();
  15. /**
  16. * 查询所有
  17. * @return
  18. */
  19. public List<Brand> selectAll(){
  20. //调用BrandMapper.selectAll()
  21. //2.获取sqlSession
  22. SqlSession sqlSession = factory.openSession();
  23. //3.获取BrandMapper
  24. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  25. //4.调用方法
  26. List<Brand> brands = mapper.selectAll();
  27. //5.关闭资源
  28. sqlSession.close();
  29. return brands;
  30. }
  31. /**
  32. * 添加数据
  33. */
  34. public void add(Brand brand){
  35. //调用BrandMapper.add(Brand brand);
  36. //2.获取sqlSession
  37. SqlSession sqlSession = factory.openSession();
  38. //3.获取BrandMapper
  39. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  40. //4.调用方法
  41. mapper.add(brand);
  42. //5.提交事务
  43. sqlSession.commit();
  44. //6.释放资源
  45. sqlSession.close();
  46. }
  47. /**
  48. * 通过id修改数据
  49. * @param id
  50. * @return
  51. */
  52. public Brand selectById(int id){
  53. //调用BrandMapper.selectById(Integer id)
  54. //获取sqlSession
  55. SqlSession sqlSession = factory.openSession();
  56. //获取BrandMapper
  57. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  58. //调用方法
  59. Brand brand = mapper.selectById(id);
  60. //提交事务
  61. sqlSession.commit();
  62. //6.释放资源
  63. sqlSession.close();
  64. return brand;
  65. }
  66. }

编写表单

编写brand.jsp

:::info 连接selectByIdServlet.java获取id :::

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: guigui
  4. Date: 2022/3/29
  5. Time: 10:32
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  10. <%@ page isELIgnored="false" %>
  11. <html>
  12. <head>
  13. <title>Title</title>
  14. </head>
  15. <body>
  16. <input type="button" value="新增" id="add"><br>
  17. <hr>
  18. <table border="1" cellspacing="0" width="%80">
  19. <tr>
  20. <th>序号</th>
  21. <th>品牌名称</th>
  22. <th>公司名称</th>
  23. <th>价格</th>
  24. <th>描述</th>
  25. <th>状态</th>
  26. </tr>
  27. <c:forEach items="${brands}" var="brand" varStatus="status">
  28. <tr align="center">
  29. <td>${brand.id}</td>
  30. <td>${brand.brand_name}</td>
  31. <td>${brand.company_name}</td>
  32. <td>${brand.ordered}</td>
  33. <td>${brand.description}</td>
  34. <c:if test="${brand.status == 1}">
  35. <td>启用</td>
  36. </c:if>
  37. <c:if test="${brand.status != 1}">
  38. <td>禁用</td>
  39. </c:if>
  40. <td><a href="/brand_demo/selectByIdServlet?id=${brand.id}">Update</a> <a href="">Delete</a></td>
  41. </tr>
  42. </c:forEach>
  43. <script>
  44. document.getElementById("add").onclick = function (){
  45. location.href = "/brand_demo/addBrand.jsp";
  46. }
  47. </script>
  48. </table>
  49. </body>
  50. </html>

编写Servlet

编写SelectByIdServlet.java

  1. package com.taotao.web;
  2. import com.taotao.pojo.Brand;
  3. import com.taotao.service.BrandService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. /**
  11. * create by 刘鸿涛
  12. * 2022/3/31 21:25
  13. */
  14. @WebServlet("/selectByIdServlet")
  15. public class SelectByIdServlet extends HttpServlet {
  16. private BrandService service = new BrandService();
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. //接收id
  20. String id = req.getParameter("id");
  21. //调用service查询
  22. Brand brand = service.selectById(Integer.parseInt(id));
  23. //存储到request中
  24. req.setAttribute("brand",brand);
  25. //转发到update.jsp
  26. req.getRequestDispatcher("/update.jsp").forward(req,resp);
  27. }
  28. @Override
  29. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  30. this.doGet(req,resp);
  31. }
  32. }

编写update.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: guigui
  4. Date: 2022/3/31
  5. Time: 12:43
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%--引用c标签--%>
  10. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  11. <%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>--%>
  12. <%--<%@ page isELIgnored="false" %>--%>
  13. <html>
  14. <head>
  15. <meta charset="UTF-8">
  16. <title>Title</title>
  17. </head>
  18. <style>
  19. * {
  20. margin: 0; /*外边距*/
  21. padding: 0; /*内边距 */
  22. font-family: "微软雅黑 Light";
  23. } /*清除所有默认样式*/
  24. :root,form { /*选择最外层标签HTML*/
  25. height: 70%;
  26. display: flex; /*改变元素*/
  27. align-items: center; /*垂直方向对齐方式*/
  28. justify-content: center; /*水平方向子项的对齐和分布方式*/
  29. }
  30. form {
  31. flex-direction: column; /*控制子项整体布局方向(从上到下)*/
  32. padding: 40px; /*上内边距*/
  33. width: 300px; /*盒子模型宽*/
  34. box-shadow: 0 15px 20px rgba(0, 0, 0, 0.5); /*应用阴影 a是alpha “透明 ”的意思*/
  35. }
  36. .form-title {
  37. margin-bottom: 20px; /*下外边距*/
  38. }
  39. .form-input,
  40. .form-radio,
  41. .form-button {
  42. width: 100%;
  43. margin-bottom: 20px; /*下外边距*/
  44. }
  45. .form-input input { /*两个选择器具有包含关系*/
  46. padding-left: 10px;
  47. height: 40px;
  48. width: 100%;
  49. box-sizing: border-box; /*盒子边框*/
  50. border: 2px solid rgba(0, 0, 0, 0.82); /*边框线宽度,样式(实线),颜色*/
  51. }
  52. .form-radio,
  53. .form-button {
  54. height: 40px;
  55. display: flex; /*控制元素类型*/
  56. align-items: center;
  57. justify-content: space-between;
  58. }
  59. .form-radio-choose {
  60. display: flex;
  61. align-items: center;
  62. }
  63. .form-radio-choose input {
  64. margin-right: 20px; /*右外边距*/
  65. height: 30px;
  66. width: 30px;
  67. }
  68. .form-button {
  69. justify-content: center;
  70. background-color: black;
  71. color: white;
  72. }
  73. </style>
  74. <body>
  75. <form action="/brand_demo/AddServlet" method="post">
  76. <div class="form-title">
  77. <h2>
  78. Update Data
  79. </h2>
  80. </div> <!--块容器标记实现网页的规划和布局-->
  81. <div class="form-input">
  82. <input type="text" placeholder="Brand Name:" name="brand_name" value="${brand.brand_name}">
  83. </div>
  84. <div class="form-input">
  85. <input type="text" placeholder="Company Name:" name="company_name" value="${brand.company_name}">
  86. </div>
  87. <div class="form-input">
  88. <input type="text" placeholder="Price:" name="ordered" value="${brand.ordered}">
  89. </div>
  90. <div class="form-input">
  91. <input type="text" placeholder="Remarks:" name="description" value="${brand.description}">
  92. </div>
  93. <c:if test="${brand.status == 0}">
  94. <div class="form-radio">
  95. <div class="form-radio-title">Status:</div>
  96. <div class="form-radio-choose">
  97. <input type="radio" checked value="1" name="status" />On
  98. </div>
  99. <div class="form-radio-choose">
  100. <input type="radio" value="0" name="status"/>Off
  101. </div>
  102. </div>
  103. </c:if>
  104. <c:if test="${brand.status == 1}">
  105. <div class="form-radio">
  106. <div class="form-radio-title">Status:</div>
  107. <div class="form-radio-choose">
  108. <input type="radio" value="1" name="status" />On
  109. </div>
  110. <div class="form-radio-choose">
  111. <input type="radio" checked value="0" name="status"/>Off
  112. </div>
  113. </div>
  114. </c:if>
  115. <div class="form-button">
  116. <%-- <input type="radio" name="status" value="0">禁用--%>
  117. <%-- <input type="radio" name="status" value="1" >启用--%>
  118. <input class="form-button" type="submit" value="Submit">
  119. </div>
  120. <%-- <div class="form-button">--%>
  121. <%--&lt;%&ndash; <p>重置</p>&ndash;%&gt;--%>
  122. <%-- </div>--%>
  123. </form>
  124. </body>
  125. </html>

运行测试

:::info 回显数据完成,鼠标指针放在update选项时左下角网页可以得到id ::: image.png

回显功能

:::info 只有回显功能,不能通过此页面更改相应id数据 ::: image.png

修改-修改数据

流程图


image.png

编写BrandMapper.java

  1. package com.taotao.mapper;
  2. import com.taotao.pojo.Brand;
  3. import org.apache.ibatis.annotations.ResultMap;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.util.List;
  6. /**
  7. * create by 刘鸿涛
  8. * 2022/3/29 16:12
  9. */
  10. public interface BrandMapper {
  11. //查询所有
  12. List<Brand> selectAll();
  13. //添加、新增数据
  14. void add(Brand brand);
  15. //修改-回显数据
  16. Brand selectById(int id);
  17. //修改-修改数据
  18. void update(Brand brand);
  19. }

编写BrandMapper.xml

  1. <!-- 修改-修改数据-->
  2. <update id="update">
  3. update tb_brand set brandName = #{brand_name},
  4. companyName = #{company_name},
  5. ordered = #{ordered},
  6. description = #{description},
  7. status = #{status} where id = #{id};
  8. </update>

编写Service

:::info 编写BrandService :::

  1. /**
  2. * 修改
  3. * @param brand
  4. */
  5. public void update(Brand brand){
  6. //调用BrandMapper.add(Brand brand);
  7. //2.获取sqlSession
  8. SqlSession sqlSession = factory.openSession();
  9. //3.获取BrandMapper
  10. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
  11. //4.调用方法
  12. mapper.update(brand);
  13. //5.提交事务
  14. sqlSession.commit();
  15. //6.释放资源
  16. sqlSession.close();
  17. }

修改update.jsp

:::info 修改update.jsp数据传输到servlet
注意要传入id到servlet
注意id不要显示在update.jsp页面中(隐藏域),type = hidden :::

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: guigui
  4. Date: 2022/3/31
  5. Time: 12:43
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%--引用c标签--%>
  10. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  11. <%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>--%>
  12. <%--<%@ page isELIgnored="false" %>--%>
  13. <html>
  14. <head>
  15. <meta charset="UTF-8">
  16. <title>Title</title>
  17. </head>
  18. <style>
  19. * {
  20. margin: 0; /*外边距*/
  21. padding: 0; /*内边距 */
  22. font-family: "微软雅黑 Light";
  23. } /*清除所有默认样式*/
  24. :root,form { /*选择最外层标签HTML*/
  25. height: 70%;
  26. display: flex; /*改变元素*/
  27. align-items: center; /*垂直方向对齐方式*/
  28. justify-content: center; /*水平方向子项的对齐和分布方式*/
  29. }
  30. form {
  31. flex-direction: column; /*控制子项整体布局方向(从上到下)*/
  32. padding: 40px; /*上内边距*/
  33. width: 300px; /*盒子模型宽*/
  34. box-shadow: 0 15px 20px rgba(0, 0, 0, 0.5); /*应用阴影 a是alpha “透明 ”的意思*/
  35. }
  36. .form-title {
  37. margin-bottom: 20px; /*下外边距*/
  38. }
  39. .form-input,
  40. .form-radio,
  41. .form-button {
  42. width: 100%;
  43. margin-bottom: 20px; /*下外边距*/
  44. }
  45. .form-input input { /*两个选择器具有包含关系*/
  46. padding-left: 10px;
  47. height: 40px;
  48. width: 100%;
  49. box-sizing: border-box; /*盒子边框*/
  50. border: 2px solid rgba(0, 0, 0, 0.82); /*边框线宽度,样式(实线),颜色*/
  51. }
  52. .form-radio,
  53. .form-button {
  54. height: 40px;
  55. display: flex; /*控制元素类型*/
  56. align-items: center;
  57. justify-content: space-between;
  58. }
  59. .form-radio-choose {
  60. display: flex;
  61. align-items: center;
  62. }
  63. .form-radio-choose input {
  64. margin-right: 20px; /*右外边距*/
  65. height: 30px;
  66. width: 30px;
  67. }
  68. .form-button {
  69. justify-content: center;
  70. background-color: black;
  71. color: white;
  72. }
  73. </style>
  74. <body>
  75. <form action="/brand_demo/UpdateServlet" method="post">
  76. <div class="form-title">
  77. <h2>
  78. Update Data
  79. </h2>
  80. </div> <!--块容器标记实现网页的规划和布局-->
  81. <%-- 隐藏域,提交id--%>
  82. <input type="hidden" name="id" value="${brand.id}">
  83. <div class="form-input">
  84. <input type="text" placeholder="Brand Name:" name="brand_name" value="${brand.brand_name}">
  85. </div>
  86. <div class="form-input">
  87. <input type="text" placeholder="Company Name:" name="company_name" value="${brand.company_name}">
  88. </div>
  89. <div class="form-input">
  90. <input type="text" placeholder="Price:" name="ordered" value="${brand.ordered}">
  91. </div>
  92. <div class="form-input">
  93. <input type="text" placeholder="Remarks:" name="description" value="${brand.description}">
  94. </div>
  95. <c:if test="${brand.status == 0}">
  96. <div class="form-radio">
  97. <div class="form-radio-title">Status:</div>
  98. <div class="form-radio-choose">
  99. <input type="radio" checked value="1" name="status" />On
  100. </div>
  101. <div class="form-radio-choose">
  102. <input type="radio" value="0" name="status"/>Off
  103. </div>
  104. </div>
  105. </c:if>
  106. <c:if test="${brand.status == 1}">
  107. <div class="form-radio">
  108. <div class="form-radio-title">Status:</div>
  109. <div class="form-radio-choose">
  110. <input type="radio" value="1" name="status" />On
  111. </div>
  112. <div class="form-radio-choose">
  113. <input type="radio" checked value="0" name="status"/>Off
  114. </div>
  115. </div>
  116. </c:if>
  117. <div class="form-button">
  118. <%-- <input type="radio" name="status" value="0">禁用--%>
  119. <%-- <input type="radio" name="status" value="1" >启用--%>
  120. <input class="form-button" type="submit" value="Submit">
  121. </div>
  122. <%-- <div class="form-button">--%>
  123. <%--&lt;%&ndash; <p>重置</p>&ndash;%&gt;--%>
  124. <%-- </div>--%>
  125. </form>
  126. </body>
  127. </html>

编写updateServlet.java

  1. package com.taotao.web;
  2. import com.taotao.pojo.Brand;
  3. import com.taotao.service.BrandService;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. /**
  11. * create by 刘鸿涛
  12. * 2022/3/31 12:02
  13. */
  14. @WebServlet("/UpdateServlet")
  15. public class UpdateServlet extends HttpServlet {
  16. private BrandService service = new BrandService();
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. //1.调用BrandService完成添加
  20. String id = req.getParameter("id");
  21. String brandName = req.getParameter("brand_name");
  22. String companyName = req.getParameter("company_name");
  23. String ordered = req.getParameter("ordered");
  24. String description = req.getParameter("description");
  25. String status = req.getParameter("status");
  26. //封装为一个Brand对象
  27. Brand brand = new Brand();
  28. brand.setId(Integer.parseInt(id));
  29. brand.setBrand_name(brandName);
  30. brand.setCompany_name(companyName);
  31. brand.setOrdered(Integer.parseInt(ordered));
  32. brand.setDescription(description);
  33. brand.setStatus(Integer.parseInt(status));
  34. //2.调用add方法,传入brand对象
  35. service.update(brand);
  36. //3.存入到requset域中
  37. // req.setAttribute("brands",brand);
  38. //4.转发到查询所有servlet中
  39. req.getRequestDispatcher("/selectAllServlet").forward(req,resp);
  40. }
  41. @Override
  42. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  43. //处理Post请求的乱码问题
  44. req.setCharacterEncoding("utf-8");
  45. this.doGet(req, resp);
  46. }
  47. }

测试运行image.pngimage.pngimage.png