2024/11/18日工作总结

完成java课堂测试:前端页面实现数据库的增功能:
mapper:

点击查看代码
package com.vivy.mapper;
import com.vivy.pojo.Classes;

import java.util.List;

public interface ClassesMapper {

    void add(Classes classes);
    void delete(String s);
    List<Classes> selectAll();

}


pojo:
点击查看代码
package com.vivy.pojo;

public class Classes {
    int id;
    String classId;
    String className;
    int number;
    String teacherName;
    String position;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClassId() {
        return classId;
    }

    public void setClassId(String classId) {
        this.classId = classId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    @Override
    public String toString() {
        return "Class{" +
                "id=" + id +
                ", classId='" + classId + '\'' +
                ", className='" + className + '\'' +
                ", number=" + number +
                ", teacherName='" + teacherName + '\'' +
                ", position='" + position + '\'' +
                '}';
    }
}

util:

点击查看代码
package com.vivy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        //静态代码块会随着类的加载自动执行,且只执行一次

        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}

web:
点击查看代码
package com.vivy.web;

import com.vivy.mapper.ClassesMapper;
import com.vivy.pojo.Classes;
import com.vivy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/addCourseServlet")
public class addCourseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String classId = request.getParameter("classId");
        String className = request.getParameter("className");
        String number = request.getParameter("number");
        String teacherName = request.getParameter("teacherName");
        String position = request.getParameter("position");

        //# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);

        Classes classes = new Classes();
        classes.setClassId(classId);
        classes.setClassName(className);
        classes.setNumber(Integer.parseInt(number));
        classes.setTeacherName(teacherName);
        classes.setPosition(position);

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);

        classesMapper.add(classes);
        sqlSession.commit();
        sqlSession.close();

    }

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

mapper:

点击查看代码
<?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.vivy.mapper.ClassesMapper">

    <select id="selectAll" resultType="classes">
        select * from tb_class;
    </select>

    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_class (class_id, class_name, number, teacher_name, position)
        values (#{classId}, #{className}, #{number}, #{teacherName}, #{position});
    </insert>

</mapper>
WEB-INF:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增课程</title>
</head>
<body>
<h2>新增课程</h2>
<form id="add-form" action="/course-demo/addCourseServlet" method="post">
    课程编号:<input name="classId" type="text" id="classId" ><br>
    课程名称:<input name="className" type="text" id="className" ><br>
    选课人数:<input name="number" type="number" id="number" ><br>
    任课教师:<input name="teacherName" type="text" id="teacherName" ><br>
    上课地点:<input name="position" type="text" id="position" ><br>

    <div class="buttons">
        <input value="添加课程" type="submit" id="add_btn">
    </div>
    <br class="clear">
</form>

</body>
</html>
posted @ 2024-11-18 23:11  张黎健  阅读(4)  评论(0编辑  收藏  举报