教务管理系统---SSM框架

1.教务管理系统需求如下:

针对对象:

1.系统管理员

2.班主任

系统管理员功能需求:

1.管理员登录

2.管理员重置密码

3.退出登录

4.修改本人资料

5.教师管理【分页展示老师信息,添加老师信息,开除老师(不删除仅修改状态),禁止登录】

6.学生管理(分页展示学生信息,根据班级、学号、姓名模糊查询学生)

7.班级管理(分页展示班级信息开设班级,修改班级班主任)

班主任功能需求:
1.班主任登录

2.班主任修改密码

3.班主任修改个人资料

4.退出登录

5.学生管理【分页查询所带班级学生信息(根据姓名、学号、班级、状态模糊查询),添加学生,修改学生信息,开除学生(不删除仅修改状态)】

6.班级管理(分页展示所带班级,单击班级在下方分页显示班级学生信息,修改班级资料,上传班级资料)

7.活动管理(创建活动、修改活动、分页展示活动、单击活动显示活动详情)

8.班级资料下载(资料展示,资料下载)


2.系统架构设计:

根据功能的不同,可以划分为几个层次:

  • 持久对象层:该层由若干个持久化类(实体类)组成。
  • 数据访问层(Dao层):该层由若干个Dao接口和Mybatis映射文件组成。接口的名称统一以Dao结尾,且Mybatis映射文件名称要与接口的名称相同
  • 业务逻辑层(Service层):该层由若干个Service接口和实现类组成。在本系统中,业务逻辑层的接口统一以Service结尾,其实现类名称统一在接口后加Impl。该层主要用于实现类的业务处理逻辑
  • Web表现层:该层主要包括SpringMVC 中的Controller类和JSP页面。Controller类主要负责拦截用户请求,并调用业务逻辑层中相应组件的业务逻辑方法来处理用户请求,然后将相应的结果返回给JSP页面。


3.数据库设计:

教务管理系统对系统功能进行分析创建数据库educational_system并建立数据库表:

1.建立管理员表(admin):

代码实现:

CREATE TABLE `admin`(
    `aid` INT PRIMARY KEY AUTO_INCREMENT COMMENT '编号',
    `aname` VARCHAR(10) NOT NULL  COMMENT '管理员姓名',
    `photo` VARCHAR(50) DEFAULT 'default.jpg',
    `username` VARCHAR(11) UNIQUE COMMENT '登录账号',
    `password` VARCHAR(6) NOT NULL DEFAULT '666666',
    `remark` VARCHAR(50) COMMENT '其他'
)ENGINE = Innodb DEFAULT CHARSET = utf8;

2.建立教师表(teacher):

代码实现:

CREATE TABLE `teacher`(
    `tid` INT PRIMARY KEY AUTO_INCREMENT COMMENT '编号',
    `tname` VARCHAR(10) NOT NULL  COMMENT '教师姓名',
    `photo` VARCHAR(50) DEFAULT 'default.jpg',
    `username` VARCHAR(11) UNIQUE COMMENT '登录账号',
    `password` VARCHAR(6) NOT NULL DEFAULT '666666',
    `gender` INT DEFAULT 1 COMMENT '性别:1.男,0.女',
    `status` INT DEFAULT 1 COMMENT '状态:1.在职;2.离职;3.开除;4.休假',
    `islogin` INT DEFAULT 1 COMMENT '是否可登录:1.是;2.否',
    `phone` VARCHAR(11) NOT NULL COMMENT '手机号码',
    `address` VARCHAR(50) COMMENT '家庭住址',
    `remark` VARCHAR(50) COMMENT '其他'
)ENGINE = Innodb DEFAULT CHARSET = utf8;

3.建立班级表(tbclass):

代码实现:

CREATE TABLE `tbclass`(
    `cid` INT PRIMARY KEY AUTO_INCREMENT COMMENT '编号',
    `cname` VARCHAR(10) NOT NULL COMMENT '班级名称',
    `cdesc` VARCHAR(100) COMMENT '班级简介',
    `teacher` INT COMMENT '班主任编号',
    `stucount` INT DEFAULT 0 COMMENT '班级人数,不需要手动更新,在添加学生时自动更新',
    `remark` VARCHAR(50) COMMENT '其他',
    FOREIGN KEY (teacher) references teacher(tid)
)ENGINE = Innodb DEFAULT CHARSET=utf8;

4.建立学生表(student):

代码实现:

CREATE TABLE `student`(
    `stuid` VARCHAR(16) UNIQUE COMMENT '学生学号,学号格式为 s+年份+月份+日期+时分秒+随机数,例如s202203081234566',
    `sname` VARCHAR(10) NOT NULL COMMENT '学生姓名',
    `gender` INT NOT NULL COMMENT '性别:1.男;2.女',
    `phone` VARCHAR(11) NOT NULL COMMENT '手机号码',
    `address` VARCHAR(50) COMMENT '家庭住址',
    `clsid` INT COMMENT '所在班级id',
    `status` INT DEFAULT 1 COMMENT '状态:1.在籍;2.毕业;3.开除;4.休假',
    `remark` VARCHAR(50) COMMENT '其他',
    FOREIGN KEY (clsid) REFERENCES tbclass(cid)
)ENGINE = Innodb DEFAULT CHARSET=utf8;

5.建立活动表(tbactivity):

代码实现:

CREATE TABLE `tbactivity`(
    `id` INT PRIMARY KEY AUTO_INCREMENT COMMENT '活动id',
    `subject` VARCHAR(20) NOT NULL COMMENT '活动主题',
    `content` VARCHAR(500) COMMENT '活动内容',
    `clsid` INT COMMENT '活动班级id',
    `starttime` VARCHAR(10) NOT NULL COMMENT '活动时间:格式为yyyy-mm-dd',
    `remark` VARCHAR(50) COMMENT '其他',
    FOREIGN KEY (clsid) REFERENCES tbclass(cid)
)ENGINE = Innodb DEFAULT CHARSET=utf8;

6.建立资料表(tbresource):

代码实现:

CREATE TABLE `tbresource`(
    `id` INT PRIMARY KEY AUTO_INCREMENT COMMENT '资料id',
    `clsid` INT COMMENT '资料班级id',
    `filename` VARCHAR(15) NOT NULL COMMENT '资料名称',
    `url` VARCHAR(100) NOT NULL COMMENT '下载地址',
    `remark` VARCHAR(50) COMMENT '其他',
    FOREIGN KEY (clsid) REFERENCES tbclass(cid)
)ENGINE = Innodb DEFAULT CHARSET=utf8;

4.系统环境搭建(pom.xml依赖注入)

pom.xml依赖注入:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>Educational_System</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Educational_System</name>
    <description>Educational_System</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>11</java.version>
        <project.bulid.sourceEncoding>UTF-8</project.bulid.sourceEncoding>
        <spring.version>4.0.2.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <slf4j.version>1.7.12</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <jackson.version>2.9.3</jackson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--spring框架包start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring框架包end-->

        <!--Mybatis框架包start-->
        <!--Mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!--Mybatis框架包end-->

        <!--Mysql连接依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <!--导入dbcp的jar包,用来在applicationContext.xml中配置数据库-->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!--Mysql-end-->

        <!--分页插件依赖start-->
        <!-- pagehelper 分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--分页插件end-->

        <!--JSTL标签类-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--log start-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!--log end-->

        <!--JSON-->
        <!--格式化对象,方便输出日志-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!--JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!--JSON-->

        <!--上传组件包start-->
        <!--进行上传文件所需要的maven依赖-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!--解析excel文档-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!--上传组件包end-->

        <!--单元测试的依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.19.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--JavaEE包-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!--            <plugin>-->
            <!--                <groupId>org.springframework.boot</groupId>-->
            <!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
            <!--                <version>2.5.7</version>-->
            <!--            </plugin>-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>


5.准备项目环境:

在resources包下编写Ioc配置文件-applicationContext.xml 文件

Spring 提供了两种类型的 IOC 容器实现:
– BeanFactory : IOC 容器的基本实现 .
– ApplicationContext : 提供了更多的高级特性 . 是 BeanFactory 的子接口 .
– BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身; ApplicationContext 面向使用 Spring 框架的开发者, 几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
– 无论 使用何种方式 , 配置文件时相同的 

 ApplicationContext 的主要实现类:
– ClassPathXmlApplicationContext : 从 类路径下 加载配置文件
– FileSystemXmlApplicationContext : 从文件系统中加载 配置文件
• ConfigurableApplicationContext 扩展于 ApplicationContext ,新增加两个主要方法: refresh() 和 close() , 让 ApplicationContext 具有启动、刷新和关闭上下文的 能力。
• ApplicationContext 在初始化上下文时就实例化所有单例的 Bean 。
• WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作。


基于XML配置文件Bean:

• 配置 bean,下面这些是配置Bean的内容目录:
– 配置形式 :基于 XML 文件的 方式;基于 注解的 方式
– Bean 的配置 方式:通过全类名(反射)、通过工厂方法(静态工厂方法 & 实例工厂方法)、 FactoryBean
– IOC 容器 BeanFactory & ApplicationContext 概述
– 依赖注入的方式:属性注入;构造器注入
– 注入属性值细节
– 自动转配
– bean 之间的关系:继承;依赖
– bean 的作用域: singleton ; prototype ; WEB 环境作用域
– 使用外部属性文件
– spEL
– IOC 容器中 Bean 的生命周期
– Spring 4.x 新特性:泛 型依赖注入
 •在 xml 文件中通过 bean 节点来配置 bean


Ioc配置文件  applicationContext.xml  如下:
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ea"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/exam/dao/*-mapper.xml"></property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value></value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <bean id="msc" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="ssf"></property>
        <property name="basePackage" value="com.exam.dao"></property>
    </bean>
</beans>

在resources包下编写 Spring MVC 配置文件(mvc - servlet.xml):

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc = "http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop ="http://www.springframework.org/schema/aop"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
        <context:component-scan base-package="com.exam.controller"></context:component-scan>
    <!--配置视图解析器
        在返回视图的时候自动加前缀以及后缀
    -->
<!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>  前缀
        <property name="suffix" value=".jsp"></property> 后缀
    </bean>-->
    <mvc:annotation-driven/>
    <!--上传配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件最大限制-->
        <property name="maxUploadSize" value="31457280"></property>
        <!--上传编码-->
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
</beans>
       
       


       
       

在Educational_System 项目点击右击,选择 Add Framework support...添加 Web Application。

添加完web表现层后在webapp>WEB-INF 下的 web.xml中,配置Spring的监听器、编码过滤器和Spring MVC 的前端控制器等信息。

<?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">
    <display-name>Educational_System</display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    <!--配置Spring MVC 的中央控制器,并加载容器-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定MVC容器位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

将日志属性配置文件放入resources包下(log4j.properties):

log4j.rootLogger=DEBUG,stdout
#SqlMap logging configuration...
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG
log4j.logger.com.ibatis.common.util.StopWatch=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
#Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p[%t] - %m%n

6.功能实现模块

6.1、创建实体层(Bean)

1.根据数据表字段,在com.exam.bean包下创建实体类  ----> Admin 管理员类---对应数据表admin

代码实现如下:

package com.exam.bean;

/*实体类Admin对应数据表admin*/
public class Admin {
    /*实体类的参数名与类型要与数据库中数据表的参数相对应*/
    private Integer aid;//编号
    private String aname;//管理员姓名
    private String photo;//管理员头像,默认default.jpg
    private String username;//登录账号用户名
    private String password;//密码,默认666666
    private String remark;//其他

    /*获取各参数的get与set方法*/

    public Integer getAid() {
        return aid;
    }

    public void setAid(Integer aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    //获取toString()方法

    @Override
    public String toString() {
        return "Admin{" +
                "aid=" + aid +
                ", aname='" + aname + '\'' +
                ", photo='" + photo + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}
2.根据数据表字段,在com.exam.bean包下创建实体类  ----> Student 学生类---对应数据表student

代码实现如下:

package com.exam.bean;

/*实体类Student对应数据表student*/
public class Student {
    private String stuid;//学生学号,学号格式为 s+年份+月份+日期+时分秒+随机数,例如s202407301234566
    private String sname; //学生姓名
    private Integer gender;//性别:1.男,2.女
    private String phone;  //手机号码
    private String address;//家庭住址
    private Integer clsid;//所在班级id
    private Integer status;//状态:1.在籍;2.毕业;3.开除;4.休假
    private String remark;//其他
    private TbClass tbclass;//班级类型-参数

    //获取参数set和get方法

    public String getStuid() {
        return stuid;
    }

    public void setStuid(String stuid) {
        this.stuid = stuid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getGender() {
        return gender;
    }

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

    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 Integer getClsid() {
        return clsid;
    }

    public void setClsid(Integer clsid) {
        this.clsid = clsid;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public TbClass getTbclass() {
        return tbclass;
    }

    public void setTbclass(TbClass tbclass) {
        this.tbclass = tbclass;
    }
    //获取字符串toString()方法--->返回类名和引用地址,返回字符串的值

    @Override
    public String toString() {
        return "Student{" +
                "stuid='" + stuid + '\'' +
                ", sname='" + sname + '\'' +
                ", gender=" + gender +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", clsid=" + clsid +
                ", status=" + status +
                ", remark='" + remark + '\'' +
                ", tbclass=" + tbclass +
                '}';
    }
}
3.根据数据表字段,在com.exam.bean包下创建实体类  ----> TbActivity 活动类---对应数据表tbactivity

代码实现如下:

package com.exam.bean;

/*实体类 Tbactivity活动类 对应数据库表 tbactivity*/
public class TbActivity {
    //定义参数
    private Integer id;//活动id
    private String subject;//活动主题
    private String content;//活动内容
    private Integer clsid;//活动班级id
    private String starttime;//活动时间:格式为yyyy-mm-dd
    private String remark;//其他
    private TbClass tbclass;//班级类型-参数

    //无参构造器
    public TbActivity() {
        super();
    }

    //有参构造器
    public TbActivity(Integer id, String subject, String content, Integer clsid, String starttime, String remark, TbClass tbclass) {
        super();
        this.id = id;
        this.subject = subject;
        this.content = content;
        this.clsid = clsid;
        this.starttime = starttime;
        this.remark = remark;
        this.tbclass = tbclass;
    }

    //获取参数set和get方法
    public Integer getId() {
        return id;
    }

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

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getClsid() {
        return clsid;
    }

    public void setClsid(Integer clsid) {
        this.clsid = clsid;
    }

    public String getStarttime() {
        return starttime;
    }

    public void setStarttime(String starttime) {
        this.starttime = starttime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public TbClass getTbclass() {
        return tbclass;
    }

    public void setTbclass(TbClass tbclass) {
        this.tbclass = tbclass;
    }
    //返回字符串值方法-toString();

    @Override
    public String toString() {
        return "Tbactivity{" +
                "id=" + id +
                ", subject='" + subject + '\'' +
                ", content='" + content + '\'' +
                ", clsid=" + clsid +
                ", starttime='" + starttime + '\'' +
                ", remark='" + remark + '\'' +
                ", tbclass=" + tbclass +
                '}';
    }
}
4.根据数据表字段,在com.exam.bean包下创建实体类  ----> TbClass 班级类---对应数据表tbclass

代码实现如下:

package com.exam.bean;

import java.util.List;

/*实体类--班级Tbclass与数据库表tbclass相对应*/
public class TbClass {
    //定义参数
    private Integer cid;//编号
    private String cname;//班级名称
    private String cdesc;//班级简介
    private Integer teacher;//班主任编号
    private Integer stucount;//班级人数,不需要手动更新,在使用添加学生功能时自动更新
    private String remark;//其他
    private Teacher tr;//老师类型参数--->班级
    private List<Student> stu;//学生集合,班级里的学生

    //获取参数set和get方法
    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCdesc() {
        return cdesc;
    }

    public void setCdesc(String cdesc) {
        this.cdesc = cdesc;
    }

    public Integer getTeacher() {
        return teacher;
    }

    public void setTeacher(Integer teacher) {
        this.teacher = teacher;
    }

    public Integer getStucount() {
        return stucount;
    }

    public void setStucount(Integer stucount) {
        this.stucount = stucount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Teacher getTr() {
        return tr;
    }

    public void setTr(Teacher tr) {
        this.tr = tr;
    }

    public List<Student> getStu() {
        return stu;
    }

    public void setStu(List<Student> stu) {
        this.stu = stu;
    }

    //获取参数字符串返回值方法--toString();

    @Override
    public String toString() {
        return "Tbclass{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", cdesc='" + cdesc + '\'' +
                ", teacher=" + teacher +
                ", stucount=" + stucount +
                ", remark='" + remark + '\'' +
                ", tr=" + tr +
                ", stu=" + stu +
                '}';
    }
}
5.根据数据表字段,在com.exam.bean包下创建实体类  ----> TbResource 资料类---对应数据表tbresource

代码实现如下:

package com.exam.bean;

/*实体类-TbResource资源类,对应数据库表tbresource
* 该类的对应的数据表与tbclass表关联*/
public class TbResource {
    private Integer id;//资料id
    private Integer clsid;//资料班级id
    private String filename;//资料名称
    private String url;//下载地址
    private String remark;//其他
    private TbClass tbclass; //该类与班级类有关联

    //获取参数的set和get方法
    public Integer getId() {
        return id;
    }

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

    public Integer getClsid() {
        return clsid;
    }

    public void setClsid(Integer clsid) {
        this.clsid = clsid;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public TbClass getTbclass() {
        return tbclass;
    }

    public void setTbclass(TbClass tbclass) {
        this.tbclass = tbclass;
    }

    //返回字符串值的toString()方法

    @Override
    public String toString() {
        return "TbResource{" +
                "id=" + id +
                ", clsid=" + clsid +
                ", filename='" + filename + '\'' +
                ", url='" + url + '\'' +
                ", remark='" + remark + '\'' +
                ", tbclass=" + tbclass +
                '}';
    }
}
6.根据数据表字段,在com.exam.bean包下创建实体类  ----> Teacher 教师类---对应数据表teacher

代码实现如下:

package com.exam.bean;

/*实体类Teacher对应数据库表teacher*/
public class Teacher {
    //定义实体类参数,参数类型和名字须与数据库一致,便于扫描读取
    private Integer tid;//编号
    private String tname;//教师姓名
    private String photo;//获取头像,默认default.jpg
    private String username;//登录账号
    private String password;//密码,默认666666
    private Integer gender;//性别:1.男;2.女
    private Integer status;//状态:1.在职;2.离职;3.开除;4.休假
    private Integer islogin;//是否可登录:1.是;2.否
    private String phone;//手机号码
    private String address;//家庭住址
    private String remark;//其他

    //无参构造器
    public Teacher() {
        super();
    }

    //有参构造器1
    public Teacher(Integer tid, String tname, String photo, String username, String password, Integer gender, Integer status, Integer islogin, String phone, String address, String remark) {
        super();
        this.tid = tid;
        this.tname = tname;
        this.photo = photo;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.status = status;
        this.islogin = islogin;
        this.phone = phone;
        this.address = address;
        this.remark = remark;
    }
    //有参构造器2
    public Teacher(String tname, String photo, String username, String password, Integer gender, Integer status, Integer islogin, String phone, String address, String remark) {
        super();
        this.tname = tname;
        this.photo = photo;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.status = status;
        this.islogin = islogin;
        this.phone = phone;
        this.address = address;
        this.remark = remark;
    }

    //获取set和get方法
    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getGender() {
        return gender;
    }

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

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getIslogin() {
        return islogin;
    }

    public void setIslogin(Integer islogin) {
        this.islogin = islogin;
    }

    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 String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    //获取返回字符串的值的toString方法

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                ", photo='" + photo + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender=" + gender +
                ", status=" + status +
                ", islogin=" + islogin +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}

6.2、创建工具类(Util)

在com.exam.util下创建生成 ID 的工具类

代码实现如下:

package com.exam.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/* ID 的 工具类*/
public class IDUtil {
    /*
    * 学生学号格式为:s+年份+月份+日期+时分秒+随机数,例如s202407301234566
    * @return
    * */
    public static final String getIDUtil(){
        String id = "s";
        int num = (int)(Math.random()*10);
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//yyyy:年 , MM:月, dd:日 , hh:1~12小时制(1-12) ,HH:24小时制(0-23) , mm:分 , ss:秒
        Date date = new Date();
        id += dateFormat.format(date)+num;
        return id;
    }

    /*获取当前时间
    * @return
    * */
    public static final String getTimeUtil(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dateFormat.format(date);
    }

}

6.3、创建数据访问层 (Dao)

根据功能需求,在com.exam.dao包下创建数据访问接口以及在resources包下创建mapper包,mapper包下存放数据访问接口所对应的映射文件。

1.管理员数据访问层(IAdminDao)以及对应映射文件(IAdminDao-mapper.xml):

管理员数据访问层(IAdminDao)代码如下:

package com.exam.dao;

import com.exam.bean.Admin;

/*
数据访问层接口,管理员层面完成功能
* */
public interface IAdminDao {
    /*-------------------------1、管理员登录功能---------------------------------------
    * @param admin
    * */
    public Admin adminLogin(Admin admin)throws Exception;

    /*-------------------------2、管理员密码重置(设置为默认666666)---------------------------------------
    * @param aid
    * */
    public Integer adminResertPS(Integer aid) throws Exception;

    /*-------------------------3、管理员修改个人资料---------------------------------------
    * @param admin
    *   @return
    *       @throws Exception
    * */
    public Integer adminUpdateByThis(Admin admin) throws Exception;

    /*-------------------------4、获得管理员个人信息---------------------------------------
    * @param aid
    * */
    public Admin queryAdminById(Integer aid) throws Exception;

    /*----------------------------- END -->  wait_add  ---------------------------------*/
}

管理员数据访问层对应映射文件(IAdminDao-mapper.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.exam.dao.IAdminDao">
<sql id="adminMsgAll">aid,aname,photo,username,password,remark</sql>
    <!--parameterType: 接口中方法参数的类型, 类型的完全限定名或别名-->
    <!--指定 resultType 返回值类型时 Admin实体类类型的,
        对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 'hashmap'
        基本数据类型考虑到重复的问题,会在其前面加上 '_',比如 byte 对应的别名是 '_byte' -->

                                <!--管理员数据访问层映射文件-->
    <!--1、管理员登录功能-->
    <select id="adminLogin" parameterType="com.exam.bean.Admin" resultType="com.exam.bean.Admin">
        select * from admin where username=#{username} and password=#{password}
    </select>

    <!--2.管理员密码重置功能-->
    <update id="adminResertPS" parameterType="int" >
        update admin set password=666666 where aid=#{aid}
    </update>

    <!--3.管理员修改个人资料-->
    <update id="adminUpdateByThis" parameterType="com.exam.bean.Admin">
        update admin set aname=#{aname},photo=#{photo},username=#{username},
                         password=#{password},remark=#{remark} where aid=#{aid}
    </update>

    <!--4.获取管理员个人信息-->
    <select id="queryAdminById" parameterType="com.exam.bean.Admin">
        select * from admin where aid=#{aid}
    </select>
</mapper>
2.学生数据访问层(IStudentDao)以及对应映射文件(IStudentDao-mapper.xml):

学生数据访问层(IStudentDao)实现代码如下:

package com.exam.dao;

import com.exam.bean.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/*
* 数据访问层接口,学生层功能
* */
public interface IStudentDao {

    /*-------------------------1、分页展示学生信息---------------------------------------
    * */
    public List<Student> queryAllStudentMessage() throws Exception;

    /*-------------------------2、根据班级学号姓名模糊查询学生信息--------------------------------------
    * @param clsid  班级id
    * @param stuid  学号
    * @param sname  学生姓名
    * */
    public List<Student> dimQueryStudentByClassAndSidAndName(@Param("clsid") String clsid,
                                                             @Param("stuid") Integer stuid,
                                                             @Param("sname") String sname) throws Exception;



    //--------------------------------------------------------------------------------------
    //----------------------------------班主任操作-------------------------------------------
    /*-----------------------------------1.添加学生--------------------------------------------
    * @param student
    * */
    public Integer insertStudent(Student student)throws Exception;
    /*-----------------------------------2.修改学生信息------------------------------------------
    @param student
    * */
    public Integer updateStudentMessage(Student student) throws Exception;
    /*--------------------------3.开除学生(不删除仅修改状态)/恢复开除的学生------------------------
     * @param status
     * @param stuid
     */
    public Integer expelStudent(@Param("status") Integer status,@Param("stuid") String stuid) throws Exception;

    /*----------------------------------------班主任操作学生End-------------------------------------------------*/


    //--------------------------------------------------------------------------------------
    //---------------管理员操作管理学生(分页展示学生信息,根据班级学号姓名模糊查询学生)---------------
    //----------------------联合查询------------------
    /*-----------------------------------1.联合查询分页展示学生信息--------------------------------------------*/
    public  List<Student> queryAllClassAndStudent(Map<String,Object> map)throws Exception;

    /*-----------------------------------2.通过id查询学生信息--------------------------------------------
    * @param stuid
    * */
    public Student queryStudentById(String stuid) throws Exception;

    /*-----------------------------------3.两表联合查询学生信息通过班级id-----------------------------------------
    *@param clsid
    * */
    public List<Student> queryByStudentByClsid(Integer clsid)throws Exception;

    /*----------------------------------------管理层管理学生End-------------------------------------------------*/

}

学生数据访问层(IStudentDao)对应映射文件(IStudentDao-mapper.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.exam.dao.IStudentDao">

    <!--1、分页展示学生信息-->
    <select id="queryAllStudentMessage" resultType="com.exam.bean.Student">
        select * from student
    </select>

    <!--2、根据班级学号姓名模糊查询学生信息-->
    <select id="dimQueryStudentByClassAndSidAndName" parameterType="map" resultType="com.exam.bean.Student">
        select * from student
        <where>
            <if test="clsid!=null">
                clsid like <![CDATA["%]]>${clsid}<![CDATA[%"]]>
            </if>
            <if test="stuid!=null">
                <trim prefix="and">
                    stuid like <![CDATA["%]]>${stuid}<![CDATA[%"]]>
                </trim>
            </if>
            <if test="sname!=null">
                <trim prefix="and">
                    sname like <![CDATA["%]]>${sname}<![CDATA[%"]]>
                </trim>
            </if>
        </where>
    </select>

    <!--班主任操作:1.添加学生-->
    <insert id="insertStudent" parameterType="com.exam.bean.Student">
        insert into student(stuid,sname,gender,phone,address,clsid,status,remark)
        values(#{stuid},#{sname},#{gender},#{phone},#{address},#{clsid},#{status},#{remark})
    </insert>
    <!--班主任操作:2.修改学生信息-->
    <update id="updateStudentMessage" parameterType="com.exam.bean.Student">
        update student set sname=#{sname},gender=#{gender},phone=#{phone},address=#{address},remark=#{remark},clsid=#{clsid},status=#{status}
        where stuid=#{stuid}
    </update>

    <!--班主任操作:3.开除学生(不删除仅修改状态)/恢复开除的学生-->
    <update id="expelStudent" parameterType="map">
        update student set status=#{status} where stuid=#{stuid}
    </update>

    <!--管理员管理学生:1.联合查询分页展示学生信息-->
    <resultMap type="com.exam.bean.Student" id="stu">
        <result column="stuid" property="stuid"/>
        <result column="sname" property="sname"/>
        <result column="gender" property="gender"/>
        <result column="phone" property="phone"/>
        <result column="address" property="address"/>
        <result column="clsid" property="clsid"/>
        <result column="status" property="status"/>
        <result column="remark" property="remark"/>
        <!--1对1使用association标签
        property :表示需要查询出来的属性
        select:指向一个查询方法用于查询级联属性
        column:使用指向一个级联字段,表示该字段当成参数传给select指向的方法,查询出一个对象,赋值给property
        -->
        <association property="tbclass" select="com.exam.dao.ITbClassDao.queryById" column="clsid"></association>
    </resultMap>
    <select id="queryAllClassAndStudent" parameterType="map" resultMap="stu">
        select * from student
        <where>
            <if test="clsid!=null">
                clsid like <![CDATA["%]]>${clsid}<![CDATA[%"]]>
            </if>
            <if test="stuid!=null">
                <trim prefix="and">
                    stuid like <![CDATA["%]]>${stuid}<![CDATA[%"]]>
                </trim>
            </if>
            <if test="sname!=null">
                <trim prefix="and">
                    sname like <![CDATA["%]]>${sname}<![CDATA[%"]]>
                </trim>
            </if>
        </where>
    </select>

    <!--管理员管理操作学生:2.通过学生id查询学生信息-->
    <select id="queryStudentById" resultType="com.exam.bean.Student">
        select * from student where stuid=#{stuid}
    </select>

    <!--管理员管理操作学生:3.两表联合查询学生信息通过班级id-->
    <resultMap id="byStuByClsid" type="com.exam.bean.Student">
        <association property="tbclass" select="com.exam.dao.ITbClassDao.queryById" column="clsid"></association>
    </resultMap>
    <select id="queryByStudentByClsid" resultMap="byStuByClsid">
        select * from student where clsid=#{clsid}
    </select>


</mapper>
3.活动数据访问层(ITbActivityDao)以及对应映射文件(ITbActivityDao-mapper.xml):

活动数据访问层(ITbActivityDao)实现代码如下:

package com.exam.dao;

import com.exam.bean.TbActivity;

import java.util.List;

/*活动数据访问层 接口
* 数据访问层接口活动管理*/
public interface ITbActivityDao {
        /*---------------------------------------------------------------*/
        /*--------------------------班主任操作---------------------------*/
        /*--------------------------1.创建活动---------------------------
        *   @param activity
        * */
    public Integer createActivity(TbActivity activity) throws Exception;

        /*--------------------------2.修改活动---------------------------
        * @param activity
        * */
    public Integer updateActivity(TbActivity activity) throws Exception;

        /*--------------------------3.分页展示活动---------------------------*/
    public List<TbActivity> queryAllActivity() throws Exception;

        /*--------------------------4.单击活动显示活动详情---------------------------
        * @param id
        * */
    public TbActivity queryActivityMessage(Integer id) throws Exception;



}

活动数据访问层(ITbActivityDao)对应映射文件(ITbActivityDao-mapper.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.exam.dao.ITbActivityDao">

    <!--班主任操作-->
    <!--1.创建活动 @param activity-->
    <insert id="createActivity" parameterType="com.exam.bean.TbActivity">
        insert into tbactivity(subject,content,clsid,starttime,remark)
        values(#{subject},#{content},#{clsid},#{starttime},#{remark})
    </insert>

    <!--2.修改活动 @param activity-->
    <update id="updateActivity" parameterType="com.exam.bean.TbActivity">
        update tbactivity set subject=#{subject},content=#{content},clsid=#{clsid},remark=#{remark} where id=#{id}
    </update>

    <!--3.分页展示活动-->
    <resultMap id="Act" type="com.exam.bean.TbActivity">
        <association property="tbclass" select="com.exam.dao.ITbClassDao.queryById" column="clsid"></association>
    </resultMap>
    <select id="queryAllActivity" resultMap="Act">
        select * from tbactivity
    </select>

    <!--4.单击活动显示活动详情 @param id-->
    <resultMap id="dataTbAct" type="com.exam.bean.TbActivity">
        <association property="tbclass" select="com.exam.dao.ITbClassDao.queryById" column="clsid"></association>
    </resultMap>
    <select id="queryActivityMessage" resultMap="dataTbAct">
        select * from tbactivity where id=#{id}
    </select>

</mapper>
4.班级数据访问层(ITbClassDao)以及对应映射文件(ITbClassDao-mapper.xml):

班级数据访问层(ITbClassDao)实现代码如下:

package com.exam.dao;

import com.exam.bean.TbClass;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/*
* 数据访问层班级管理接口
* @author Administrator
* */
public interface ITbClassDao {
    //-----------------------------------------------------------------
     //-----------------------管理员操作--------------------------------
    /*  1.分页展示班级信息  */
    public List<TbClass> dimQueryTbClassMessage() throws Exception;

    /*  2.开设班级
    * @param tbclass
    */
    public Integer insertTbClass(TbClass tbclass) throws Exception;

    /*  3.修改班级班主任
    * @param teacher   @param cid
    * */
    public Integer updateTbClass(@Param("teacher")Integer teacher,@Param("cid")Integer cid) throws Exception;

    /*  4.通过班级名称查询班级  */
    public TbClass queryByClassName() throws Exception;

    /*--------------------------班主任操作--------------------------------*/
    /*  1.分页展示班级信息   */
    public List<TbClass> queryAllTbClass() throws Exception;

    /*  2.单击班级在下方分页显示班级学生信息
    *   @param cid
    * */
    public List<TbClass> queryTbClassStudentMessage(Integer cid) throws Exception;

    /*  3.通过学生关联 id 查询班级
    *  @param cid
    * */
    public TbClass queryById(Integer cid) throws Exception;

    /*  4.查询班级人数
    * @param cid
    * */
    public Integer queryStuCountById(Integer cid) throws Exception;

    /*  5.修改班级人数
    * @param cid
    * @param count
    * */
    public Integer updateClassPersonNumber(@Param("cid") Integer cid,@Param("count") Integer count) throws Exception;

    /*  6.修改班级资料
    * @param tbclass
    * */
    public Integer updateClassMessage(TbClass tbclass) throws Exception;

    /*  7.修改班级信息,通过班级 id 查询
    * @param cid
    * */
    public TbClass queryClassById(Integer cid) throws Exception;

}

班级数据访问层(ITbClassDao)对应映射文件(ITbClassDao-mapper.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.exam.dao.ITbClassDao">
    <!--管理员操作-->
    <!--1.分页展示班级信息-->
<resultMap id="tc" type="com.exam.bean.TbClass">
    <result column="cid" property="cid"/>
    <result column="cname" property="cname"/>
    <result column="cdesc" property="cdesc"/>
    <result column="teacher" property="teacher"/>
    <result column="stucount" property="stucount"/>
    <result column="remark" property="remark"/>
    <association property="tr" select="com.exam.dao.ITeacherDao.queryById" column="teacher"></association>
</resultMap>
    <select id="dimQueryTbClassMessage" resultMap="tc">
        select * from tbclass
    </select>
    <!--2.开设班级 @param tbclass-->
    <insert id="insertTbClass" parameterType="com.exam.bean.TbClass">
        insert into tbclass(cname,cdesc,teacher,stucount,remark)
        values(#{cname},##{cdesc},#{teacher},#{stucount},#{remark})
    </insert>
    <!--3.修改班级班主任
     @param teacher   @param cid-->
    <update id="updateTbClass">
        update tbclass set teacher=#{teacher} where cid = #{cid}
    </update>
    <!--4.通过班级名称查询班级-->
    <select id="queryByClassName" parameterType="string" resultType="com.exam.bean.TbClass">
        select * from tbclass where cname=#{cname}
    </select>

    <!--班主任操作-->
    <!--1.分页展示班级信息-->
    <select id="queryAllTbClass" resultType="com.exam.bean.TbClass">
        select * from tbclass
    </select>
    <!--2.单击班级在下方分页显示班级学生信息-->
    <select id="queryTbClassStudentMessage" parameterType="int" resultType="com.exam.bean.TbClass">
        select s.* from tbclass t,student s where t.cid = s.clsidadncid=#{cid}
    </select>
    <!--3.通过学生关联 id 查询班级-->
    <resultMap id="tbData" type="com.exam.bean.TbClass">
        <association property="tr" select="com.exam.dao.ITeacherDao.queryById" column="teacher"></association>
    </resultMap>
    <select id="queryById" resultMap="tbData">
        select * from tbclass where cid = #{cid}
    </select>
    <!--4.查询班级人数-->
    <select id="queryStuCountById" resultType="int">
        select stucount from tbclass where cid=#{cid}
    </select>
    <!--5.修改班级人数-->
    <update id="updateClassPersonNumber">
        update tbclass set stucount = #{count} where cid=#{cid}
    </update>
    <!--6.修改班级资料-->
    <update id="updateClassMessage" parameterType="com.exam.bean.TbClass">
        update tbclass set cname = #{cname},cdesc = #{cdesc},teacher = #{teacher},stucount = #{stucount},remark = #{remark}
        where cid = #{cid}
    </update>
    <!--7.修改班级信息,通过班级 id 查询-->
    <select id="queryClassById" resultType="com.exam.bean.TbClass">
        select * from tbclass where cid = #{cid}
    </select>
</mapper>
5.资料数据访问层(ITbResourceDao)以及对应映射文件(ITbResourceDao-mapper.xml):

资料数据访问层(ITbResourceDao)实现代码如下:

package com.exam.dao;

import com.exam.bean.TbResource;

import java.util.List;

/*
* 数据访问层资料管理接口
* */
public interface ITbResourceDao {
    /*-------------------------班主任操作------------------------------*/
    /*    1.上传班级资料
    * @param resource   */
    public Integer uploadTbClassData(TbResource resource) throws Exception;
    /*    2.资料展示  */
    public List<TbResource> queryResource() throws Exception;
    /*    3.查询班级资料通过班级id
    *      @param clsid
    * */
    public List<TbResource> queryByResourceByClsid(Integer clsid) throws Exception;
    /*     4.修改班级资料
    *       @param resource
    * */
    public Integer updateClassData(TbResource resource) throws Exception;
    /*     5.查询资料通过
    *       @param id
    * */
    public TbResource queryResourceById(Integer id) throws Exception;

}

资料数据访问层(ITbResourceDao)对应映射文件(ITbResourceDao-mapper.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.exam.dao.ITbResourceDao">
<!--班主任操作-->
    <!--1.上传班级资料-->
    <insert id="uploadTbClassData" parameterType="com.exam.bean.TbResource">
        insert into tbresource(clsid,filename,url,remark)
        values(#{clsid},#{filename},#{url},#{remark})
    </insert>
    <!--2.资料展示-->
    <resultMap id="tbr" type="com.exam.bean.TbResource">
        <association property="tbclass" select="com.exam.dao.ITbClassDao.queryById" column="clsid"></association>
    </resultMap>
    <select id="queryResource" resultMap="tbr">
        select * from tbresource
    </select>
    <!--3.查询班级资料通过班级id-->
    <select id="queryByResourceByClsid" resultType="com.exam.bean.TbResource">
        select * from tbresource where clsid = #{clsid}
    </select>
    <!--4.修改班级资料-->
    <update id="updateClassData" parameterType="com.exam.bean.TbResource">
        update tbresource set filename = #{filename},url = #{url},remark = #{remark}
        where id = #{id}
    </update>
    <!--5.查询资料通过-->
    <select id="queryResourceById" resultType="com.exam.bean.TbResource">
        select * from tbresource where id = #{id}
    </select>
</mapper>
6.教师数据访问层(ITeacherDao)以及对应映射文件(ITeacherDao-mapper.xml):

教师数据访问层(ITeacherDao)实现代码如下:

package com.exam.dao;

import com.exam.bean.Teacher;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/*
* 数据访问层接口教师管理
* */
public interface ITeacherDao {
/*----------------------------------------------------------------*/
    /*       管理员操作           */
    /*     1.分页展示老师信息     */
    public List<Teacher> queryAllTeacherMessage() throws Exception;

    /*     2.添加老师信息
    *       @param teacher
    * */
    public Integer insertTeacher(Teacher teacher) throws Exception;

    /*     3.开除老师(不删除仅修改状态)/恢复开除老师状态
    *       @param tid
    *       @param status
    * */
    public Integer expelTeacher(@Param("tid") Integer tid,@Param("status") Integer status) throws Exception;

    /*     4.禁止登录/解除禁止登录
    *       @param tid
    *       @param islogin
    * */
    public Integer forbidTeacherLogin(@Param("tid") Integer tid,@Param("islogin") Integer islogin) throws Exception;

    /*     5.通过班主任名称查询是否存在  */
    public Teacher queryByTeacherName() throws Exception;

    /*---------------------------------------------------------------*/
    /*             班主任操作                */
    /*     1.班主任登录
    *       @param teacher
    *  */
    public Teacher teacherLogin(Teacher teacher) throws Exception;

    /*     2.班主任修改密码
    *       @param tid 班主任编号
    *       @param password 密码
    * */
    public Integer teacherUpdatePassword(@Param("tid") Integer tid,@Param("password") Integer password) throws Exception;

    /*     3.班主任修改个人资料
    *       @param teacher
    * */
    public Integer teacherUpdatePersonlData(Teacher teacher) throws Exception;

    /*---------------------------------------------------------------*/
    /*  管理员操作
    *       分页展示班级信息两表联合
    *       @param tid
    * */
    public Teacher queryById(Integer tid) throws Exception;






}

资料数据访问层(ITeacherDao)对应映射文件(ITeacherDao-mapper.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.exam.dao.ITeacherDao">
    <!--管理员操作-->
    <!--1.分页展示老师信息 queryAllTeacherMessage -->
        <select id="queryAllTeacherMessage" resultType="com.exam.bean.Teacher">
            select * from teacher
        </select>

    <!--2.添加老师信息  insertTeacher -->
        <insert id="insertTeacher" parameterType="com.exam.bean.Teacher">
            insert into teacher(tname,photo,username,gender,phone,address,remark)
            values(#{tname},#{photo},#{username},#{gender},#{phone},#{address},#{remark})
        </insert>

    <!--3.开除老师(不删除仅修改状态)/恢复开除老师状态  expelTeacher -->
        <update id="expelTeacher" parameterType="map">
            update teacher set status = #{status} where tid = #{tid}
        </update>

    <!--4.禁止登录/解除禁止登录  forbidTeacherLogin -->
        <update id="forbidTeacherLogin" parameterType="map">
            update teacher set islogin = #{islogin} where tid = #{tid}
        </update>

    <!-- 5.通过班主任名称查询是否存在 queryByTeacherName -->
        <select id="queryByTeacherName" parameterType="string" resultType="com.exam.bean.Teacher">
            select * from teacher where tname = #{tname}
        </select>


    <!--班主任操作-->
    <!--1.班主任登录  teacherLogin -->
        <select id="teacherLogin" resultType="com.exam.bean.Teacher" parameterType="com.exam.bean.Teacher">
            select * from teacher where username = #{username} and password = #{password}
        </select>

    <!--2.班主任修改密码 teacherUpdatePassword -->
        <update id="teacherUpdatePassword" parameterType="map">
            update teacher set password = #{password} where tid = #{tid}
        </update>

    <!--3.班主任修改个人资料 teacherUpdatePersonlData -->
        <update id="teacherUpdatePersonlData" parameterType="com.exam.bean.Teacher">
            update teacher set tname = #{tname},photo = #{photo},username = #{username},gender = #{gender},
                               status = #{status},islogin = #{islogin},phone = #{phone},address = #{address},
                               remark = #{remark}
            where tid = #{tid}
        </update>


    <!--管理员操作 ——> 分页展示班级信息两表联合  queryById  -->
    <select id="queryById" resultType="com.exam.bean.Teacher">
        select * from teacher where tid = #{tid}
    </select>


</mapper>

数据访问层的目录结构如下图所示:



6.4、创建业务逻辑层(service)

1.管理员服务层接口(IAdminService)以及对应的实现类(AdminServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.Admin;

/*
* 服务层功能管理员接口
* */
public interface IAdminService {
    /*
    * 管理员登录
    * @param admin
    * */
    public Admin adminLogin(Admin admin) throws Exception;

    /*管理员重置密码(666666)
    *  @param aid
    * */
    public Boolean adminResertPS(Integer aid) throws Exception;

    /*管理员修改个人资料
    * @param admin
    * */
    public Boolean adminUpdateByThis (Admin admin) throws Exception;

    /*管理员获得个人信息
    *   @param aid
    * */
    public Admin queryAdminById(Integer aid) throws Exception;



}

实现类(AdminServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.Admin;
import com.exam.dao.IAdminDao;
import com.exam.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*服务层管理员接口的实现类*/
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    IAdminDao dao;

    /*管理员登录*/
    @Override
    public Admin adminLogin(Admin admin) throws Exception {
        return dao.adminLogin(admin);
    }
    /*管理员重置密码(666666)*/
    @Override
    public Boolean adminResertPS(Integer aid) throws Exception {
        return dao.adminResertPS(aid)>0;
    }
    /*管理员修改个人资料*/
    @Override
    public Boolean adminUpdateByThis(Admin admin) throws Exception {
        return dao.adminUpdateByThis(admin)>0;
    }
    /*管理员获得个人信息*/
    @Override
    public Admin queryAdminById(Integer aid) throws Exception {
        return dao.queryAdminById(aid);
    }
}
2.学生服务层接口(IStudentService)以及对应的实现类(StudentServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.Student;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

/*服务层学生管理接口*/
public interface IStudentService {
    /*
    * 分页展示学生信息
    * */
    public PageInfo<Student> queryAllStudentMessage(Integer pageNum,Integer pageSize) throws Exception;

    /*
    * 根据班级学号姓名模糊查询学生
    * @param clsid 班级id
    * @param stuid 学号
    * @param sname 姓名
    */
    public List<Student> dimQueryStudentByClassAndSidAndName(String clsid, Integer stuid, String sname) throws Exception;


    /*--------------------------------------------------------------------------------*/
    /*---班主任操作-------*/
    /*
    * 添加学生
    * @param student
    * */
    public Boolean insertStudent(Student student) throws Exception;

    /*修改学生信息
    * @param student
    * */
    public Boolean updateStudentMessage(Student student) throws Exception;

    /*
    * 开除学生(不删除仅修改学生状态)/恢复开除的学生
    * @param status
    * @param stuid
    * */
    public Boolean expelStudent(Integer status,String stuid) throws Exception;

    /*
    * 联合查询分页展示学生信息
    * */
    public PageInfo<Student> queryAllClassAndStudent(Integer pageNum, Integer pageSize, Map<String,Object> map) throws Exception;

    /*通过 id 查询学生
    * @param stuid
    * */
    public Student queryStudentById(String stuid) throws Exception;

    /* 通过班级id两表联合查询学生信息
    * @param clsid
    * */
    public PageInfo<Student> queryByStudentByClsid(Integer clsid,Integer pageNum,Integer pageSize) throws Exception;

}

对应的实现类(StudentServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.Student;
import com.exam.dao.IStudentDao;
import com.exam.dao.ITbClassDao;
import com.exam.service.IStudentService;
import com.exam.util.IDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*服务层学生管理接口的实现类*/
@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private IStudentDao dao;
    @Autowired
    private ITbClassDao tbclassDao;

    /*分页展示学生信息*/
    @Override
    public PageInfo<Student> queryAllStudentMessage(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = dao.queryAllStudentMessage();
        PageInfo<Student> info = new PageInfo<>(list);
        return info;
    }
    /*根据班级学号姓名模糊查询学生*/
    @Override
    public List<Student> dimQueryStudentByClassAndSidAndName(String clsid, Integer stuid, String sname) throws Exception {
        return dao.dimQueryStudentByClassAndSidAndName(clsid, stuid, sname);
    }

    /*---班主任操作-------*/
    /*添加学生*/
    @Override
    public Boolean insertStudent(Student student) throws Exception {
        student.setStuid(IDUtil.getIDUtil());
        student.setStatus(1);
        Boolean flag = dao.insertStudent(student)>0;
            if(flag){
                Integer count = tbclassDao.queryStuCountById(student.getClsid());
                tbclassDao.updateClassPersonNumber(student.getClsid(), (count+1));
            }
        return null;
    }
    /*修改学生信息*/
    @Override
    public Boolean updateStudentMessage(Student student) throws Exception {
        Student flagClass =dao.queryStudentById(student.getStuid());
        if(student.getClsid()!= flagClass.getClsid()){
            //如果修改后的班级和原来的不一样
            System.out.println("班级修改了");
            //查询原来班级的人数总和
            Integer nowCount = tbclassDao.queryStuCountById(flagClass.getClsid());
            //查询修改后班级的人数总和
            Integer updateCount = tbclassDao.queryStuCountById(student.getClsid());
            //原来班级人数-1
            tbclassDao.updateClassPersonNumber(flagClass.getClsid(), (nowCount-1));
            //修改后的班级人数+1
            tbclassDao.updateClassPersonNumber(student.getClsid(), (updateCount+1));
        }
        return dao.updateStudentMessage(student)>0;
    }
    /*开除学生(不删除仅修改学生状态)/恢复开除的学生*/
    @Override
    public Boolean expelStudent(Integer status, String stuid) throws Exception {
        return dao.expelStudent(status,stuid)>0;
    }
    /*联合查询分页展示学生信息*/
    @Override
    public PageInfo<Student> queryAllClassAndStudent(Integer pageNum, Integer pageSize, Map<String, Object> map) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = dao.queryAllClassAndStudent(map);
        PageInfo<Student> info = new PageInfo<>(list);
        return info;
    }
    /*通过 id 查询学生*/
    @Override
    public Student queryStudentById(String stuid) throws Exception {
        return dao.queryStudentById(stuid);
    }
    /*通过班级id两表联合查询学生信息*/
    @Override
    public PageInfo<Student> queryByStudentByClsid(Integer clsid, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list = dao.queryByStudentByClsid(clsid);
        PageInfo<Student> info = new PageInfo<>(list);
        return info;
    }
}
3.活动服务层接口(ITbActivityService)以及对应的实现类(TbActivityServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.TbActivity;
import com.github.pagehelper.PageInfo;

/*
* 服务层活动管理接口
* */
public interface ITbActivityService {
    /*--------------------------------------------------------*/
    /*----班主任操作-----*/
    /*
    * 创建活动
    * @param activity
    * */
    public Boolean createActivity(TbActivity activity) throws Exception;

    /*
    * 修改活动
    * @param activity
    * */
    public Boolean updateActivity(TbActivity activity) throws Exception;

    /*
    * 分页展示活动
    * */
    public PageInfo<TbActivity> queryAllActivity(Integer pageNum,Integer pageSize) throws Exception;

    /*
    * 单击活动显示活动详情
    * @param id
    * */
    public TbActivity queryActivityMessage(Integer id) throws Exception;
}

对应的实现类(TbActivityServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.TbActivity;
import com.exam.dao.ITbActivityDao;
import com.exam.service.ITbActivityService;
import com.exam.util.IDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

/*服务层活动管理接口的实现类*/
@Service
public class TbActivityServiceImpl implements ITbActivityService {
    @Autowired
    private ITbActivityDao dao;

    /*----班主任操作-----*/
    /*创建活动*/
    @Override
    public Boolean createActivity(TbActivity activity) throws Exception {
        //插入时间
        activity.setStarttime(IDUtil.getTimeUtil());
        return dao.createActivity(activity)>0;
    }
    /*修改活动*/
    @Override
    public Boolean updateActivity(TbActivity activity) throws Exception {
        return dao.updateActivity(activity)>0;
    }
    /*分页展示活动*/
    @Override
    public PageInfo<TbActivity> queryAllActivity(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<TbActivity> list = dao.queryAllActivity();
        PageInfo<TbActivity> info = new PageInfo<>(list);
        return info;
    }
    /*单击活动显示活动详情*/
    @Override
    public TbActivity queryActivityMessage(Integer id) throws Exception {
        return dao.queryActivityMessage(id);
    }


    /*主线程主方法*/
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        TbActivityServiceImpl dao = ac.getBean(TbActivityServiceImpl.class);
        try{
            PageInfo<TbActivity> list = dao.queryAllActivity(1,5);
            System.out.println(list);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
4.班级服务层接口(ITbClassService)以及对应的实现类(TbClassServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.TbClass;
import com.github.pagehelper.PageInfo;

import java.util.List;

/*
* 服务层班级管理接口
* */
public interface ITbClassService {
    /*
    * 分页展示班级信息
    * */
    public PageInfo<TbClass> dimQueryTbClassMessage(Integer pageNum,Integer pageSize) throws Exception;

    /*
    * 开设班级
    * @param tbclass
    * */
    public Boolean insertTbClass(TbClass tbclass) throws Exception;

    /*
    * 修改班级班主任
    * @param teacher
    * @param cid
    * */
    public Boolean updateTbClass(Integer teacher,Integer cid) throws Exception;

    /*
    * 通过班名查询班级
    * */
    public TbClass queryByClassName() throws Exception;

    /*
    * 查询所有班级名称信息
    * */
    public List<TbClass> queryAllClassMessage() throws Exception;

    /*
    * 修改班级资料
    * @param tbclass
    * */
    public Boolean updateClassMessage(TbClass tbclass) throws Exception;

    /*
    * 通过班级id 查询修改班级信息
    * @param cid
    * */
    public TbClass queryClassById(Integer cid) throws Exception;


}

对应的实现类(TbClassServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.TbClass;
import com.exam.dao.ITbClassDao;
import com.exam.service.ITbClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*服务层班级管理接口的实现类*/
@Service
public class TbClassServiceImpl implements ITbClassService {
    @Autowired
    private ITbClassDao dao;

    /*分页展示班级信息*/
    @Override
    public PageInfo<TbClass> dimQueryTbClassMessage(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<TbClass> list = dao.dimQueryTbClassMessage();
        PageInfo<TbClass> info = new PageInfo<>(list);
        return info;
    }
    /*开设班级*/
    @Override
    public Boolean insertTbClass(TbClass tbclass) throws Exception {
        tbclass.setStucount(0);
        return dao.insertTbClass(tbclass)>0;
    }
    /*修改班级班主任*/
    @Override
    public Boolean updateTbClass(Integer teacher, Integer cid) throws Exception {
        return dao.updateTbClass(teacher, cid)>0;
    }
    /*通过班名查询班级*/
    @Override
    public TbClass queryByClassName() throws Exception {
        return dao.queryByClassName();
    }
    /*查询所有班级名称信息*/
    @Override
    public List<TbClass> queryAllClassMessage() throws Exception {
        return dao.dimQueryTbClassMessage();
    }
    /*修改班级资料*/
    @Override
    public Boolean updateClassMessage(TbClass tbclass) throws Exception {
        return dao.updateClassMessage(tbclass)>0;
    }
    /*通过班级id 查询修改班级信息*/
    @Override
    public TbClass queryClassById(Integer cid) throws Exception {
        return dao.queryClassById(cid);
    }
}
5.资料服务层接口(ITbResourceService)以及对应的实现类(TbResourceServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.TbResource;
import com.github.pagehelper.PageInfo;

import java.util.List;

/*
* 服务层资料管理接口
* */
public interface ITbResourceService {
    /*----------------班主任操作-----------------------------*/
    /*
    * 上传班级资料
    * @param resource
    * */
    public Boolean uploadTbClassData(TbResource resource) throws Exception;

    /*
    资料展示
    */
    public PageInfo<TbResource> queryResource(Integer pageNum,Integer pageSize) throws Exception;

    /*
    * 通过班级id查询班级资料
    * @param clsid
    * */
    public List<TbResource> queryByResourceByClsid(Integer clsid) throws Exception;

    /*
    * 修改班级资料
    * @param resource
    * */
    public Boolean updateClassData(TbResource resource) throws Exception;

    /*
    * 通过id查询资料
    * @param id
    * */
    public TbResource queryResourceById(Integer id) throws Exception;


}

对应的实现类(TbResourceServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.TbResource;
import com.exam.dao.ITbResourceDao;
import com.exam.service.ITbResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*服务层资料管理接口的实现类*/
@Service
public class TbResourceServiceImpl implements ITbResourceService {
    @Autowired
    private ITbResourceDao dao;

    /*班主任操作*/
    /*上传班级资料*/
    @Override
    public Boolean uploadTbClassData(TbResource resource) throws Exception {
        return dao.uploadTbClassData(resource)>0;
    }
    /*资料展示*/
    @Override
    public PageInfo<TbResource> queryResource(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<TbResource> list = dao.queryResource();
        PageInfo<TbResource> info = new PageInfo<>(list);
        return info;
    }
    /*通过班级id查询班级资料*/
    @Override
    public List<TbResource> queryByResourceByClsid(Integer clsid) throws Exception {
        return dao.queryByResourceByClsid(clsid);
    }
    /*修改班级资料*/
    @Override
    public Boolean updateClassData(TbResource resource) throws Exception {
        return dao.updateClassData(resource)>0;
    }
    /*通过id查询资料*/
    @Override
    public TbResource queryResourceById(Integer id) throws Exception {
        return dao.queryResourceById(id);
    }
}
6.教师服务层接口(ITeacherService)以及对应的实现类(TeacherServiceImpl) 如下所示:
package com.exam.service;

import com.exam.bean.Teacher;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/*
业务服务层教师管理接口
*/
public interface ITeacherService {
    /*
    * 分页展示老师信息
    * */
    public PageInfo<Teacher> queryAllTeacherMessage(Integer pageNum,Integer pageSize) throws Exception;

    /*
    * 添加老师信息
    * @param teacher
    * */
    public Boolean insertTeacher(Teacher teacher) throws Exception;

    /*
    * 开除老师(仅修改教师状态,不删除)/恢复开除老师状态
    * @param status
    * @param tid
    * */
    public Boolean expelTeacher(Integer tid,Integer status) throws Exception;

    /*
    * 禁止登录/解除禁止登录 :1.是;2.否
    * @param tid
    * @param islogin
    *  */
    public Boolean forbidTeacherLogin(Integer tid,Integer islogin) throws Exception;

    /*
    * 通过班主任名称查询是否存在
    * */
    public Teacher queryByTeacherName() throws Exception;

    /*
    * ------------班主任操作-------*/
    /*
       班主任登录
    * @param teacher
    * */
    public Teacher teacherLogin(Teacher teacher) throws Exception;

    /*
    * 班主任修改密码
    * @param tid 班主任编号
    * @param password 密码
    * */
    public Boolean teacherUpdatePassword(@Param("tid") Integer tid,@Param("password") Integer password) throws Exception;

    /*
    * 班主任修改个人资料
    * @param teacher
    * */
    public Boolean teacherUpdatePersonalData(Teacher teacher) throws Exception;

    /*----------管理员操作----------*/
    /*
    * 查询所有老师的信息
    * */
    public List<Teacher> queryAllTeacher() throws Exception;
    /*管理员-->分页展示班级信息两表联合
    * @param tid
    * */
    public Teacher queryById(Integer tid) throws Exception;

}

对应的实现类(TeacherServiceImpl) 如下所示:

package com.exam.service.Impl;

import com.exam.bean.Teacher;
import com.exam.dao.ITeacherDao;
import com.exam.service.ITeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
业务服务层教师管理接口的实现类
* */
@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private ITeacherDao dao;

    /*分页展示老师信息*/
    @Override
    public PageInfo<Teacher> queryAllTeacherMessage(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> list = dao.queryAllTeacherMessage();
        PageInfo<Teacher> info = new PageInfo<>(list);
        return info;
    }
    /*添加老师信息*/
    @Override
    public Boolean insertTeacher(Teacher teacher) throws Exception {
        return dao.insertTeacher(teacher)>0;
    }
    /*开除老师(仅修改教师状态,不删除)/恢复开除老师状态*/
    @Override
    public Boolean expelTeacher(Integer tid, Integer status) throws Exception {
        return dao.expelTeacher(tid,status)>0;
    }
    /*禁止登录/解除禁止登录 :1.是;2.否*/
    @Override
    public Boolean forbidTeacherLogin(Integer tid, Integer islogin) throws Exception {
        return dao.forbidTeacherLogin(tid,islogin)>0;
    }
    /*通过班主任名称查询是否存在*/
    @Override
    public Teacher queryByTeacherName() throws Exception {
        return dao.queryByTeacherName();
    }

    /*------------班主任操作-------*/
    /*班主任登录*/
    @Override
    public Teacher teacherLogin(Teacher teacher) throws Exception {
        return dao.teacherLogin(teacher);
    }
    /*班主任修改密码*/
    @Override
    public Boolean teacherUpdatePassword(Integer tid, Integer password) throws Exception {
        return dao.teacherUpdatePassword(tid,password)>0;
    }
    /*班主任修改个人资料*/
    @Override
    public Boolean teacherUpdatePersonalData(Teacher teacher) throws Exception {
        return dao.teacherUpdatePersonalData(teacher)>0;
    }

    /*----------管理员操作----------*/
    /*查询所有老师的信息*/
    @Override
    public List<Teacher> queryAllTeacher() throws Exception {
        return dao.queryAllTeacherMessage();
    }

    /*管理员-->分页展示班级信息两表联合
     * @param tid
     * */
    @Override
    public Teacher queryById(Integer tid) throws Exception {
        return dao.queryById(tid);
    }
}

业务逻辑层的目录结构如下图所示:



6.5、创建控制层(controller)

1.管理员控制层(AdminController):
package com.exam.controller;

import com.exam.bean.Admin;
import com.exam.bean.Teacher;
import com.exam.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
管理员控制层
*/
@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private IAdminService service;

    @RequestMapping("adminLogin")
    @ResponseBody
    public Map<String,Object> adminLogin(Admin admin, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        Admin adminMg = null;
        try {
            /*调用service层下-->管理员登录 的方法*/
            adminMg = service.adminLogin(admin);
            if(adminMg != null){
                session.setAttribute("admin", adminMg);
                map.put("sAnde", "管理员登录成功");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("sAnde","账号不存在,或密码输入错误");
        return map;
    }

    @RequestMapping("exitLogin")
    public String exitLogin(HttpSession session){
        //销毁管理员
        Admin admin = (Admin) session.getAttribute("admin");
        if(admin != null) {
            session.removeAttribute("admin");
        }
        //销毁老师
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        if(teacher != null){
            session.removeAttribute("teacher");
        }
        return "redirect:/login.jsp";
    }

    @RequestMapping("resetPassword")
    public String resetPassword(HttpSession session){
        //重置密码
        Admin admin = (Admin) session.getAttribute("admin");
        if(admin != null){
            try {
                /*调用service层下的--->管理员重置密码(666666)的方法 */
                service.adminResertPS(admin.getAid());
                session.removeAttribute("admin");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /*返回登录页面*/
        return "redirect:/login.jsp";
    }

    @RequestMapping("adminUpdateByThis")
    public String adminUpdateByThis(Admin admin, @RequestParam("myFile")MultipartFile myFile,
                                    HttpServletRequest request, Model model, HttpSession session) {
        Boolean flag = false;
        try {
            //1.设置上传路径
            String storPath = request.getServletContext().getRealPath("img");
            //2.判断该位置存在不存在
            File file = new File(storPath, myFile.getOriginalFilename());
            if (!file.getParentFile().exists()) {  //如果父路径不存在
                file.mkdirs();//则创建目录
            }
            //3.将文件上传到上传路径
            try {
                myFile.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
            admin.setPhoto(myFile.getOriginalFilename());
            /*调用service层下的---->管理员修改个人资料 的方法*/
            flag = service.adminUpdateByThis(admin);
            if (flag) {
                //修改成功,重新存session
                /*调用service层下的---> 管理员登录 方法*/
                Admin adminSession = service.adminLogin(admin);
                session.setAttribute("admin", adminSession);
            }
            //前端显示是否修改成功
            String adminUpdateFlag = flag ? "修改个人信息成功" : "修改个人信息失败";
            model.addAttribute("adminUpdateFlag", adminUpdateFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*返回到管理员页面*/
        return "/adminMain.jsp";
    }

    @RequestMapping("queryAdminById")
    @ResponseBody
    public Admin queryAdminById(HttpSession session){
        Admin sessionAdmin = (Admin) session.getAttribute("admin");
        Admin admin = null;
        try {
            /*调用service层下的-->管理员获得个人信息的方法*/
            admin =service.queryAdminById(sessionAdmin.getAid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return admin;
    }
}
2.学生控制层(StudentController)
package com.exam.controller;

import com.exam.bean.Student;
import com.exam.service.IStudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/*学生控制层*/
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService service;

    @RequestMapping("/queryStudentAll")
    @ResponseBody
    public PageInfo<Student> queryStudentAll(Integer pageNum,Integer pageSize,String clsid,Integer stuid,String sname){
        PageInfo<Student> info = null;
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("clsid", clsid);
            map.put("stuid", stuid);
            map.put("sname", sname);
            /*调用service层下的联合查询分页展示学生信息方法*/
            info = service.queryAllClassAndStudent(pageNum, pageSize, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("/insertStudent")
    public String insertStudent(Student student, Model model, HttpSession session){
        try {
            /*调用service层下的添加学生方法*/
            Boolean flag = service.insertStudent(student);
            String insertFlag = flag?"添加成功" : "添加失败";
            System.out.println("---------------" + insertFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherMain.jsp";
    }

    @RequestMapping("/queryStudentById")
    @ResponseBody
    public Student queryStudentById(String stuid){
        Student student = null;
        try {
            /*调用service层下的通过 id 查询学生的方法*/
            student = service.queryStudentById(stuid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return student;
    }

    @RequestMapping("/updateStudentMessage")
    public String updateStudentMessage(Student student,Model model){
        try {
            /*调用service层下修改学生信息的方法*/
            Boolean flag = service.updateStudentMessage(student);
            String stuUpdateFlag = flag?"修改成功" : "修改失败";
            model.addAttribute("stuUpdateFlag", stuUpdateFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherMain.jsp";
    }

    @RequestMapping("/expelStudent")
    @ResponseBody
    public Boolean expelStudent(String stuid,Integer status){
        Boolean flag = false;
        try {
            /*调用service层下的--->开除学生(不删除仅修改学生状态)/恢复开除的学生 的方法*/
            flag = service.expelStudent(status,stuid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("queryStudentMessageByClsid")
    @ResponseBody
    public PageInfo<Student> queryStudentMessageByClsid(Integer cid,Integer pageNum,Integer pageSize){
        PageInfo<Student> info = null;
        try {
            /*调用service层下-->通过班级id两表联合查询学生信息的方法*/
            info = service.queryByStudentByClsid(cid,pageNum,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

}
3.活动控制层(ActivityController)
package com.exam.controller;

import com.exam.bean.TbActivity;
import com.exam.service.Impl.TbActivityServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*活动管理控制层*/
@Controller
@RequestMapping("activity")
public class ActivityController {
    /*创建了service层对象,通过对象来调用service层下的方法*/
    @Autowired
    private TbActivityServiceImpl service;

    @RequestMapping("queryAllActivity")
    @ResponseBody
    public PageInfo<TbActivity> queryAllActivity(Integer pageNum,Integer pageSize){
        PageInfo<TbActivity> info = null;
        try {
            /*调用service层下的-->分页展示活动的方法*/
            info = service.queryAllActivity(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("createActivity")
    public String createActivity(TbActivity activity, Model model){
        try {
//           /*调用了service层下的--->创建活动 的方法 */
            Boolean flag = service.createActivity(activity);
            String createFlag = flag?"操作成功" : "创建失败";
            model.addAttribute("createFlag", createFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherActivityManager.jsp";
    }

    @RequestMapping("queryActivityById")
    @ResponseBody
    public TbActivity queryActivityById(Integer id){
        TbActivity activity = null;
        try {
            /*通过service层对象调用service层下的---》 单击活动显示活动详情 的方法*/
            activity = service.queryActivityMessage(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activity;
    }

    @RequestMapping("updateActivity")
    public String updateActivity(TbActivity activity,Model model){
        try {
            /*通过service层对象来调用了service层下的--->修改活动 的方法 */
            Boolean flag = service.updateActivity(activity);
            String updateFlag = flag? "操作成功" : "修改失败";
            model.addAttribute("updateFlag", updateFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherActivityManager.jsp";
    }

    @RequestMapping("queryActivityMessage")
    @ResponseBody
    public TbActivity queryActivityMessage(Integer id,Model model){
        TbActivity activity = null;
        try {
            /*调用了service层对象来调用了service层下的--->单击活动显示活动详情的方法 */
            activity = service.queryActivityMessage(id);
            model.addAttribute("activity", activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activity;
    }
}
4.班级控制层(ClassController)
package com.exam.controller;

import com.exam.bean.TbClass;
import com.exam.service.ITbClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 班级管理控制层
* */
@Controller
@RequestMapping("class")
public class ClassController {
    @Autowired
    private ITbClassService service;

    @RequestMapping("queryAllClassMessage")
    @ResponseBody
    public Map<String,Object> queryAllClassMessage(){
        Map<String,Object> map = new HashMap<String, Object>();
        List<TbClass> list = new ArrayList<TbClass>();
        try {
//          /*调用了service层下的 --->查询所有班级名称信息 的方法*/
            list = service.queryAllClassMessage();
            map.put("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("queryAllTeacherMessage")
    @ResponseBody
    public PageInfo<TbClass> queryAllTeacherMessage (Integer pageNum,Integer pageSize){
        PageInfo<TbClass> info = null;
        try {
//           /*调用了service层下的--->分页展示班级信息的方法*/
            info = service.dimQueryTbClassMessage(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("updateTeacherById")
    @ResponseBody
    public Boolean updateTeacherById(Integer teacher,Integer cid){
        Boolean flag = false;
        try {
            //老师id,班级id
//           /*调用了service层下的--->修改班级班主任 的方法*/
            flag = service.updateTbClass(teacher, cid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("insertTbClass")
    @ResponseBody
    public Boolean insertTbClass(TbClass tbclass, Model model){
        Boolean flag = false;
        try {
//           /*调用了service层下的--->开设班级方法*/
            flag = service.insertTbClass(tbclass);
            String insertTbClassFlag = flag? "开设成功" : "开设失败";
            model.addAttribute("insertTbClassFlag", insertTbClassFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("queryClassById")
    @ResponseBody
    public TbClass queryClassById(Integer cid){
        TbClass tbclass = null;
        try {
//          /*调用了service层下的--->通过班级id 查询修改班级信息的方法*/
            tbclass = service.queryClassById(cid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbclass;
    }

    @RequestMapping("updateClassById")
    public String updateClassById(TbClass tbclass,Model model){
        try {
            /*调用了service层下的--->修改班级资料 的方法*/
            Boolean flag = service.updateClassMessage(tbclass);
            String updateTbClassFlag = flag?"操作成功":"修改失败";
            model.addAttribute("updateTbClassFlag", updateTbClassFlag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherClassManager.jsp";
    }

}
5.资料控制层(ResourceController)
package com.exam.controller;


import com.exam.bean.TbResource;
import com.exam.service.ITbResourceService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 资料管理控制层
 * */
@Controller
@RequestMapping("resource")
public class ResourceController {
    /*创建一个资源service层对象,通过对象来调用service层下的方法*/
    @Autowired
    private ITbResourceService service;

    @RequestMapping("queryByResourceByClsid")
    @ResponseBody
    public Map<String,Object> queryByResourceByClsid(Integer clsid){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            /*调用了service层下的--->通过班级id查询班级资料的方法*/
            List<TbResource> classResource = service.queryByResourceByClsid(clsid);
            map.put("classResource", classResource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("updateClassData")
    public String updateClassData(TbResource resource, Model model, @RequestParam("myFile")MultipartFile myFile, HttpServletRequest request){
        //1.设置上传路径
        String storPath = request.getServletContext().getRealPath("fileHome");
        //2.判断该位置存在不存在
        File file = new File(storPath,myFile.getOriginalFilename());
        String url = file.toString();
        resource.setUrl(url);
        if(!file.getParentFile().exists()) { //如果父路径不存在
            file.mkdirs();  //则创建目录
        }
            try {
                //3.将文件上传到上传路径
                myFile.transferTo(file);
                /*调用了service层下的--->修改班级资料 的方法*/
                Boolean flag = service.updateClassData(resource);
                String resourceUpdate = flag ? "操作成功" : "修改失败";
                model.addAttribute("resourceUpdate", resourceUpdate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "/teacherClassManager.jsp";
    }
    @RequestMapping("queryResourceById")
    @ResponseBody
    public TbResource queryResourceById(Integer id){
        TbResource resource = null;
        try {
            /*调用了service层下的--->通过id查询资料 的方法*/
            resource = service.queryResourceById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resource;
    }

    @RequestMapping("uploadTbClassData")
    public String uploadTbClassData(TbResource resource,Model model,@RequestParam("myFile") MultipartFile myFile,HttpServletRequest request){
        //1.设置上传路径
        String storPath = request.getServletContext().getRealPath("fileHome");
        //2.判断该位置存在不存在
        File file = new File(storPath,myFile.getOriginalFilename());
        String url = file.getAbsolutePath();//获得全路径
        resource.setUrl(url);
        if(!file.getParentFile().exists()){  //如果父路径不存在
            file.mkdirs();//则创建目录
        }
        try {
            myFile.transferTo(file);
            /*调用了service层下的---->上传班级资料 的方法 */
            Boolean flag = service.uploadTbClassData(resource);
            String resourceUpdate = flag? "操作成功" : "修改失败";
            model.addAttribute("resourceUpdate", resourceUpdate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/teacherClassManager.jsp";
    }

    @RequestMapping("/queryAllResource")
    @ResponseBody
    public PageInfo<TbResource> queryAllResource(Integer pageNum,Integer pageSize){
        PageInfo<TbResource> info = null;
        try {
//           /*调用了service层下的 资料展示的方法*/
            info = service.queryResource(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("/downloadResource")
    public ResponseEntity<byte[]> downloadResource(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException {
        //获得下载的路径
        String stroeHome = request.getServletContext().getRealPath("fileHome");
        //设置字符编码
        String fileNameStr = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
        File file = new File(stroeHome + File.separator + fileName);
        //设置请求体
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", fileNameStr);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
6.教师控制层(TeacherController)
package com.exam.controller;

import com.exam.bean.Teacher;
import com.exam.service.ITeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 教师管理控制层
* */
@Controller
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private ITeacherService service;

    @RequestMapping("teacherLogin")
    @ResponseBody
    public Map<String,Object> teacherLogin(Teacher teacher, HttpSession session){
        Boolean flag = false;
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            /*通过service层对象来调用service层下的 --->班主任登录的方法*/
            Teacher teacherMg = service.teacherLogin(teacher);
            if(teacherMg != null){
                System.out.println(teacherMg);
                if(teacherMg.getStatus() == 2){
                    map.put("sAnde","该教师已离职");
                    return map;
                }
                if(teacherMg.getStatus() == 3){
                    map.put("sAnde","该教师已被开除");
                    return map;
                }
                if(teacherMg.getIslogin() == 2){
                    map.put("sAnde","该账号无法登录");
                    return map;
                }
                session.setAttribute("teacher", teacherMg);
                map.put("sAnde", "教师登录成功");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("sAnde", "该账号不存在,或者密码输入错误");
        return map;
    }

    @RequestMapping("queryAllTeacherMessage")
    @ResponseBody
    public PageInfo<Teacher> queryAllTeacherMessage(Integer pageNum,Integer pageSize){
        PageInfo<Teacher> info = null;
        try {
            /*通过service层对象来调用service层下的--->分页展示老师信息*/
            info = service.queryAllTeacherMessage(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("expelTeacher")
    @ResponseBody
    public Boolean expelTeacher(Integer tid,Integer status){
        Boolean flag = false;
        try {
            //开除老师(不删除仅修改状态)
            /*通过service层对象来调用service层下的-->开除老师(仅修改教师状态,不删除)/恢复开除老师状态 -->的方法*/
            flag = service.expelTeacher(tid,status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("forbidTeacherLogin")
    @ResponseBody
    public Boolean forbidTeacherLogin(Integer tid,Integer islogin){
        Boolean flag = false;
        try {
            //禁止登录是否可登录:1.是;2.否
            /*通过service层对象来调用了service层下的-->禁止登录/解除禁止登录*/
            flag = service.forbidTeacherLogin(tid,islogin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping("insertTeacher")
    public String insertTeacher(Teacher teacher, @RequestParam("myFile")MultipartFile myFile, HttpServletRequest request, Model model){
        try{
        //1.设置上传路径
        String storPath = request.getServletContext().getRealPath("img");
        //2.判断该位置存在不存在
        File file = new File(storPath,myFile.getOriginalFilename());
        if (!file.getParentFile().exists()){//如果父路径不存在
            file.mkdirs();//则创建目录
        }
        //3.将文件上传到上传路径
        try {
            myFile.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        teacher.setPhoto(myFile.getOriginalFilename());
        /*通过service层对象来调用service层下的-->添加老师信息 的方法*/
        Boolean insertFlag = service.insertTeacher(teacher);
        String msg = insertFlag?"添加成功":"添加失败";
        model.addAttribute("msg", msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/adminMain.jsp";
    }

    @RequestMapping("queryAllTeacher")
    @ResponseBody
    public Map<String,Object> queryAllTeacher(){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            /*通过service层对象来调用service层下的-->查询所有老师的信息 的方法*/
            List<Teacher> list = service.queryAllTeacher();
            map.put("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("queryTeacherById")
    @ResponseBody
    public Teacher queryTeacherById(HttpSession session){
        Teacher teacher = null;
        try {
            Teacher sessionTeacherMg = (Teacher) session.getAttribute("teacher");
            /*通过service层对象来调用service层下的--->分页展示班级信息两表联合 的方法*/
            teacher = service.queryById(sessionTeacherMg.getTid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return teacher;
    }

    @RequestMapping("teacherUpdatePersonalData")
    public String teacherUpdatePersonalData(Teacher teacher,@RequestParam("myFile") MultipartFile myFile,
                                            HttpServletRequest request,Model model,HttpSession session){
        try {
            //1.设置上传路径
            String storPath = request.getServletContext().getRealPath("img");
            //2.判断该位置存在不存在
            File file = new File(storPath, myFile.getOriginalFilename());
            if (!file.getParentFile().exists()) {//如果父路径不存在
                file.mkdirs(); //则创建目录
            }
            //3.将文件上传到上传路径
            try {
                myFile.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
            teacher.setPhoto(myFile.getOriginalFilename());
            Teacher sessionTeacherMg = (Teacher) session.getAttribute("teacher");
            teacher.setStatus(sessionTeacherMg.getStatus());
            teacher.setIslogin(sessionTeacherMg.getIslogin());
            System.out.println("=============" + teacher);
            /*通过service层对象来调用service层下的---> 班主任修改个人资料 的方法*/
            Boolean updateFlag = service.teacherUpdatePersonalData(teacher);
            String msg = updateFlag ? "修改成功" : "修改失败";
            if (updateFlag) {
                Teacher saveSession = service.teacherLogin(teacher);
                session.setAttribute("teacher", saveSession);
            }
            model.addAttribute("msg", msg);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/teacherMain.jsp";
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值