首页 > 其他分享 >案例-添加

案例-添加

时间:2023-06-30 13:55:18浏览次数:26  
标签:brand request 案例 添加 import com Brand itheima

 

package com.itheima.mapper;

import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BrandMapper {
    /*
    * 查询所有
    * */
    @Select("select * from tb_brand")
    @ResultMap("brandResultMap")
    List<Brand> selectAll();

    /*
    * 添加
    * */
    @Insert("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
    void add(Brand brand);
}
package com.itheima.service;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
        //1. 使用工具类获util取到getSqlSessionFactory
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    /*
    * 查询所有
    * */
    public List<Brand> selectAll(){
        // 调用BrandMapper.selectAll()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4. 调用方法selectAll()
        List<Brand> brands=mapper.selectAll();

        //返回brand集合,释放资源
        sqlSession.close();
        return brands;
    }

    /*
    * 添加
    * */
    public void add(Brand brand){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        mapper.add(brand);

        //提交事务
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }
}
package com.itheima.web;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandService service=new BrandService();

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

        //处理POST请求的乱码问题
        request.setCharacterEncoding("utf-8");

        //1. 接受表单提交的数据,封装为一个Brand对象
        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");

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

        //2.调用servlet 完成添加
        service.add(brand);

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


    }

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

}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="新增" id="add"><br>
<hr>
<table border="1" cellspacing="0" width="80%">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>

    </tr>

    <c:forEach items="${brands}" var="brand" varStatus="status">
        <tr align="center">
<%--            <td>${brand.id}</td>--%>
<%--            index 从0开始排序  count 表示用1开始排序 --%>
            <td>${status.count}</td>
            <td>${brand.brandName}</td>
            <td>${brand.companyName}</td>
            <td>${brand.ordered}</td>
            <td>${brand.description}</td>
            <c:if test="${brand.status == 1}">
                <td>启用</td>
            </c:if>
            <c:if test="${brand.status != 1}">
                <td>禁用</td>
            </c:if>
            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>
    </c:forEach>

</table>

<script>
    document.getElementById("add").onclick = function () {
        location.href = "/brand-demo/addBrand.jsp";
    }
</script>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2023/6/30
  Time: 13:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>添加品牌</title>
</head>
<body>
<h3>添加品牌</h3>
<form action="/brand-demo/addServlet" 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>

 

标签:brand,request,案例,添加,import,com,Brand,itheima
From: https://www.cnblogs.com/Karl-hut/p/17516607.html

相关文章

  • Dynamics 365 主表修改了未保存,显示“未保存的更改”时,不可添加明细记录
     实现方式,在明细表的新增按钮,设置为可自定义,绑定函数://授权记录显示“未保存的更改”时不可添加授权产品functionIsSavedAuthorize(selectedEntityTypeName,primaryEntityTypeName,firstPrimaryItemId,primaryControl,selectedControl){if(Xrm.Page.data.getIsD......
  • 小游戏——贪吃蛇案例
    一、案例目的 1、实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子,上下左右控制“蛇”的移动,吃到“豆子”以后“蛇”的身体加长一点。2、“蛇”碰到边界或蛇头与蛇身相撞,蛇死亡,游戏结束。3、为游戏设计友好的交互界面;例如欢迎界面,游戏界面,游戏结束界面。要有开始键......
  • TodoList案例
       案例介绍文本框里面输入内容,按下回车,就可以生成待办事项。点击待办事项复选框,就可以把当前数据添加到已完成事项里面。点击已完成事项复选框,就可以把当前数据添加到待办事项里面。但是本页面内容刷新页面不会丢失。toDoList分析以及注意点刷新页面不会丢失数据,......
  • 敏感词过滤--DFA算法及代码案例
    我们应该都遇见过敏感词过滤,比如当我们输入一些包含暴力或者色情的文本,系统会阻止信息提交。敏感词过滤就是检查用户输入的内容有没有敏感词,检查之后有两个策略。直接阻止信息保存,接口返回错误信息允许信息保存,但是会把敏感词替换为***不管是哪种策略,首先都得找到是否包含敏......
  • 三菱PLC项目案例学习之PLC控制伺服或步进电机带动丝运行案例
    三菱PLC项目案例学习之PLC控制伺服或步进电机带动丝运行案例。器件:三菱FX1SPLC,威纶通触摸屏,48步进驱动器,伺服电机,丝杆滑台等。控制方式:PLC发脉冲给步进驱动器控制步进电机带动丝杆滑台同步运行。功能:学到:丝杆实际行走的距离转化成工程量脉冲个数的程序编写计算,fx1sPLC没有浮......
  • 昆仑通态MCGS与台达伺服ASD-B2 通讯控制案例功能:通过昆仑通态MCGS触摸屏实现与台达AS
    昆仑通态MCGS与台达伺服ASD-B2 通讯控制案例功能:通过昆仑通态MCGS触摸屏实现与台达ASDB2通讯控制,全通讯控制台达B2驱动器速度设置,速度选择,速度启动,带扭矩限制速度控制,扭矩限制通讯设置。配件:昆仑通态MCGS触摸屏,台达ASDB2伺服驱动器+伺服电机。CN1接线端子,CN3伺服驱动器通讯......
  • 案例-准备环境
       mybatis-config.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><c......
  • .NET 个人博客-添加RSS订阅功能
    个人博客-添加RSS订阅功能前言个人博客系列已经完成了留言板文章归档推荐文章优化推荐文章排序博客地址:https://pljzy.top然后博客开源的原作者也是百忙之中添加了一个名为RSS订阅的功能,那么我就来简述一下这个功能是干嘛的,然后照葫芦画瓢实现一下。RSS简述来自ch......
  • 亚马逊国际获得AMAZON商品详情API接口采集sku商品规格信息列表Java调用演示案例
    ​亚马逊商品详情API接口的作用是获取Lazada电商平台上的某一商品的详情信息,包括商品的名称、销售价格、库存数量、图片、商品描述、品牌、产地、售后保障等信息。开发者可以使用该API接口获取到商品的原始数据,进行分析、筛选等操作。通过该接口获取到的商品详情数据可以结合其......
  • 【python爬虫案例】用python爬豆瓣电影TOP250排行榜!
    一、爬虫对象-豆瓣电影TOP250前几天,我分享了一个python爬虫案例,爬取豆瓣读书TOP250数据:【python爬虫案例】用python爬豆瓣读书TOP250排行榜!今天,我再分享一期,python爬取豆瓣电影TOP250数据!爬虫大体流程和豆瓣读书TOP250类似,细节之处见逻辑。首先,打开豆瓣电影TOP250的页面:https......