Article Directory
foreword
This article will introduce the design and implementation of the student status management project in detail.
Supplement: database file
1. General overview of the project
1. Project introduction
(1) Main functions
This project is aimed at administrators, students, and teachers, and is used to manage student status information.
The main functions of the project :
(1) The administrator can add, delete, modify and check the information of students, classes, majors, teachers, etc., and assign permissions.
(2) Teachers can query student information, manage grades, and approve applications.
(3) Students can check personal grades, course information, and submit applications.
All types of users can view personal information and change passwords.
The functional structure of the system is shown in the figure:
(2) Main technologies
The system as a whole adopts B/S architecture and MVC design pattern for design and development.
Backend: SSM (Spring+SpringMVC+Mybatis)
Frontend: Layuimini
Database: MySQL
Development tool: IDEA
2. Project display
The following is a partial page display.
Login interface:
Administrator login Home page:
Administrator-Professional management page:
Teacher login-student query page:
Student login-application management page:
2. Detailed design
1. Database design
The following is the database design relationship diagram of the system:
From this diagram, the association relationship between the tables can be obtained.
2. Project framework construction
(1) Create a maven project and configure the pom.xml file
Create a maven project in idea, and introduce the jar package and mybatis and mybatis-spring packages that the spring framework depends on in its core file pom.xml file.
Note: After creating a maven project, create a java directory (to store source code) and a resource directory (to store resource information) at the same level as the webapp
Supplement: Simple understanding of mybatis : the use of MyBatis tags
Part of the code in the pom.xml file is as follows:
<!--引入Spring相关jar-->
<!--如:context坐标,context依赖core,beans等-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-beans</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-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--引入mybatis相关jar-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
(2) Configure the spring core configuration file applicationContext.xml
Create applicationContext.xml configuration file under resources
The following shows part of the file code:
<?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:shemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
If you don't know spring, you can refer to: SSM framework learning
(3) Configure the spring-MVC core configuration file spring-mvc.xml
Create a spring-mvc.xml configuration file under resources
The following shows part of the code of the file:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
(4) Configure web.xml (integrate spring)
Spring integration steps:
a. Configure the front controller DispatcherServlet, load the Spring-mvc.xml configuration file (load the spring container, etc.).
b. Configure the Spring context listener: load the applicationContext.xml file.
c. Configure encoding filter: Solve encoding problems
<servlet>
<servlet-name>student_system</servlet-name>
<!--配置前端控制器DispatcherServlet,加载Spring-mvc.xml配置文件,加载spring容器-->
<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>
<!--启动Tomcat容器就加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--映射信息 <url-pattern>/</url-pattern> 拦截不带请求 -->
<servlet-mapping>
<servlet-name>student_system</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置上下文参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring上下文监听 加载applicationContext.xml文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<!--3、配置编码过滤器:解决编码问题 –>-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(5) Integrate Mybatis
Preparations:
a. Create db.properties to store the connection information of the database
b. Create the mybatis-config.xml file in the resource directory
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--根标签-->
<configuration>
<!--settings:控制mybatis全局行为-->
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
c. Add mysql dependency and database connection pool in the pom.xml file
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
Configuration: In the applicationContext.xml file:
it can be divided into four steps: load db.properties, configure data source, obtain sqlSessionFactory factory, scan mapper file
<!--1.加载db.properties-->
<context:property-placeholder location="classpath:db.properties" />
<!--2.配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--3.获取sqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--配置mapper文件-->
<property name="mapperLocations">
<list>
<value>classpath:com/demo/dao/*.xml</value>
</list>
</property>
<!--配置分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--4.扫描mapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描dao目录下的所有mapper文件-->
<property name="basePackage" value="com.demo.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
(6) Test the SSM framework
a. Create an entity class: create an entity class User under entity
b. Create an interface in the Dao layer: UserDao, and the corresponding mapper file: UserMapper.xml
UserDao:
The screenshot of UserMapper.xml is as follows:
c. Create UserService in the service layer
d. Create a UserController test at the controller layer
:
a. In spring-mvc.xml:
b. In applicationContext.xml:
use postman for testing.
The framework is built successfully.
3. Project structure display
The system directory structure is shown in the figure:
Relationship between layers:
(1) Under the com.demo directory structure:
a. Controller (control layer): responsible for the control of the specific business module process, accepting the parameters from the page, calling the corresponding service service to complete the processing of user requests, and then passing the return value to the page.
For specific business processes, there will be different controllers.
b. DAO layer: Some tasks responsible for contacting the database are encapsulated here, such as: adding, deleting, modifying and checking basic sql statement operations.
c. entity layer: the entity (model layer) of the data object, which is an entity bean.
It is used for ORM object-relational mapping. In this system design, different entities are mapped into tables without business logic code.
d. Service layer: write specific business logic, also called service layer.
Each Service generally contains a set of related business logic. When designing, first design the interface, then design its implementation class, and then configure its implementation association in the Spring configuration file. Then call the Service interface to perform business processing.
e.utils layer: This directory is a custom tool class
such as: BeanMapUtils converts bean objects into maps. MD5utils: encrypt and store user passwords.
(2) Under the resources directory structure:
(3) Under the webapp directory structure:
a. static directory: store related resources introduced by the layuimini framework.
b. WEB-INF directory: store all Jsp pages in the front end.
4. Write each layer
(1) Dao layer writing
Take UserDao as an example, implement specific additions, deletions, changes, and queries in the UserMapper.xml file .
Implemented with dynamic sql technology :
If you don’t know about dynamic sql technology, you can refer to: Use of dynamic sql in mapper mapping file
The following is the code part of UserMapper.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文件生成动态代理实现类-->
<mapper namespace="com.demo.dao.UserDao">
<!-- resultMap:mybatis结果集封装-->
<!-- property指定javaBean的属性名-->
<!-- column指定数据库字段名或者其别名-->
<resultMap type="com.demo.entity.User" id="User">
<id column="id" property="id"/>
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="remark" property="remark"/>
<result column="user_name" property="userName"/>
<result column="user_pwd" property="userPwd"/>
</resultMap>
<!--增-->
<insert id="create" keyProperty="id" useGeneratedKeys="true" parameterType="com.demo.entity.User">
insert into tb_user(
name,
remark,
user_name,
user_pwd
)values(
#{name},
#{remark},
#{userName},
#{userPwd}
)
</insert>
<!--删-->
<delete id="delete">
delete from tb_user
<include refid="UserFindCriteria"/>
</delete>
<!--改-->
<update id="update">
update tb_user
<include refid="UserUpdateCriteria"/>
<include refid="UserFindCriteria"/>
</update>
<!--查-->
<select id="query" resultMap="User">
select * from tb_user
<include refid="UserFindCriteria"/>
<if test="offset!=null and rows!=null">limit ${offset} , ${rows}</if>
</select>
<!--查询分页-->
<select id="count" resultType="int">
select count(1) from tb_user
<include refid="UserFindCriteria"/>
</select>
<!--查询明细-->
<select id="detail" resultMap="User">
select * from tb_user
<include refid="UserFindCriteria"/>
limit 1
</select>
<!--动态sql查询-->
<sql id="UserFindCriteria">
<where>
<if test="id != null">and id = #{id}</if>
<if test="name != null and name != ''">and name like concat('%',#{name},'%')</if>
<if test="remark != null and remark != ''">and remark = #{remark}</if>
<if test="userName != null and userName != ''">and user_name like concat('%',#{userName},'%')</if>
<if test="userPwd != null and userPwd != ''">and user_pwd = #{userPwd}</if>
</where>
</sql>
<!--动态sql更新-->
<sql id="UserUpdateCriteria">
<set>
<if test="updateId != null">id = #{updateId},</if>
<if test="updateName != null and updateName != ''">name = #{updateName},</if>
<if test="updateRemark != null and updateRemark != ''">remark = #{updateRemark},</if>
<if test="updateUserName != null and updateUserName != ''">user_name = #{updateUserName},</if>
<if test="updateUserPwd != null and updateUserPwd != ''">user_pwd = #{updateUserPwd},</if>
</set>
</sql>
</mapper>
Supplement: In the above code, #{} and ${} are used, and their usage can be referred to:
In addition, fuzzy query is used in the query condition :
its usage can refer to: Four ways of fuzzy query in mybatis
ResultMap is used in the above code , and its use method refers to: The role of ResultMap in Mybatis
Define the specific implementation method in the UserDao interface:
package com.demo.dao;
import java.util.List;
import java.util.Map;
import com.demo.entity.User;
public interface UserDao {
public int create(User pi);
public int delete(Map<String, Object> paramMap);
public int update(Map<String, Object> paramMap);
public List<User> query(Map<String, Object> paramMap);
public User detail(Map<String, Object> paramMap);
public int count(Map<String, Object> paramMap);
}
The writing of other modules in the Dao layer is similar:
The directory structure of the Dao layer:
(2) Service layer writing
The Dao layer uses map for data transfer parameters. Since the Service layer needs to implement specific business operations, the data transfer parameters are specific class objects (Bean), and the Bean needs to be converted into a Map.
(1) Create the BeanMapUtils class in the utils directory:
/* Bean转为Map */
public class BeanMapUtils {
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = new HashMap();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(key+"", beanMap.get(key));
}
}
return map;
}
//处理更新---多加的update
public static <T> Map<String, Object> beanToMapForUpdate(T bean) {
Map<String, Object> map = new HashMap();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put("update"+upperFirstLatter(key+""),beanMap.get(key));
}
}
return map;
}
//大小写转换
public static String upperFirstLatter(String letter){
char[] chars = letter.toCharArray();
if(chars[0]>='a' && chars[0]<='z'){
chars[0] = (char) (chars[0]-32);
}
return new String(chars);
}
(2) Encapsulate the map: MapParameter
takes UserService as an example:
Other modules of the Service layer are written similarly:
Service layer directory structure:
(3) Controller layer writing
Take UserController as an example:
Other modules in the Controller layer are written similarly:
Directory structure of the Controller layer:
5. Integrate static pages (layUImini single-page version)
(1) Create a static directory under the webapp and import the required resources
(2) Springmvc view parser
Function: access jsp pages under the WEBINF directory.
Configure in Springmvc.xml:
(3) Configure the basePath path:
3. Project module design
1. Login module
The flowchart of the login module is as follows:
page style design and form data submission on the login.jsp page:
user name and password data verification in LoginController:
different users log in:
//管理员登录
if("1".equals(type)){
User user = userService.login(userName, MD5Utils.getMD5(password));
if(user != null){
session.setAttribute("user",user);
session.setAttribute("type",1);
return MapControll.getInstance().success().add("data",user).getMap();
}else{
return MapControll.getInstance().error("用户名或密码错误").getMap();
}
}
//老师登录
if("2".equals(type)){
Teacher teacher = teacherService.login(userName, MD5Utils.getMD5(password));
if(teacher != null){
session.setAttribute("user",teacher);
session.setAttribute("type",2);
return MapControll.getInstance().success().add("data",teacher).getMap();
}else{
return MapControll.getInstance().error("用户名或密码错误").getMap();
}
}
//学生登录
if("3".equals(type)){
Student student = studentService.login(userName, MD5Utils.getMD5(password));
if(student != null){
session.setAttribute("user",student);
session.setAttribute("type",3);
return MapControll.getInstance().success().add("data",student).getMap();
}else{
return MapControll.getInstance().error("用户名或密码错误").getMap();
}
}
Call the Service layer code corresponding to different user roles, taking the administrator login as an example, call the login method of userService.
(1) verification code
Design ideas:
a. When visiting any website, create a session session (whether you log in or not)
b. The picture of the verification code itself (the background is based on the combination of letters and numbers, and generates a picture response to the client. Before generating the picture, it will The text information of the generated verification code is put into the session)
c. When logging in, enter the text information on the picture, and judge whether it is consistent with the text information put into the session when generating the verification code. If they are consistent, the verification code is entered correctly. Otherwise, it will prompt that the verification code is wrong.
CaptchaController class:
(2) MD5 encryption
In order to ensure that user passwords are stored encrypted in the database, use the MD5 encryption algorithm to encrypt
and create the MD5Utils class:
package com.demo.utils;
import org.springframework.util.DigestUtils;
public class MD5Utils {
//盐
private static final String salt = "StudentSystemManager###$$@@";
public static String getMD5(String string){
String val = string+salt;
return DigestUtils.md5DigestAsHex(val.getBytes());
}
//MD5加密
public static void main(String[] args) {
System.out.println(getMD5("123456"));
}
}
(3) Permission filtering
(1) Create an interceptor: PermissionInterceptor:
(2) SpringMVC.xml:
2. Management module
Log in as an administrator, taking professional management as an example:
class management, teacher management, course management, and administrator management are basically the same.
(1) Professional list query
a. In SubjectController - page jump
b. Create subject directory under page directory - create list.jsp, add.jsp, update.jsp pages
c. Realize in list.jsp
d. Query - form binding submit event
(2) Professional list pagination
The paging function uses the Mybatis-PageHelper plug-in
a. Configure the paging plug-in in applicationContext.xml
b. Create the entity class Entity (attribute 1: page attribute 2: limit) - realize paging - let all other classes inherit from this class
c. In SubjectService:
(3) Professional additions, deletions and modifications
a. Delete – batch delete in list.jsp:
in the controller layer----service layer (increase batch delete)
b. Add – list.jsp:
in add.jsp page:
c. Modify – list.jsp , only one line can be selected at a time to modify the controller service
3. Course management module
The following is a screenshot of the course management module interface:
the course management module adopts the left and right fence style design: the left is a tree structure, and the right is a list structure
Among them, the tree structure adopts the ztree plug-in
(1) list.jsp page layout: left and right grid format, Make data submission
(2) layui+ztree integration (custom module)
ztree.js:
(3) The function of integrating course management
is in SectionController:
for other roles (students, teachers), refer to the professional management section of administrators.
Summarize
Project source code: student status management system
The above is the entire content of this article.