SSM+AJAX+jsp添加数据功能

2021.5.7_SSM+AJAX开发,添加功能

1.StudentMapper

package com.hy.mapper;

import com.hy.entity.Student;

import java.util.List;

public interface StudentMapper {

    //判断学号是否存在
    public Student selectSId(String studentId);

    //添加
    public int addStudent(Student student);


}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hy.mapper.StudentMapper">

 
    <!--判断学号是否重复-->
    <select id="selectSId" parameterType="String" resultType="Student">
        select * from student where studentId = #{studentId};
    </select>

    <!--添加-->
    <insert id="addStudent" parameterType="com.hy.entity.Student">
        insert into student (studentId,studentName,college,major,className) values
                    (#{studentId},#{studentName},#{college},#{major},#{className})
    </insert>

</mapper>

2.StudentService

package com.hy.service;

import com.hy.entity.Student;

import java.util.List;

public interface StudentService {

    //判断学号是否存在
    public boolean selectSId(String studentId);

    //添加
    public int addStudent(Student student);

}

3.StudentServiceImpl

package com.hy.service;

import com.hy.entity.Student;
import com.hy.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    //判断学号是否存在
    @Override
    public boolean selectSId(String studentId) {
        Student stuId = studentMapper.selectSId(studentId);
        if (stuId != null){
            return false;
        }
        return true;

    }

    //添加
    @Override
    public int addStudent(Student student) {
        return studentMapper.addStudent(student);
    }

}

4.StudentController

package com.hy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.entity.Student;
import com.hy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //判断学号
    @ResponseBody //此注解是返回JSON字符串
    @RequestMapping("/selectSId")
    public boolean selectSId(String studentId){
        boolean flag = studentService.selectSId(studentId);
        return flag;
    }

    //添加
    @ResponseBody
    @RequestMapping("/addStudent")
    public String addStudent(@RequestBody Student student){
       return studentService.addStudent(student)>0? "success":"error";
    }

}

5.jsp

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2021/5/5
  Time: 16:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Student</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.css">
    <%--jQuery需要在bootstrap.前面--%>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.0.0/jquery.js"></script>
    <script src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.js"></script>

    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .form-inline .form-group .btn{
              outline: none;
          }

        .table{
            text-align: center;
            margin-top: 10px;
        }
        .alert{
            display: none;
            width: auto;
            text-align: center;
            position: fixed;
            top: 20%;
            left:50%;

        }
        #delete_success,#add_success,#update_success{
            background:#19be6b;
            color: #ffff;
        }
        #delete_cancel,#add_cancel,#update_cancel{
            background: #ed4014;
            color: #ffffff;
        }
    </style>
</head>
<body>

<%--添加开始--%>
<div class="modal fade" id="addStudent" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content" style="border-radius: 15px;">
            <div class="modal-header" style="background:#515a6e;border-radius:15px 15px 0 0;color: #ffffff">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true" style="color: #ffffff">&times;</span>
                </button>
                <h4 style="text-align: center">添加</h4>
            </div>
            <div class="modal-body">
                <form  class="form-horizontal" id="myForm">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">学号:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="addStudentId"  name="studentId" ><span id="msg"></span><br>
                        </div>
                    </div>
                    <p></p>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">姓名:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="addStudentName" name="studentName" >
                        </div>
                    </div>
                    <p></p>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">学院:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="addCollege" name="college" >
                        </div>
                    </div>
                    <p></p>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">专业:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="addMajor" name="major" >
                        </div>
                    </div>
                    <p></p>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">班级:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="addClassName" name="className" >
                        </div>
                    </div>
                    <p></p>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary"  id="add_ok" data-toggle="modal" data-target="#addStudent">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" id="cancel_add">取消</button>
            </div>
        </div>
    </div>
</div>
<%--添加结束--%>

<%--提示框开始--%>
<div class="alert alert-success" id="delete_success" role="alert">
    <span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span>&nbsp;删除成功
</div>
<div class="alert alert-danger" id="delete_cancel" role="alert">
    <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>&nbsp;已取消删除
</div>
<div class="alert alert-success" id="add_success" role="alert">
    <span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span>&nbsp;添加成功
</div>
<div class="alert alert-danger" id="add_cancel" role="alert">
    <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>&nbsp;已取消添加
</div>
<div class="alert alert-success" id="update_success" role="alert">
    <span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span>&nbsp;修改成功
</div>
<div class="alert alert-danger" id="update_cancel" role="alert">
    <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>&nbsp;已取消修改
</div>
<%--提示框结束--%>
</body>
<script>

    /*失去焦点交互数据*/
    $(function (){
        $("#addStudentId").blur(function (){ /*blur失去焦点*/
            var studentId = $("#addStudentId").val();
            if(studentId != ""){
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/student/selectSId",
                    data:{"studentId":studentId},
                    dataType:"json",
                    success:function (data){
                        if (data) {
                            $("#msg").text("学号可以用");
                            $("#addStudentId").css('borderColor','green');
                            $("#msg").css('color','green')

                        }
                        else{
                            $("#msg").text("学号已被占用");
                            $("#addStudentId").css('borderColor','red');
                            $("#msg").css('color','red')
                        }
                    }
                });
            }
            else{
                $("#msg").text("");
            }
        });
        $("#add_ok").click(function (){
            var studentId = $("#addStudentId").val();
            var studentName = $("#addStudentName").val();
            var college = $("#addCollege").val();
            var major = $("#addMajor").val();
            var className = $("#addClassName").val();
            var m = $("#msg").text();
            if (m == "学号可以用"){
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/student/addStudent",
                    data:JSON.stringify({
                        "studentId":studentId,
                        "studentName":studentName,
                        "college":college,
                        "major":major,
                        "className":className}),
                    dataType: "text",
                    contentType: "application/json;charset=utf-8",
                    success:function (msg){
                        if(msg == "success"){
                            $("#addStudentId").val("");
                            $("#addStudentName").val("");
                            $("#addCollege").val("");
                            $("#addMajor").val("");
                            $("#addClassName").val("");
                            $("#msg").val("");
                            select();
                            $("#add_success").show().delay(1500).fadeOut();
                        }
                    }
                });
            }
            else if(m == "学号已被占用"){
                $("#addStudentId").val("");
                $("#addStudentName").val("");
                $("#addCollege").val("");
                $("#addMajor").val("");
                $("#addClassName").val("");
                $("#msg").text("");
            }

        });
        $("#cancel_add").click(function (){
            $("#addStudentId").val("");
            $("#addStudentName").val("");
            $("#addCollege").val("");
            $("#addMajor").val("");
            $("#addClassName").val("");
            $("#msg").text("");
            $("#add_cancel").show().delay(1500).fadeOut();
        })
    });
    /*添加结束*/

</script>
</html>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

[猫玖]

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

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

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

打赏作者

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

抵扣说明:

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

余额充值