添加数据

流程图

image.png

编写BrandMapper接口

  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. }

编写BrandMapper.xml

:::info 可以再BrandMapper.java中生成statement,alt + enter自动识别 :::

  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. # (brandName,companyName,ordered,description,status)
  19. values (#{id},#{brand_name},#{company_name},#{ordered},#{description},#{status});
  20. </insert>
  21. </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. }

点击“新增”跳转到JSP表单

编写index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <a href="/brand_demo/selectAllServlet">查询所有</a>
  9. </body>
  10. </html>

编写brand.jsp

:::info 点击“新增”,转到addBrand.jsp :::

  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="800">
  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. </tr>
  41. </c:forEach>
  42. <script>
  43. document.getElementById("add").onclick = function (){
  44. location.href = "/brand_demo/addBrand.jsp";
  45. }
  46. </script>
  47. </table>
  48. </body>
  49. </html>

编写addBrand.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. <%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>--%>
  10. <%--<%@ page isELIgnored="false" %>--%>
  11. <html>
  12. <head>
  13. <meta charset="UTF-8">
  14. <title>Title</title>
  15. </head>
  16. <body>
  17. <h3>添加品牌</h3>
  18. <form action="/brand_demo/AddServlet" method="post" id="aa">
  19. <%-- 序号:<input type="text" name="id"><br>--%>
  20. 品牌名称:<input type="text" name="brand_name"><br>
  21. 企业名称:<input type="text" name="company_name"><br>
  22. 排序:<input type="text" name="ordered"><br>
  23. 描述信息:<input type="text" name="description">
  24. 状态:<input type="radio" name="status" value="0">禁用
  25. <input type="radio" name="status" value="1" >启用
  26. <input type="submit" >
  27. </form>
  28. </body>
  29. </html>

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

编写AddServlet

:::info com.taotao.web.AddServlet.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("/AddServlet")
  15. public class AddServlet 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 brandName = req.getParameter("brand_name");
  21. String companyName = req.getParameter("company_name");
  22. String ordered = req.getParameter("ordered");
  23. String description = req.getParameter("description");
  24. String status = req.getParameter("status");
  25. //封装为一个Brand对象
  26. Brand brand = new Brand();
  27. brand.setBrand_name(brandName);
  28. brand.setCompany_name(companyName);
  29. brand.setOrdered(Integer.parseInt(ordered));
  30. brand.setDescription(description);
  31. brand.setStatus(Integer.parseInt(status));
  32. //2.调用add方法,传入brand对象
  33. service.add(brand);
  34. //3.存入到requset域中
  35. // req.setAttribute("brands",brand);
  36. //4.转发到查询所有servlet中
  37. req.getRequestDispatcher("/selectAllServlet").forward(req,resp);
  38. }
  39. @Override
  40. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  41. //处理Post请求的乱码问题
  42. req.setCharacterEncoding("utf-8");
  43. this.doGet(req, resp);
  44. }
  45. }

测试运行

:::info 虽然中途遇到无数个bug,但是依然完美运行
解决的bug有form写成from的,导致提交表单无响应
网页提交404,路径指引错误
post请求中文乱码,在servlet,post请求里设置去utf-8字符编码即可 ::: image.png
image.png