赞
踩
1、数据库(分别是年级表、性别表、学生表、学科表)
附上数据库代码(如下)
- /*
- SQLyog Ultimate v12.09 (64 bit)
- MySQL - 5.7.14-log : Database - schooldemo3
- *********************************************************************
- */
-
-
- /*!40101 SET NAMES utf8 */;
-
- /*!40101 SET SQL_MODE=''*/;
-
- /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
- /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
- CREATE DATABASE /*!32312 IF NOT EXISTS*/`schooldemo3` /*!40100 DEFAULT CHARACTER SET utf8 */;
-
- USE `schooldemo3`;
-
- /*Table structure for table `class` */
-
- DROP TABLE IF EXISTS `class`;
-
- CREATE TABLE `class` (
- `id` INT(10) NOT NULL AUTO_INCREMENT,
- `sid` INT(10) NOT NULL,
- `classes` VARCHAR(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-
- /*Data for the table `class` */
-
- INSERT INTO `class`(`id`,`sid`,`classes`) VALUES (1,1,'一班'),(2,2,'二班'),(3,3,'三班');
-
- /*Table structure for table `sex` */
-
- DROP TABLE IF EXISTS `sex`;
-
- CREATE TABLE `sex` (
- `id` INT(10) NOT NULL AUTO_INCREMENT,
- `sid` INT(10) NOT NULL,
- `sex` VARCHAR(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-
- /*Data for the table `sex` */
-
- INSERT INTO `sex`(`id`,`sid`,`sex`) VALUES (1,1,'男'),(2,2,'女');
-
- /*Table structure for table `student` */
-
- DROP TABLE IF EXISTS `student`;
-
- CREATE TABLE `student` (
- `id` INT(10) NOT NULL AUTO_INCREMENT,
- `sname` VARCHAR(20) NOT NULL,
- `ssex` INT(10) NOT NULL,
- `sage` INT(10) NOT NULL,
- `sclass` INT(10) NOT NULL,
- `szym` VARCHAR(50) DEFAULT NULL,
- `ssubject` INT(10) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=INNODB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-
- /*Data for the table `student` */
-
- INSERT INTO `student`(`id`,`sname`,`ssex`,`sage`,`sclass`,`szym`,`ssubject`) VALUES (1,'张三',1,16,1,'读书使我快乐',1),(2,'李四',2,15,1,'我爱读书',2),(3,'王五',1,15,2,'书中自有黄金屋',3),(4,'赵六',1,16,2,'书中自有颜如玉',2),(5,'夏七',2,15,3,'书籍是人类进步的阶梯',1),(6,'jack',1,16,3,'I like china',1),(7,'rouse',2,15,3,'study everyday',3);
-
- /*Table structure for table `subject` */
-
- DROP TABLE IF EXISTS `subject`;
-
- CREATE TABLE `subject` (
- `id` INT(10) NOT NULL AUTO_INCREMENT,
- `sid` INT(10) NOT NULL,
- `subject` VARCHAR(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-
- /*Data for the table `subject` */
-
- INSERT INTO `subject`(`id`,`sid`,`subject`) VALUES (1,1,'语文'),(2,2,'数学'),(3,3,'英语');
-
- /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
- /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
- /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
- /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
2、项目整体结构
3、导入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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.hbt</groupId>
- <artifactId>SSMTest6</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
-
- <name>SSMTest6 Maven Webapp</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <!--导入spring的依赖-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.2.5.RELEASE</version>
- </dependency>
- <!--Spring操作数据库的话,还需要一个spring-jdbc-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>5.3.9</version>
- </dependency>
- <!--aop注入包-->
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.9.4</version>
- </dependency>
- <!--mybatis与spring整合的包-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>2.0.6</version>
- </dependency>
- <!--junit-->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.7</version>
- <scope>test</scope>
- </dependency>
-
- <!--mysql驱动-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- </dependency>
- <!--mybatis-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.2</version>
- </dependency>
- <!--log4j-->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.12</version>
- </dependency>
- <!--Lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.10</version>
- </dependency>
- <!--阿里的连接池-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.6</version>
- </dependency>
-
- <!--mvc-->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.59</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>5.2.5.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>5.2.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>5.2.5.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.6</version>
- </dependency>
-
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- 引入JSON -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
-
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <!-- <dependency>-->
- <!-- <groupId>com.fasterxml.jackson.core</groupId>-->
- <!-- <artifactId>jackson-databind</artifactId>-->
- <!-- <version>2.8.5</version>-->
- <!-- </dependency>-->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.9.5</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.5</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>2.9.5</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.module</groupId>
- <artifactId>jackson-module-jaxb-annotations</artifactId>
- <version>2.9.5</version>
- </dependency>
- </dependencies>
-
- </project>
4、编写ssm配置文件
在resources下新建个配置文件database.properties,内容如下:
- driver=com.mysql.jdbc.Driver
- #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
- url=jdbc:mysql://127.0.0.1:3306/schooldemo3?useUnicode=true&characterEncoding=utf-8
- user=root
- password=root
在resources下新建个配置文件log4j.properties,内容如下:
- # rootLogger是所有日志的根日志,修改该日志属性将对所有日志起作用
- # 下面的属性配置中,所有日志的输出级别是info,输出源是con
- log4j.rootLogger=debugger,con
- # 定义输出源的输出位置是控制台
- log4j.appender.con=org.apache.log4j.ConsoleAppender
- # 定义输出日志的布局采用的类
- log4j.appender.con.layout=org.apache.log4j.PatternLayout
- # 定义日志输出布局
- log4j.appender.con.layout.ConversionPattern=%d{MM-dd HH:mm:ss}[%p]%c%n -%m%n
在resources下新建个配置文件spring-dao.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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:property-placeholder location="classpath:database.properties"/>
-
- <!--配置数据源-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${driver}"></property>
- <property name="url" value="${url}"></property>
- <property name="username" value="${user}"></property>
- <property name="password" value="${password}"></property>
- <property name="maxActive" value="50"></property>
- <property name="initialSize" value="3"></property>
- <property name="maxWait" value="60000"></property>
- </bean>
-
- <!--生成session工厂-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="typeAliasesPackage" value="com.hbt.entity"></property>
- <property name="mapperLocations">
- <array>
- <value>classpath:mapper/*Mapper.xml</value>
- </array>
- </property>
- </bean>
-
- <!--扫描mapper接口,代理生成mapper实例-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.hbt.dao"></property>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- </bean>
- </beans>
-
在resources下新建个配置文件spring-mvc.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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:component-scan base-package="com.hbt"/>
- <!--视图解析器-->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
- </beans>
-
web.xml文件
- <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>Archetype Created Web Application</display-name>
- <!--welcome pages-->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
-
- <!--需要配置一个servlet springmvc的核心类,总控制器-->
- <servlet>
- <servlet-name>dispatchServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- <async-supported>true</async-supported>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatchServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-dao.xml</param-value>
- </context-param>
-
- <!-- 解决字符乱码问题 -->
- <filter>
- <filter-name>characterEncodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
iml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <module type="JAVA_MODULE" version="4">
- <component name="FacetManager">
- <facet type="web" name="Web">
- <configuration>
- <descriptors>
- <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
- </descriptors>
- <webroots>
- <root url="file://$MODULE_DIR$/web" relative="/" />
- </webroots>
- <sourceRoots>
- <root url="file://$MODULE_DIR$/src/main/java" />
- <root url="file://$MODULE_DIR$/src/main/resources" />
- </sourceRoots>
- </configuration>
- </facet>
- </component>
- </module>
5、entity层代码
Class实体类
- package com.hbt.dao;
-
- import com.hbt.entity.Sex;
- import com.hbt.entity.Student;
- import com.hbt.entity.Subject;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description StudentMapper
- * @since 2022-03-23 13:53
- */
- public interface StudentMapper {
- //下拉框加载学生性别
- public List<Sex> selsex();
- //下拉框加载学生班级
- public List<Class> selclass();
- //下拉框加载学生最喜欢学科
- public List<Subject> selsubject();
- //分页查询数据
- public List<Student> selAllinfo(@Param("start") Integer start,@Param("end") Integer end,
- @Param("sname") String sname,@Param("ssex") Integer ssex,
- @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
- //查询总条数
- public Integer count(@Param("sname") String sname,@Param("ssex") Integer ssex,
- @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
- //新增学生信息
- public void addinfo(Student student);
- //根据id查询出该学生信息
- public Student selByidinfo(Integer id);
- //修改学生信息
- public void upinfo(Student student);
- //删除学生信息
- public void delinfo(Integer id);
- }
Page实体类
- package com.hbt.entity;
-
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description Page
- * @since 2022-03-23 14:02
- */
- public class Page {
- private Integer totalPage; //总页数
- private Integer totalNumber; //总条数
- private Integer pageNumber; //每页展示数据
- private Integer currentPage; //当前页
- private Integer backPage; //上一页
- private Integer nextPage; //下一页
- private List<Student> studentList;
-
- public Integer getTotalPage() {
- return totalPage;
- }
-
- public void setTotalPage(Integer totalPage) {
- this.totalPage = totalPage;
- }
-
- public Integer getTotalNumber() {
- return totalNumber;
- }
-
- public void setTotalNumber(Integer totalNumber) {
- this.totalNumber = totalNumber;
- }
-
- public Integer getPageNumber() {
- return pageNumber;
- }
-
- public void setPageNumber(Integer pageNumber) {
- this.pageNumber = pageNumber;
- }
-
- public Integer getCurrentPage() {
- return currentPage;
- }
-
- public void setCurrentPage(Integer currentPage) {
- this.currentPage = currentPage;
- }
-
- public Integer getBackPage() {
- return backPage;
- }
-
- public void setBackPage(Integer backPage) {
- this.backPage = backPage;
- }
-
- public Integer getNextPage() {
- return nextPage;
- }
-
- public void setNextPage(Integer nextPage) {
- this.nextPage = nextPage;
- }
-
- public List<Student> getStudentList() {
- return studentList;
- }
-
- public void setStudentList(List<Student> studentList) {
- this.studentList = studentList;
- }
-
- @Override
- public String toString() {
- return "Page{" +
- "totalPage=" + totalPage +
- ", totalNumber=" + totalNumber +
- ", pageNumber=" + pageNumber +
- ", currentPage=" + currentPage +
- ", backPage=" + backPage +
- ", nextPage=" + nextPage +
- ", studentList=" + studentList +
- '}';
- }
- }
Sex实体类
- package com.hbt.entity;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description Sex
- * @since 2022-03-23 13:52
- */
- public class Sex {
- private Integer id;
- private Integer sid;
- private String sex;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- @Override
- public String toString() {
- return "Sex{" +
- "id=" + id +
- ", sid=" + sid +
- ", sex='" + sex + '\'' +
- '}';
- }
- }
Student实体类
- package com.hbt.entity;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description Student
- * @since 2022-03-23 13:44
- */
- public class Student {
- private Integer id; //学生表id
- private String sname; //学生姓名
- private Integer ssex; //学生性别id
- private String sex; //学生性别
- private Integer sage; //学生年龄
- private Integer sclass; //学生班级id
- private String classes; //学生班级
- private String szym; //座右铭
- private Integer ssubject;//最喜欢学科id
- private String subject; //最喜欢学科
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
-
- public Integer getSsex() {
- return ssex;
- }
-
- public void setSsex(Integer ssex) {
- this.ssex = ssex;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public Integer getSage() {
- return sage;
- }
-
- public void setSage(Integer sage) {
- this.sage = sage;
- }
-
- public Integer getSclass() {
- return sclass;
- }
-
- public void setSclass(Integer sclass) {
- this.sclass = sclass;
- }
-
- public String getClasses() {
- return classes;
- }
-
- public void setClasses(String classes) {
- this.classes = classes;
- }
-
- public String getSzym() {
- return szym;
- }
-
- public void setSzym(String szym) {
- this.szym = szym;
- }
-
- public Integer getSsubject() {
- return ssubject;
- }
-
- public void setSsubject(Integer ssubject) {
- this.ssubject = ssubject;
- }
-
- public String getSubject() {
- return subject;
- }
-
- public void setSubject(String subject) {
- this.subject = subject;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", sname='" + sname + '\'' +
- ", ssex=" + ssex +
- ", sex='" + sex + '\'' +
- ", sage=" + sage +
- ", sclass=" + sclass +
- ", classes='" + classes + '\'' +
- ", szym='" + szym + '\'' +
- ", ssubject=" + ssubject +
- ", subject='" + subject + '\'' +
- '}';
- }
- }
Subject实体类
- package com.hbt.entity;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description Subject
- * @since 2022-03-23 13:53
- */
- public class Subject {
- private Integer id;
- private Integer sid;
- private String subject;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSubject() {
- return subject;
- }
-
- public void setSubject(String subject) {
- this.subject = subject;
- }
-
- @Override
- public String toString() {
- return "Subject{" +
- "id=" + id +
- ", sid=" + sid +
- ", subject='" + subject + '\'' +
- '}';
- }
- }
6、dao层代码
- package com.hbt.dao;
-
- import com.hbt.entity.Sex;
- import com.hbt.entity.Student;
- import com.hbt.entity.Subject;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description StudentMapper
- * @since 2022-03-23 13:53
- */
- public interface StudentMapper {
- //下拉框加载学生性别
- public List<Sex> selsex();
- //下拉框加载学生班级
- public List<Class> selclass();
- //下拉框加载学生最喜欢学科
- public List<Subject> selsubject();
- //分页查询数据
- public List<Student> selAllinfo(@Param("start") Integer start,@Param("end") Integer end,
- @Param("sname") String sname,@Param("ssex") Integer ssex,
- @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
- //查询总条数
- public Integer count(@Param("sname") String sname,@Param("ssex") Integer ssex,
- @Param("sclass") Integer sclass,@Param("ssubject") Integer ssubject);
- //新增学生信息
- public void addinfo(Student student);
- //根据id查询出该学生信息
- public Student selByidinfo(Integer id);
- //修改学生信息
- public void upinfo(Student student);
- //删除学生信息
- public void delinfo(Integer id);
- }
7、service层代码
- package com.hbt.service;
-
- import com.hbt.entity.Page;
- import com.hbt.entity.Sex;
- import com.hbt.entity.Student;
- import com.hbt.entity.Subject;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description StudentService
- * @since 2022-03-23 13:54
- */
- public interface StudentService {
- //下拉框加载学生性别
- public List<Sex> selsex();
- //下拉框加载学生班级
- public List<Class> selclass();
- //下拉框加载学生最喜欢学科
- public List<Subject> selsubject();
- //分页查询数据
- public Page selinfo(Integer start, Integer end, String sname, Integer ssex, Integer sclass, Integer ssubject);
- //新增学生信息
- public void addinfo(Student student);
- //根据id查询出该学生信息
- public Student selByidinfo(Integer id);
- //修改学生信息
- public void upinfo(Student student);
- //删除学生信息
- public void delinfo(Integer id);
- }
8、serviceimpl层代码
- package com.hbt.service.impl;
-
- import com.hbt.dao.StudentMapper;
- import com.hbt.entity.Page;
- import com.hbt.entity.Sex;
- import com.hbt.entity.Student;
- import com.hbt.entity.Subject;
- import com.hbt.service.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description StudentServiceImpl
- * @since 2022-03-23 13:54
- */
- @Service
- public class StudentServiceImpl implements StudentService {
- @Autowired
- private StudentMapper studentMapper;
-
- @Override
- public List<Sex> selsex() {
- return studentMapper.selsex();
- }
-
- @Override
- public List<Class> selclass() {
- return studentMapper.selclass();
- }
-
- @Override
- public List<Subject> selsubject() {
- return studentMapper.selsubject();
- }
-
- @Override
- public Page selinfo(Integer start, Integer end, String sname, Integer ssex, Integer sclass, Integer ssubject) {
- Page page = new Page();
- Integer totalcount = studentMapper.count(sname, ssex, sclass, ssubject);
- if (start == null )start =1;
- page.setTotalNumber(totalcount);
- page.setCurrentPage(start == 0 ? 1:start);
- page.setTotalPage(totalcount%3>0 ? totalcount/3+1 : totalcount/3);
- page.setBackPage(start-1<0 ? 1:start-1);
- page.setNextPage(start+1>page.getTotalPage() ? page.getTotalPage() : start+1);
- List<Student> studentList =studentMapper.selAllinfo(
- (page.getCurrentPage()-1)*3, 3, sname, ssex, sclass, ssubject);
- page.setStudentList(studentList);
- return page;
- }
-
- @Override
- public void addinfo(Student student) {
- studentMapper.addinfo(student);
- }
-
- @Override
- public Student selByidinfo(Integer id) {
- return studentMapper.selByidinfo(id);
- }
-
- @Override
- public void upinfo(Student student) {
- studentMapper.upinfo(student);
- }
-
- @Override
- public void delinfo(Integer id) {
- studentMapper.delinfo(id);
- }
- }
9、controller层代码
- package com.hbt.controller;
-
- import com.alibaba.fastjson.JSONObject;
- import com.hbt.entity.Page;
- import com.hbt.entity.Sex;
- import com.hbt.entity.Student;
- import com.hbt.entity.Subject;
- import com.hbt.service.StudentService;
- 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.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.List;
-
- /**
- * @author houbotong
- * @version 0.0.3
- * @description StudentController
- * @since 2022-03-23 13:34
- */
- @Controller
- public class StudentController {
- @Resource
- private StudentService studentService;
-
- //默认页面
- @RequestMapping("/initWeb")
- public String initWeb(Model model){
- //初始页面下拉框加载
- List<Sex> sexList = studentService.selsex();
- List<Class> classList = studentService.selclass();
- List<Subject> subjectList = studentService.selsubject();
- model.addAttribute("sexList",sexList);
- model.addAttribute("classList",classList);
- model.addAttribute("subjectList",subjectList);
- return "index";
- }
-
- //分页展示所有数据
- @RequestMapping("/studentList")
- @ResponseBody
- public void selAllInfo(HttpServletRequest request, HttpServletResponse response, Integer start, Integer end,
- String sname, Integer ssex, Integer sclass, Integer ssubject) throws IOException {
- if (start == null) start=1;
- //System.out.println("sname:"+sname+";ssex:"+ssex+";sclass:"+sclass+";ssubject:"+ssubject);
- Page page = studentService.selinfo(start, end, sname, ssex, sclass, ssubject);
- JSONObject jobj = new JSONObject();
- jobj.put("page",page);
- //System.out.println(page);
- response.getWriter().append(jobj.toJSONString());
- }
-
- //新增学生信息
- @RequestMapping("/stuAdd")
- public String addinfo(Student student){
- studentService.addinfo(student);
- return "redirect:/initWeb";
- }
-
- //新增信息跳转
- @RequestMapping("/toAdd")
- public String toAdd(Model model){
- //新增页面下拉框加载
- List<Sex> sexList = studentService.selsex();
- List<Class> classList = studentService.selclass();
- List<Subject> subjectList = studentService.selsubject();
- model.addAttribute("sexList",sexList);
- model.addAttribute("classList",classList);
- model.addAttribute("subjectList",subjectList);
- return "addinfo";
- }
-
- //根据id查出该学生信息
- @RequestMapping("/selupinfo")
- public String selupinfo(Model model,Integer id){
- //修改页面下拉框加载
- List<Sex> sexList = studentService.selsex();
- List<Class> classList = studentService.selclass();
- List<Subject> subjectList = studentService.selsubject();
- model.addAttribute("sexList",sexList);
- model.addAttribute("classList",classList);
- model.addAttribute("subjectList",subjectList);
- Student student = studentService.selByidinfo(id);
- model.addAttribute("student",student);
- return "upinfo";
- }
-
- //修改学生信息
- @RequestMapping("/updateinfo")
- public String updateinfo(Student student){
- studentService.upinfo(student);
- return "redirect:/initWeb";
- }
-
- //删除学生信息
- @RequestMapping("/delindo")
- public String delinfo(Integer id){
- studentService.delinfo(id);
- return "redirect:/initWeb";
- }
- }
10、前端页面代码
添加页面
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>新增学生</title>
- </head>
-
- <body>
- <h2>新增学生</h2>
- <form action="stuAdd" method="post" accept-charset="UTF-8">
- <table border="1">
- <tr>
- <td>学生姓名</td>
- <td>
- <input type="text" name="sname">
- </td>
- </tr>
- <tr>
- <td>年龄</td>
- <td>
- <input type="text" name="sage">
- </td>
- </tr>
- <tr>
- <td>座右铭</td>
- <td>
- <input type="text" name="szym">
- </td>
- </tr>
- <tr>
- <td>性别</td>
- <td>
- <select name="ssex" id="ssex">
- <c:forEach items="${sexList}" var="sex">
- <option value="${sex.sid}">${sex.sex}</option>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td>班级</td>
- <td>
- <select name="sclass" id="sclass">
- <c:forEach items="${classList}" var="cl">
- <option value="${cl.sid}">${cl.classes}</option>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td>最爱学科</td>
- <td>
- <select name="ssubject" id="ssubject">
- <c:forEach items="${subjectList}" var="sub">
- <option value="${sub.sid}">${sub.subject}</option>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="提交">
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
加载页面
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>学生信息</title>
- </head>
- <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
- <script language="JavaScript">
- window.onload=function (){
- aaa();
- }
- function aaa(start){
- var info={
- sname : document.getElementById("sname").value,
- ssex : document.getElementById("ssex").value,
- sclass : document.getElementById("sclass").value,
- ssubject : document.getElementById("ssubject").value,
- start : start
- };
- //alert(info.ssex);
- $.ajax({
- url:"/SSMTest6_war_exploded/studentList",//服务器地址
- type:"post",//提交方式
- data: info,//提交数据
- dataType:"json",//回调数据类型
- success:function (page){
- var dataObj = page.page.studentList;//返回的result的json格式的数据
- var dataObj2 = page.page.totalPage;
- con = "";
- con2 = "";
- con+="<tr>";
- con+="<th>学生姓名</th>";
- con+="<th>年龄</th>";
- con+="<th>座右铭</th>";
- con+="<th>性别</th>";
- con+="<th>班级</th>";
- con+="<th>最爱学科</th>";
- con+="<th colspan=2>操作</th>";
- con+="</tr>";
- $.each(dataObj,function (index,item){
- con+="<tr>";
- con+="<td>"+item.sname+"</td>";
- con+="<td>"+item.sage+"</td>";
- con+="<td>"+item.szym+"</td>";
- con+="<td>"+item.sex+"</td>";
- con+="<td>"+item.classes+"</td>";
- con+="<td>"+item.subject+"</td>";
- con+="<td><a href='/SSMTest6_war_exploded/selupinfo?id="+item.id+"'>修改</a> | <a href='/SSMTest6_war_exploded/delindo?id="+item.id+"'>删除</a></td>";
- con+="</tr>";
- });
- $("#con").html(con);
- con2+="<button id='aaa' onclick='aaa(1)'>首页</button>";
- con2+="<button id='aaa' onclick='aaa("+page.page.backPage+")'>上一页</button>";
- con2+="<button id='aaa' onclick='aaa("+page.page.nextPage+")'>下一页</button>";
- con2+="<button id='aaa' onclick='aaa("+page.page.totalPage+")'>末页</button>";
- con2+="<span>第"+page.page.currentPage+"页/共"+page.page.totalPage+"页("+page.page.totalNumber+"条记录)</span><br/>";
- //con2+="<div>跳转至<input type='text' id='paper' onkeyup='this.value=this.value.replace(/\D/g,'')' onafterpaste='this.value=this.value.replace(/\D/g,'')'/>页<input type='button' value='GO' onclick='aaa()'/></div>";
- for(var i=1; i<=dataObj2; i++){
- con2+="<button id='aaa' onclick='aaa("+i+")'>"+i+"</button>";
- }
- $("#con2").html(con2);
- }
- });
- }
- </script>
- <body>
- <a href="toAdd">添加学生</a>
- <div>
- 学生名称<input type="text" name="sname" id="sname">
- 性别<select name="ssex" id="ssex">
- <option value="">--请选择--</option>
- <c:forEach items="${sexList}" var="sex">
- <option value="${sex.sid}">${sex.sex}</option>
- </c:forEach>
- </select>
- 班级<select name="sclass" id="sclass">
- <option value="">--请选择--</option>
- <c:forEach items="${classList}" var="cl">
- <option value="${cl.sid}">${cl.classes}</option>
- </c:forEach>
- </select>
- 最爱学科<select name="ssubject" id="ssubject">
- <option value="">--请选择--</option>
- <c:forEach items="${subjectList}" var="sub">
- <option value="${sub.sid}">${sub.subject}</option>
- </c:forEach>
- </select>
- <button id="aaa" onclick="aaa(1)">查询</button>
- </div>
- <div id="bbb">
- <table border="1" id="con">
-
- </table>
- </div>
- <div id="con2">
-
- </div>
- </body>
- </html>
修改页面
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>修改学生信息</title>
- </head>
-
- <body>
- <h2>修改学生信息</h2>
- <form action="updateinfo" method="post">
- <input type="hidden" name="id" value="${student.id}">
- <table border="1">
- <tr>
- <td>学生姓名</td>
- <td>
- <input type="text" name="sname" value="${student.sname}">
- </td>
- </tr>
- <tr>
- <td>年龄</td>
- <td>
- <input type="text" name="sage" value="${student.sage}">
- </td>
- </tr>
- <tr>
- <td>座右铭</td>
- <td>
- <input type="text" name="szym" value="${student.szym}">
- </td>
- </tr>
- <tr>
- <td>性别</td>
- <td>
- <select name="ssex">
- <c:forEach items="${sexList}" var="sex">
- <c:if test="${sex.sid == student.ssex}">
- <option value="${sex.sid}" selected = "selected">${sex.sex}</option>
- </c:if>
- <c:if test="${sex.sid != student.ssex}">
- <option value="${sex.sid}">${sex.sex}</option>
- </c:if>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td>班级</td>
- <td>
- <select name="sclass">
- <c:forEach items="${classList}" var="cl">
- <c:if test="${cl.sid == student.sclass}">
- <option value="${cl.sid}" selected = "selected">${cl.classes}</option>
- </c:if>
- <c:if test="${cl.sid != student.sclass}">
- <option value="${cl.sid}">${cl.classes}</option>
- </c:if>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td>最爱学科</td>
- <td>
- <select name="ssubject">
- <c:forEach items="${subjectList}" var="sub">
- <c:if test="${sub.sid == student.ssubject}">
- <option value="${sub.sid}" selected = "selected">${sub.subject}</option>
- </c:if>
- <c:if test="${sub.sid != student.ssubject}">
- <option value="${sub.sid}">${sub.subject}</option>
- </c:if>
- </c:forEach>
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="提 交" />
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
11、mapper文件StudentMapper.xml代码(sql)
- <?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.hbt.dao.StudentMapper">
- <!-- 下拉框加载学生性别 -->
- <select id="selsex" resultType="com.hbt.entity.Sex">
- SELECT * FROM sex
- </select>
- <!-- 下拉框加载学生班级 -->
- <select id="selclass" resultType="com.hbt.entity.Class">
- SELECT * FROM class
- </select>
- <!-- 下拉框加载学生最喜欢学科 -->
- <select id="selsubject" resultType="com.hbt.entity.Subject">
- SELECT * FROM SUBJECT
- </select>
- <!-- 分页查询学生数据 -->
- <select id="selAllinfo" resultType="com.hbt.entity.Student">
- SELECT a.id,a.sname,a.sage,a.szym,b.sex,c.classes,d.subject FROM student a
- INNER JOIN sex b ON b.sid=a.ssex
- INNER JOIN class c ON c.sid=a.sclass
- INNER JOIN SUBJECT d ON d.sid=a.ssubject
- <if test="sname != null and sname != ''">
- and a.sname like concat('%',#{sname},'%')
- </if>
- <if test="ssex != null and ssex != ''">
- and a.ssex = #{ssex}
- </if>
- <if test="sclass != null and sclass != ''">
- and a.sclass = #{sclass}
- </if>
- <if test="ssubject != null and ssubject != ''">
- and a.ssubject = #{ssubject}
- </if>
- ORDER BY a.id limit #{start },#{end }
- </select>
- <!-- 查询总条数 -->
- <select id="count" resultType="java.lang.Integer">
- SELECT COUNT(*) FROM student
- <trim prefix="where">
- 1=1
- <if test="sname != null and sname != ''">
- and sname like concat('%',#{sname},'%')
- </if>
- <if test="ssex != null and ssex != ''">
- and ssex = #{ssex}
- </if>
- <if test="sclass != null and sclass != ''">
- and sclass = #{sclass}
- </if>
- <if test="ssubject != null and ssubject != ''">
- and ssubject = #{ssubject}
- </if>
- </trim>
- </select>
- <!-- 新增学生信息 -->
- <insert id="addinfo" parameterType="com.hbt.entity.Student">
- INSERT INTO student(sname,ssex,sage,sclass,szym,ssubject)
- VALUES(#{sname}, #{ssex}, #{sage}, #{sclass}, #{szym}, #{ssubject})
- </insert>
- <!-- 根据id查询出该学生信息 -->
- <select id="selByidinfo" resultType="com.hbt.entity.Student">
- SELECT * FROM student WHERE id = #{id}
- </select>
- <!-- 修改学生信息 -->
- <update id="upinfo" parameterType="com.hbt.entity.Student">
- UPDATE student SET sname=#{sname},sage=#{sage},szym=#{szym},ssex=#{ssex},sclass=#{sclass},ssubject=#{ssubject}
- where id=#{id}
- </update>
- <!-- 根据id删除学生信息 -->
- <delete id="delinfo" parameterType="com.hbt.entity.Student">
- delete from student where id=#{id}
- </delete>
- </mapper>
-
-
-
功能实现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。