学生选课系统--教师功能的实现

学生选课系统--教师功能的实现

小总结:

完成的管理员和学生的所有功能之后,这个教师功能也很快就能完成了。

教师总功能选择页面

1.tea.jsp(总功能页面)

<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.xxxx.util.GetSqlSession" %>
<%@ page import="com.xxxx.mapper.TeaMapper" %>
<%@ page import="com.xxxx.entity.Tea" %><%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/11/13
  Time: 0:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>tea.jsp</title>
</head>
<%
    String uname=(String)request.getSession().getAttribute("uname");
    SqlSession sqlSession= GetSqlSession.CreateSqlSession();
    TeaMapper teaMapper=sqlSession.getMapper(TeaMapper.class);
    Tea tea=teaMapper.selectByname(uname);
    request.getSession().setAttribute("tea",tea);
    request.getSession().setAttribute("uname",uname);//修改密码需要传递的
    sqlSession.close();
%>
<body>
欢迎${uname}登录<br>
<a href="teaselect.jsp">查看教师个人信息</a><br>
<a href="uptea.jsp?id=<%=tea.getId()%>">修改个人信息</a><br>
<a href="stupwd.jsp">修改个人密码</a><br>
<a href="teacla.jsp">修改个人所授课</a><br>
<a href="teacla.jsp">查询个人课表</a><br>
</body>
</html>
View Code点击查看代码

查看个人信息

2.teaselect.jsp(教师查看个人信息页面)

<%@ page import="com.xxxx.entity.Tea" %><%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/11/17
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>teaselect.jsp</title>
</head>
<%--显示教师个人信息--%>
<body>
<%
    Tea tea=(Tea)request.getSession().getAttribute("tea");
%>
<table align="center" border="1" width="800">
    <tr>
        <td>工号</td><td>姓名</td><td>职位</td><td>所属院</td>
    </tr>
    <tr>
        <td><%=tea.getId()%></td><td><%=tea.getName()%></td><td><%=tea.getPro()%></td><td><%=tea.getYuan()%></td></tr>
</table>
</body>
</html>
View Code点击查看代码

修改个人信息

3.uptea.jsp(教师修改个人信息页面)

<%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/11/13
  Time: 1:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>uptea.jsp</title>
</head>
<body>
<%
    String eid=request.getParameter("id");//获取超链接传过来的id
    request.getSession().setAttribute("eid",eid);//设置域对象
%>
<h2 align="center">修改教师信息</h2>
<table align="center" width="800" border="1">
    <form method="post" action="upteaServlet" name="upForm" id="upForm">
        <tr>
            <td>工号</td>
            <td><input type="text" name="id" id="id"><span id="msg1" style="font-size: 16px;color: red"></span> </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="name" id="name"></td>
        </tr>
        <tr>
            <td>职称</td>
            <td>
                <input type="radio" name="pro" value="助教">助教<br>
                <input type="radio" name="pro" value="讲师">讲师<br>
                <input type="radio" name="pro" value="副教授">副教授<br>
                <input type="radio" name="pro" value="教授">教授<br>
            </td>
        </tr>
        <tr>
            <td>所属学院</td>
            <td>
                <select name="yuan" id="yuan">
                    <option value="信息" >信息</option>
                    <option value="土木" >土木</option>
                    <option value="机械" >机械</option>
                    <option value="电气" >电气</option>
                    <option value="交通" >交通</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2"><span style="font-size: 16px;color:red;" id="msg2"></span> </td>
        </tr>
        <tr align="center">
            <td colspan="2"><button type="button" id="upBtn">修改</button></td>
        </tr>
    </form>
</table>
</body>
<script type="text/javascript" src="js/jquery-3.6.1.js"></script>
<script type="text/javascript">
    $("#upBtn").click(function(){
        var id=$("#id").val();
        var yuan=$("#yuan").val();
        //先验证工号格式,6位数字,前两位01-5
        if(isNaN(id)|| id.length!=6){
            $("#msg1").html("工号格式错误1");return;//不要忘记return
        }
        else{
            if(id[0]!='0'||id[1]>5||id[1]<1){//***
                //工号错误
                $("#msg1").html("工号格式错误2");return;
            }
            else {
                //工号正确了-->判断工号和院系是否对应
                if((id[1]==1&&yuan!="信息")||(id[1]==2&&yuan!="土木")||(id[1]==3&&yuan!="机械")||(id[1]==4&&yuan!="电气")||(id[1]==5&&yuan!="交通")){
                    //工号和院系不匹配
                    $("#msg2").html("工号和院系不匹配");return;
                }
                else{
                    //所有条件都满足了
                    $("#upForm").submit();
                }
            }
        }
    });
</script>
</html>
View Code点击查看代码

4.upteaServlet(修改 个人信息Servlet)

package com.xxxx.servlet;

import com.xxxx.entity.Stu;
import com.xxxx.entity.Tea;
import com.xxxx.mapper.StuMapper;
import com.xxxx.mapper.TeaMapper;
import com.xxxx.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

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("/upteaServlet")
public class upteaServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        int eid= Integer.valueOf((String) request.getSession().getAttribute("eid"));//获取要修改的id写int会出错**接收先前的id
         int id      = Integer.parseInt(request.getParameter("id"));//工号
         String name =request.getParameter("name");//姓名
         String pro  =request.getParameter("pro");//职位
         String yuan =request.getParameter("yuan");//所属院
        Tea tea=new Tea(id,name,pro,yuan);
        SqlSession sqlSession = GetSqlSession.CreateSqlSession();
        TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
        teaMapper.updateByeid(tea,eid);
        response.getWriter().write("修改成功");
        response.getWriter().close();
        sqlSession.close();
    }
}
View Code点击查看代码

修改个人密码(和学生修改个人密码同)

5.stupwd.jsp(修改密码页面)

<%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/11/16
  Time: 15:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>stupwd.jsp</title>
</head>
<%--修改个人密码--%>
<body>
<%
    String uname= (String) request.getSession().getAttribute("uname");
    request.getSession().setAttribute("uname",uname);
%>
<form action="stupwdServlet" method="post">
账户:<%=uname%><br>密码:<input type="password" name="upwd"><br>
    <span id="msg" style="color: red;font-size: 16px">${msg}</span>
    <button>修改</button>
</form>
</body>
</html>
View Code点击查看代码

6.stupwdServlet(修改密码Servlet)

package com.xxxx.servlet;

import com.xxxx.entity.Login;
import com.xxxx.mapper.LoginMapper;
import com.xxxx.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

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("/stupwdServlet")
public class stupwdServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String uname= (String) request.getSession().getAttribute("uname");
        String upwd=request.getParameter("upwd");
        SqlSession sqlSession= GetSqlSession.CreateSqlSession();
        LoginMapper loginMapper=sqlSession.getMapper(LoginMapper.class);
        Login login=loginMapper.selectByUname(uname);
        if(login.getUpwd().equals(upwd)){
            request.getSession().setAttribute("msg","密码不能和原来的密码一样");
            request.getRequestDispatcher("stupwd.jsp").forward(request,response);
        }
        else{
            loginMapper.updateByUname(uname,upwd);
            response.getWriter().write("修改密码成功");
            response.getWriter().close();
        }
        sqlSession.close();
    }
}
View Code点击查看代码

修改个人授课信息和显示个人授课课表

7.teacla.jsp(个人授课课程表)

<%@ page import="com.xxxx.entity.Tea" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.xxxx.util.GetSqlSession" %>
<%@ page import="com.xxxx.mapper.ClaMapper" %>
<%@ page import="com.xxxx.entity.Cla" %><%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/11/17
  Time: 16:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>teacla.jsp</title>
</head>
<%--教师查询到自己的课程信息,可以点击修改进行修改--%>
<body>
<table align="center" border="1" width="800">
    <tr>
        <td>课程编号</td><td>课程名称</td><td>开课时间</td><td>上课时间</td><td>开课院系</td>
        <td>任课老师</td><td>老师工号</td><td>限制人数</td><td>已选人数</td>
    </tr>
<%
    String uname= (String) request.getSession().getAttribute("uname");
    SqlSession sqlSession= GetSqlSession.CreateSqlSession();
    ClaMapper claMapper=sqlSession.getMapper(ClaMapper.class);
    Cla[] clas=claMapper.selectByteaname(uname);//查询到该老师的所有课程信息
    for (Cla cla:clas) {
        %>
<tr>
    <td><%=cla.getId()%></td><td><%=cla.getName()%></td><td><%=cla.getTime()%></td><td><%=cla.getCi()%></td>
    <td><%=cla.getYuan()%></td><td><%=cla.getTea()%></td><td><%=cla.getTeaid()%></td><td><%=cla.getPeople()%></td><td><%=cla.getXuan()%>&nbsp;<a href="teaclaServlet?id=<%=cla.getId()%>&xuan=<%=cla.getXuan()%>">修改</a></td>
</tr>
<%
    }
%>
</table>
</body>
</html>
View Code点击查看代码

8.teaclaServlet(修改个人授课已选人数Servlet)

package com.xxxx.servlet;

import com.xxxx.mapper.ClaMapper;
import com.xxxx.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

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("/teaclaServlet")
public class teaclaServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        int id= Integer.parseInt(request.getParameter("id"));//获取课程id
        int xuan= Integer.parseInt(request.getParameter("xuan"));//获取已选人数
        SqlSession sqlSession= GetSqlSession.CreateSqlSession();
        ClaMapper claMapper=sqlSession.getMapper(ClaMapper.class);
        claMapper.updateXuan(id,xuan-1);
        response.getWriter().write("修改课程已选人数成功");
        response.getWriter().close();
        sqlSession.close();
    }
}
View Code点击查看代码

Mapper

9.ClaMapper.java

package com.xxxx.mapper;

import com.xxxx.entity.Cla;
import org.apache.ibatis.annotations.Param;

public interface ClaMapper {
    void add(Cla cla);

    Cla[] selectAll();

    void update(Cla cla);

    Cla selectByTeaname(String teaname);

    void deleteById(int id);

    Cla selectByid(int claid);

    void updateXuan(@Param("id") int id,@Param("num") int num);

    Cla[] selectByteaname(String name);
}
View Code点击查看代码

10.ClaMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"-->
<!--         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
<!--         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"-->
<!--         version="4.0">-->
<!--</web-app>-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:命名空间-->
<mapper namespace = "com.xxxx.mapper.ClaMapper">

    <insert id="add">
        insert into cla (name,time,ci,yuan,tea,teaid,people)
        values (#{name},#{time},#{ci},#{yuan},#{tea},#{teaid},#{people});
    </insert>
    <update id="update">
        update cla set time=#{time},ci=#{ci},yuan=#{yuan},tea=#{tea},teaid=#{teaid},people=#{people}
                       where id=#{id};
    </update>
    <update id="updateXuan">
        update cla set xuan=#{num}
        where id=#{id};
    </update>

    <delete id="deleteById">
        delete from cla where id=#{id};
    </delete>
    <select id="selectAll" resultType="com.xxxx.entity.Cla">
        select * from cla;
    </select>
    <select id="selectByTeaname" resultType="com.xxxx.entity.Cla">
        select *
        from cla where tea=#{teaname};
    </select>
    <select id="selectByid" resultType="com.xxxx.entity.Cla">
        select *
        from cla where id=#{claid};
    </select><select id="selectByteaname" resultType="com.xxxx.entity.Cla">
    select * from cla where tea=#{name};
</select>
</mapper>
View Code点击查看代码

11.TeaMapper.java

package com.xxxx.mapper;

import com.xxxx.entity.Tea;
import org.apache.ibatis.annotations.Param;

public interface TeaMapper {
    void add(Tea tea);

    Tea[] selectAll();

    void updateByeid(@Param("tea") Tea tea, @Param("eid") int eid);

    Tea selectByid(int id);

    void deleteById(String id);

    Tea selectByname(String uname);
}
View Code点击查看代码

12.TeaMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"-->
<!--         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
<!--         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"-->
<!--         version="4.0">-->
<!--</web-app>-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:命名空间-->
<mapper namespace = "com.xxxx.mapper.TeaMapper">


    <insert id="add">
        insert into tea01 (id,name,pro,yuan)
        values (#{id},#{name},#{pro},#{yuan});
    </insert>
    <delete id="deleteById">
        delete
        from tea01
        where id=#{id};
    </delete>
    <select id="selectAll" resultType="com.xxxx.entity.Tea">
        select * from tea01;
    </select>
    <select id="selectByid" resultType="com.xxxx.entity.Tea">
        select * from tea01 where id=#{id};
    </select>
    <select id="selectByname" resultType="com.xxxx.entity.Tea">
        select * from tea01 where name=#{uname};
    </select>
    <update id="updateByeid">
        update tea01
        set id=#{tea.id},name=#{tea.name},pro=#{tea.pro},yuan=#{tea.yuan}
        where id=#{eid};
    </update>
</mapper>
View Code点击查看代码

 

 

 

posted on 2022-11-15 17:41  201812  阅读(72)  评论(0编辑  收藏  举报