【SSM项目】02_后端实现添加、修改、删除、批量删除功能

接上篇文章继续实现功能

【SSM项目】01_PageHelper插件后端实现分页查询操作

本篇文章主要实现添加、修改、删除、批量删除功能
效果展示:
在这里插入图片描述添加修改界面(同一jsp界面):
在这里插入图片描述在这里插入图片描述

1.dao层代码

   /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Video queryById(Integer id);

    /**
     * 通过实体作为筛选条件查询
     *
     * @param video 实例对象
     * @return 对象列表
     */
    List<Video> queryAll(Video video);

    /**
     * 新增数据
     *
     * @param video 实例对象
     * @return 影响行数
     */
    int insert(Video video);

    /**
     * 修改数据
     *
     * @param video 实例对象
     * @return 影响行数
     */
    int update(Video video);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Integer id);
1.1 VideoDao.xml代码

有的sql语句逆向工程直接生成的哦~大家看看就行…

  <!--查询单个-->
    <select id="queryById" resultMap="VideoMap">
        SELECT id,title,speaker_id,course_id,time,image_url,video_url,detail
        FROM video
        WHERE id = #{id}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="VideoMap">
        select
        id, title, detail, time, speaker_id, course_id, video_url, image_url, play_num
        from video.video
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="title != null and title != ''">
                and title = #{title}
            </if>
            <if test="detail != null and detail != ''">
                and detail = #{detail}
            </if>
            <if test="time != null">
                and time = #{time}
            </if>
            <if test="speakerId != null">
                and speaker_id = #{speakerId}
            </if>
            <if test="courseId != null">
                and course_id = #{courseId}
            </if>
            <if test="videoUrl != null and videoUrl != ''">
                and video_url = #{videoUrl}
            </if>
            <if test="imageUrl != null and imageUrl != ''">
                and image_url = #{imageUrl}
            </if>
            <if test="playNum != null">
                and play_num = #{playNum}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into video.video(title, detail, time, speaker_id, course_id, video_url, image_url, play_num)
        values (#{title}, #{detail}, #{time}, #{speakerId}, #{courseId}, #{videoUrl}, #{imageUrl}, #{playNum})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update video.video
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="detail != null and detail != ''">
                detail = #{detail},
            </if>
            <if test="time != null">
                time = #{time},
            </if>
            <if test="speakerId != null">
                speaker_id = #{speakerId},
            </if>
            <if test="courseId != null">
                course_id = #{courseId},
            </if>
            <if test="videoUrl != null and videoUrl != ''">
                video_url = #{videoUrl},
            </if>
            <if test="imageUrl != null and imageUrl != ''">
                image_url = #{imageUrl},
            </if>
            <if test="playNum != null">
                play_num = #{playNum},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from video.video where id = #{id}
    </delete>

2.service层代码

VideoService.java

    /**
     * 通过实体作为筛选条件查询
     *
     * @param video 实例对象
     * @return 对象列表
     */
    List<Video> queryAll(Video video);

    /**
     * 新增数据
     *
     * @param video 实例对象
     * @return 影响行数
     */
    int insert(Video video);

    /**
     * 修改数据
     *
     * @param video 实例对象
     * @return 影响行数
     */
    int update(Video video);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Integer id);

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Video queryById(Integer id);

VideoServiceImpl.java

    @Override
    public List<Video> queryAll(Video video) {
        return videoDao.queryAll(video);
    }

    @Override
    public int insert(Video video) {
        return videoDao.insert(video);
    }

    @Override
    public int update(Video video) {
        return videoDao.update(video);
    }

    @Override
    public int deleteById(Integer id) {
        return videoDao.deleteById(id);
    }

    @Override
    public Video queryById(Integer id) {
        return videoDao.queryById(id);
    }

3.controller层代码

  /**
     * 修改前的查询操作
     * @param id
     * @return
     */
    @RequestMapping("queryById")
    public String queryById(Model model,Integer id) {

        Video video = videoService.queryById(id);
        model.addAttribute("video",video);

        //课程名
        List<Course> allCourse = courseService.findAllCourse();
        model.addAttribute("allCourse",allCourse);

        //主讲人
        List<Speaker> allSpeaker = speakerService.findAllSpeaker();
        model.addAttribute("allSpeaker",allSpeaker);

        return "/behind/addVideo";
    }

    /**
     * 新增数据
     *
     * @param video 实例对象
     * @return 添加/修改界面
     */
    @RequestMapping("/addOrUpdateVideo")
    public String insert(Video video,Model model,Integer id){

        //课程名
        List<Course> allCourse = courseService.findAllCourse();
        model.addAttribute("allCourse",allCourse);

        //主讲人
        List<Speaker> allSpeaker = speakerService.findAllSpeaker();
        model.addAttribute("allSpeaker",allSpeaker);

        //视频信息
        model.addAttribute("video",video);

        if (video.getId() == null) {
            videoService.insert(video);
        } else {
            //修改
            video.setId(id);
           videoService.update(video);
        }

        return "redirect:/video/findByPage";
    }
    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 删除该行
     */
    @RequestMapping("/videoDel")
    @ResponseBody
    public String deleteById(Integer id) {
        int flag = videoService.deleteById(id);
        if (flag == 1) {
            return "success";
        } else {
            return "false";
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delAll")
    public String delAll(Integer [] ids){

        for(Integer id:ids){

            videoService.deleteById(id);
            System.out.println(id);
        }
        return "redirect:/video/findByPage";
    }
}

4.jsp页面

addVideo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">

    <!--表示使用IE最新的渲染模式进行解析-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--
    	兼容一些移动设备,会根据屏幕的大小缩放
    	width=device-width  表示宽度是设备的宽度(很多手机的宽度都是980px)
    	initial-scale=1  初始化缩放级别   1-5
    	minimum-scale=1  maximum-scale=5
    	user-scalable=no  禁止缩放
    -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>添加或修改视频</title>

    <!-- Bootstrap -->
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->

    <!-- 如果IE版本小于9,加载以下js,解决低版本兼容问题 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


    <!--
    	引入网络的jquery  ,如果想换成自己的,导入即可
    	网站优化:建议将你网站的css\js等代码,放置在互联网公共平台上维护,比如:七牛
    -->
    <script src="${pageContext.request.contextPath}/js/jquery-1.12.4.min.js"></script>

    <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
    <style type="text/css">
        th {
            text-align: center;
        }
    </style>
    <script type="text/javascript">

        function showName(obj, id, type) {

            if (type == 1) {
                //想获取下拉列表选中的值
                var chooseName = $(obj).text();
                // 将获取的值显示在输入框内
                $("#speakerName").val(chooseName);
                //想给隐藏的文本赋值
                $("#speakerId").val(id);
            } else {
                var chooseName = $(obj).text();
                // 将获取的值显示在输入框内
                $("#courseName").val(chooseName);
                //想给隐藏的文本赋值
                $("#courseId").val(id);
            }

        }


        //页面加载完毕之后就执行以下代码片段
        $(function () {
            var speakerId = '${video.speakerId}';
            $("#selectSpeaker li").each(function () {

                if ($(this).val() == speakerId) {
                    $("#speakerName").val($(this).text());
                }
            });

            var courseId = '${video.courseId}';
            $("#selectCourse li").each(function () {

                if ($(this).val() == courseId) {
                    $("#courseName").val($(this).text());
                }
            });


        });

    </script>

</head>
<body>


<nav class="navbar-inverse">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">

            <a class="navbar-brand" href="${pageContext.request.contextPath}/video/findByPage">视频管理系统</a>
        </div>

        <div class="collapse navbar-collapse"
             id="bs-example-navbar-collapse-9">
            <ul class="nav navbar-nav">
                <li class="active"><a href="${pageContext.request.contextPath}/video/findByPage">视频管理</a></li>
                <li><a href="${pageContext.request.contextPath}/speaker/findByPage">主讲人管理</a></li>
                <li><a href="${pageContext.request.contextPath}/showCourseList">课程管理</a></li>


            </ul>
            <p class="navbar-text navbar-right">
                <span>${sessionScope.userName}</span> <i class="glyphicon glyphicon-log-in"
                                                         aria-hidden="true"></i>&nbsp;&nbsp;<a
                    href="${pageContext.request.contextPath}/admin/exit"
                    class="navbar-link">退出</a>
            </p>
        </div>
        <!-- /.navbar-collapse -->


    </div>
    <!-- /.container-fluid -->
</nav>

<div class="jumbotron" style="padding-top: 15px;padding-bottom: 15px;">
    <div class="container">

        <%-- <c:if test="empty ${video.id}"> --%>
        <c:if test="${empty video.id}">
            <h2>添加视频信息</h2>
        </c:if>

        <c:if test="${not empty video.id}">
            <h2>修改视频信息</h2>
        </c:if>

    </div>
</div>


<div class="container" style="margin-top: 20px;">

    <form class="form-horizontal" action="${pageContext.request.contextPath}/video/addOrUpdateVideo" method="post">


        <c:if test="${not empty video.id}">
            <input type="hidden" name="id" value="${video.id}">
        </c:if>

        <div class="form-group">
            <label class="col-sm-2 control-label">名称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="title" value="${video.title}" placeholder="视频名称">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">教师名字</label>
            <div class="col-sm-10">
                <div class="input-group">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false">下拉菜单<span class="caret"></span></button>
                        <ul id="selectSpeaker" class="dropdown-menu">
                            <c:forEach items="${allSpeaker}" var="speaker">
                                <li value='${speaker.id}'><a href="#"
                                                             onclick="showName(this,'${speaker.id}',1)">${speaker.speakerName}</a>
                                </li>
                            </c:forEach>
                            <!-- <li ><a href="#" οnclick="showName(this,1,1)">闫振伟</a></li>
                            <li ><a href="#" οnclick="showName(this,2,1)">李文魁</a></li>
                            <li ><a href="#" οnclick="showName(this,3,1)">石军培</a></li>  -->
                        </ul>
                    </div><!-- /btn-group -->
                    <c:if test="${empty video.speakerId}">
                        <input type="hidden" class="form-control" id="speakerId" name="speakerId" value="0">
                    </c:if>
                    <c:if test="${not empty video.speakerId}">
                        <input type="hidden" class="form-control" id="speakerId" name="speakerId"
                               value="${video.speakerId}">
                    </c:if>
                    <input type="text" class="form-control" disabled id="speakerName" aria-label="...">
                </div><!-- /input-group -->
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">所属课程</label>
            <div class="col-sm-10">
                <div class="input-group">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                                aria-haspopup="true" aria-expanded="false">下拉菜单<span class="caret"></span></button>
                        <ul id="selectCourse" class="dropdown-menu">
                            <c:forEach items="${allCourse}" var="course">
                                <li value="${course.id}"><a href="#"
                                                            onclick="showName(this,${course.id},2)">${course.courseTitle}</a>
                                </li>
                            </c:forEach>

                        </ul>
                    </div><!-- /btn-group -->
                    <c:if test="${empty video.courseId}">
                        <input type="hidden" class="form-control" id="courseId" name="courseId" value="0">
                    </c:if>
                    <c:if test="${not empty video.courseId}">
                        <input type="hidden" class="form-control" id="courseId" name="courseId" value="${video.courseId}">
                    </c:if>
                    <input type="text" class="form-control" disabled id="courseName" aria-label="...">
                </div><!-- /input-group -->
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">时长</label>
            <div class="col-sm-10">
                <c:if test="${empty video.time}">
                    <input type="number" class="form-control" value="0" name="time" placeholder="精确到毫秒(正整数)">
                </c:if>
                <c:if test="${not empty video.time}">
                    <input type="number" class="form-control" value="${video.time}" name="time"
                           placeholder="精确到毫秒(正整数)">
                </c:if>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">封面图片地址</label>
            <div class="col-sm-10">
                <input type="url" name="imageUrl" class="form-control" value="${video.imageUrl}" placeholder="具体的url">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">视频播放地址</label>
            <div class="col-sm-10">
                <input type="url" name="videoUrl" class="form-control" value="${video.videoUrl}" placeholder="具体的url">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">备注</label>
            <div class="col-sm-10">
                <textarea class="form-control" name="detail" rows="3">${video.detail}</textarea>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">保存</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>
实现博客类别的添加修改删除之前,需要先创建相应的数据库表和对应的实体类。 假设我们创建了一个名为“blog_category”的表,包含以下字段: - id:主键,自增长 - name:类别名称 - description:类别描述 接下来,我们按照以下步骤实现博客类别的添加修改删除: **1. 添加类别** 首先,在后台管理系统中添加一个“添加类别”的页面,包含类别名称和类别描述的输入框以及一个“提交”按钮。 在提交按钮的点击事件中,将输入框中的数据通过 AJAX 发送到后台,调用相应的 Service 方法将数据插入到数据库中。 **2. 修改类别** 在后台管理系统中添加一个“修改类别”的页面,与“添加类别”页面类似,但是需要在页面加载时将当前类别的名称和描述显示在输入框中。 在提交按钮的点击事件中,将输入框中的数据通过 AJAX 发送到后台,调用相应的 Service 方法将数据更新到数据库中。 **3. 删除类别** 在后台管理系统中添加一个“删除类别”的功能,例如在类别列表中每一行后面添加一个“删除”按钮。 在按钮的点击事件中,通过 AJAX 发送请求到后台,调用相应的 Service 方法将该类别从数据库中删除。 以上就是博客类别的添加修改删除实现流程。需要注意的是,在实现过程中需要考虑异常情况的处理,例如插入重复的类别、删除不存在的类别等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky__cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值