基于mybatis的学生成绩管理系统(部分功能)

本文档详细介绍了如何使用Mybatis框架来实现学生成绩管理系统的后端功能,包括学生查询、添加、删除和修改操作。涉及到的组件包括配置mybatis.xml和web.xml,创建util、model、filter、servlet、service、Dao和Mapper等包,以及对应的前端页面如login.html和list.html。在实践中,由于在StudentService中遗漏了设置grade属性导致前端报错,修复后系统恢复正常运行。
摘要由CSDN通过智能技术生成

用Mybatis实现学生成绩管理系统的学生查询、添加、删除、修改操作

后端代码

配置mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--配置文件-->
    <!--mybatis核心配置文件-->
    <!--导入属性文件-->
    <properties resource="config.properties"></properties>
    <!--全局设置-->
    <settings>
        <!--日志组件配置-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--从数据库命名   user_gender    自动映射到Java  userGende-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--开启mybatis二级缓存功能-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <!--配置类型别名-->
    <typeAliases>
        <typeAlias type="studentmybatisPro.model.User" alias="User"></typeAlias>
        <typeAlias type="studentmybatisPro.model.Grade" alias="Grade"></typeAlias>
        <typeAlias type="studentmybatisPro.model.Student" alias="Student"></typeAlias>
    </typeAliases>
    <!--配置与数据库的连接信息-->
    <environments default="development">
        <environment id="development">
            <!--配置数据事物管理方式-->
            <transactionManager type="JDBC"/>
            <!--数据源配置 type="POOLED" 使用数据库连接池功能-->
            <dataSource type="POOLED">
                <!--数据库连接-->
                <property name="driver" value="${driverClassName}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--sql映射-->
    <mappers>
        <mapper resource="mapper/LoginMapper.xml"></mapper>
        <mapper resource="mapper/StudentMapper.xml"></mapper>
    </mappers>

</configuration>

配置web.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">

    <!--登录配置-->
    <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>studentmybatisPro.servlet.loginServlet_back</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/loginServlet</url-pattern><!--定义url-->
    </servlet-mapping>

   <!-- 配置学生管理servlet-->
    <servlet>
        <servlet-name>StudentServlet</servlet-name>
        <servlet-class>studentmybatisPro.servlet.StudentServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>StudentServlet</servlet-name>
        <url-pattern>/back/StudentServlet</url-pattern>
    </servlet-mapping>

    <!--退出-->
    <servlet>
        <servlet-name>logOutServlet</servlet-name>
        <servlet-class>studentmybatisPro.servlet.logOutServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>logOutServlet</servlet-name>
        <url-pattern>/back/logOutServlet</url-pattern><!--定义url-->
    </servlet-mapping>

    <!--配置过滤器-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>studentmybatisPro.filter.EncodingFilter</filter-class>
        <!--为过滤器配置参数-->
        <init-param>
            <param-name>charset</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <!--配置哪些请求可以进入到此过滤器-->
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern><!--表示所有请求都进入到过滤器-->
    </filter-mapping>

    <!--跨域过滤器-->
    <filter>
        <filter-name>corsFilter</filter-name>
        <filter-class>studentmybatisPro.filter.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--后端验证用户是否登录-->
    <filter>
        <filter-name>isLogin</filter-name>
        <filter-class>studentmybatisPro.filter.IsLoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>isLogin</filter-name>
        <url-pattern>/back/*</url-pattern>
    </filter-mapping>
</web-app>

util

把重复使用的mybatis代码封装起来,创建util包,建立mybatisUtil

package studentmybatisPro.util;

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

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

public class mybatisUtil {
   
    /*加载类时,静态代码块只执行一次*/
    static  SqlSessionFactory sqlSessionFactory;
    //在mybatisUtil第一次加载时,初始化好SqlSessionFactory
    static
    {
   
        String resource = "mybatis.xml";
        //读取mybatis配置文件
        InputStream inputStream = null;
        try {
   
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        /*
        构造出一个SqlSessionFactory对象,用来创建SqlSession对象,
        在一个项目中由于SqlSessionFactory创建开销较大,一个项目中只创建一个,
        */
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    public static SqlSession getSqlSession()
    {
   
        return sqlSessionFactory.openSession();
    }
}

model

Grade
package studentmybatisPro.model;

import java.io.Serializable;

public class Grade implements Serializable{
   
    private Integer id;
    private String name;

    public Integer getId() {
   
        return id;
    }

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

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }
}
Student
package studentmybatisPro.model;

import java.io.Serializable;
import java.util.Date;

public class Student  implements Serializable {
   
    private Integer id;//null 如果是基本类型就是0
    private Integer no;
    private String name;
    private String gender;
    private Grade grade ;//类与类的关联关系  has-a关系
    private String phone;
    private String address;
    private Date regTime;
    private User user;


    public Integer getId() {
   
        return id;
    }

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


    public Integer getNo() {
   
        return no;
    }

    public void setNo(Integer no) {
   
        this.no = no;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public String getGender() {
   
        return gender;
    }

    public void setGender(String gender) {
   
        this.gender = gender;
    }

    public Grade getGrade() {
   
        return grade;
    }

    public void setGrade(Grade grade) {
   
        this.grade = grade;
    }

    public String getPhone() {
   
        return phone;
    }

    public void setPhone(String phone) {
   
        this.phone = phone;
    }

    public String getAddress() {
   
        return address;
    }

    public void setAddress(String address) {
   
        this.address = address;
    }

    public Date getRegTime() {
   
        return regTime;
    }

    public void setRegTime(Date regTime) {
   
        this.regTime = regTime;
    }

    public User getUser() {
   
        return user;
    }

    public void setUser(User user) {
   
        this.user = user;
    }

    @Override
    public String toString() {
   
        return "Student{" +
                "id=" + id +
                ", no=" + no +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", grade=" + grade +
                ", phone='" + phone + '\'' +
                ", address='" + address + 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值