1 Star 0 Fork 4

lixiaoliang / 基于SpringBoot框架的管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

基于SpringBoot框架的管理系统

介绍

基于SpringBoot框架的管理系统 简洁版

实现 登录注册

可继续完善增加前端、校验、其他功能等;

可作为 SpringBoot框架 开发练习基础模型;

课程设计毕业设计 开发基础;

任何复杂的框架都是简单的知识组合并延伸的,学好基础知识才是最重要的;

此项目简单,便于理解基本原理,为复杂的SpringBoot框架复杂项目的开发打下基础。

环境准备

开发平台:Idea 2019

数据库:MySQL 5.0.22

服务器:Tomcat 9.0.37 SpringBoot自动配置Tomcat服务器

注意:需要安装IDEA开发平台、MySQL数据库,版本不一定完全一致,做好版本适应性配置即可。

技术架构:

后端:SpringBoot

数据库管理:Mybatis

前端:Thymeleaf

项目结构

1.数据库及Java代码文件 输入图片说明

2.前端代码及配置文件 输入图片说明

3.项目启动

输入图片说明

4.MySQL数据库 输入图片说明

基本原理

1. 相关概念

SpringBoot 是伴随着 Spring4.0 共同诞生的,目的是简化 spring 的配置及开发,并协助开发人员可以整体管理应用程序的配置,提供了众多开发组件,且内嵌了 web 应用容器,如 Tomcat 和 Jetty 等。目的是使开发得到简化,且能大幅提高开发人员的开发效率,为了简化Spring功能的配置我们可以引入或启动需要的Spring功能。避免开发人员过多的对框架的关注,把更多的精力与时间放在系统的业务逻辑代码中。

2. SpringBoot的作用

SpringBoot框架,功能非常简单,便是帮助实现自动配置。SpringBoot框架的核心是自动配置。只要有相应的jar包,Spring则会帮助开发者实现自动配置,而无需像使用spring框架一样做很多配置。当默认配置不能满足要求的时候,能够用自己的配置来替换这些自动的配置类。此外,SpringBoot内嵌了web应用容器,除此之外还集成了系统监控等功能,都可以帮助快速搭建企业级的应用程序并使用。

3. SpringBoot的核心功能

1.可以不依赖Tomcat等外部容器独立运行web项目,springboot的优点是能够以jar包的形式运行;

2.嵌入式的Servlet容器:不需要再打war包后再运行,在springboot中可以选择内嵌的tomcat、Jetty或者Undertow等容器来直接运行;

3.使pom文件配置更简化:只需要在 pom 文件中添加starter-web 依赖即可,无需引入很多依赖;

4.能够在生产环境中直接使用性能指标、健康检查和应用信息等;

5.springboot不需要任何xml文件配置而能实现所有的spring配置。

4. SpringBoot的原理

1、使用IDEA开发工具创建一个springboot项目,主要依赖

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

spring-boot-starter-web会自动引入开发过程中所需要的web模块jar包,如包含了spring-web和spring-webmvc的依赖,不需要再逐个的去导入,只需要导入一个就可以,避免遗漏或者版本问题。

2、SpringBootApplication类

@SpringBootApplication(exclude={SecurityAutoConfiguration.class})
@MapperScan(value = "com.mapper")  
public class ManagerSystem_SpringBoot_Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ManagerSystem_SpringBoot_Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(ManagerSystem_SpringBoot_Application.class, args);
    }

面试会经常被问到Springboot的核心注解是什么?

@SpringBootApplication包含了很多注解,

其中

@SpringBootConfiguration,

@EnableAutoConfiguration,

@ComponentScan

三个注解尤为重要

@SpringBootConfiguration

源码如下:

package org.springframework.boot;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

其中@Configuration注解就是用来读取spring.factories文件

@EnableAutoConfiguration

而@EnableAutoConfiguration注解和@Configuration注解都具有配置的功能,那么又有什么区别?

@EnableAutoConfiguration注解使用的是自动配置机制,是自动导入,通俗的说就是在代码中自动import操作。例如,如果pom文件中配置了spring-boot-starter-web,那么他的jar包会放到类路径classpath里面,spring会自动配置tomcat和springmvc。如果不是自动配置的话,那么就需要手动配置。如果使用了自动配置以后,这些配置代码就不需要再额外的去配置了。

@Configuration的作用是如果有自动导入的话那么肯定就会有自动导出,注意,这需要该类方法中的@bean注解,可以理解为xml中的bean配置。

@ComponentScan

作用是告诉Spring哪个包下面的类用了这个注解,那么就会被spring自动扫描并且放入bean容器。

5.SpringBoot的启动原理

SpringBoot整个启动流程分为两个步骤:

  • 初始化一个SpringApplication对象

  • 执行该对象的run方法。

package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootConfiguration
@SpringBootApplication(exclude={SecurityAutoConfiguration.class})
@MapperScan(value = "com.mapper") 
public class ManagerSystem_SpringBoot_Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ManagerSystem_SpringBoot_Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(ManagerSystem_SpringBoot_Application.class, args);
    }

}
5.1SpringApplication初始化
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        this.webApplicationType = deduceWebApplicationType();
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = deduceMainApplicationClass();
    }

可以知道初始化流程中最重要的就是通过SpringFactoriesLoader找到spring.factories文件中配置的ApplicationContextInitializer和ApplicationListener两个接口的实现类名称,以便后期构造相应的实例

  • ApplicationContextInitializer的主要目的是在ConfigurableApplicationContext做refresh之前,对ConfigurableApplicationContext实例做进一步的设置或处理。

  • ApplicationListener的目的是Spring框架对Java事件监听机制的一种框架实现。

    Spring Boot提供两种方式来添加自定义监听器通过

    SpringApplication.addListeners(ApplicationListener<?>... listeners)

    或者

    SpringApplication.setListeners(Collection extends ApplicationListener>> listeners)

    两个方法来添加一个或者多个自定义监听器。然后还需要开发者直接在自己的jar包的META-INF/spring.factories文件中新增配置即可:

org.springframework.context.ApplicationListener = cn.moondev.listeners.xxxxListener\(自定义监听器
5.2SpringBoot启动流程

Spring Boot应用的整个启动流程都封装在SpringApplication.run方法中,本质上其实就是在spring的基础之上做了封装,做了大量的扩张。

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
//1.通过SpringFactoriesLoader查找并加载所有的SpringApplicationRunListeners,通过调用
//starting()方法通知所有的SpringApplicationRunListeners:应用开始启动了
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
//2.创建并配置当前应用将要使用的Environment
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
//3.打印banner
            Banner printedBanner = printBanner(environment);
//4.根据是否是web项目,来创建不同的ApplicationContext容器
            context = createApplicationContext();
//5.创建一系列FailureAnalyzer
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
//6.初始化ApplicationContext
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
//7.调用ApplicationContext的refresh()方法,刷新容器
            refreshContext(context);
//8.查找当前context中是否注册有CommandLineRunner和ApplicationRunner,如果有则遍历执行它们。
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            listeners.started(context);
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, exceptionReporters, ex);
            throw new IllegalStateException(ex);
        }
        listeners.running(context);
        return context;
    }

1.通过SpringFactoriesLoader查找并加载所有的SpringApplicationRunListeners,通过调用starting()方法通知所有的SpringApplicationRunListeners:应用开始启动。(SpringApplicationRunListeners其本质上就是一个事件发布者,在SpringBoot应用启动的不同时间点发布不同应用事件类型(ApplicationEvent),如果有哪些事件监听者(ApplicationListener)对这些事件感兴趣,则可以接收并且处理)

SpringApplicationRunListeners源码:

public interface SpringApplicationRunListener {
    // 运行run方法时立即调用此方法,可以用户非常早期的初始化工作
    void starting();
    // Environment准备好后,并且ApplicationContext创建之前调用
    void environmentPrepared(ConfigurableEnvironment environment);
    // ApplicationContext创建好后立即调用
    void contextPrepared(ConfigurableApplicationContext context);
    // ApplicationContext加载完成,在refresh之前调用
    void contextLoaded(ConfigurableApplicationContext context);
    // 当run方法结束之前调用
    void finished(ConfigurableApplicationContext context, Throwable exception);
}

SpringApplicationRunListener只有一个实现类:EventPublishingRunListener。只会获取到一个EventPublishingRunListener的实例

starting()方法的内容:

public void starting() {
    // 发布一个ApplicationStartedEvent
    this.initialMulticaster.multicastEvent(new ApplicationStartedEvent(this.application, this.args));
}

2.创建并配置当前应用将要使用的Environment,Environment用于描述应用程序当前的运行环境,其抽象了两个方面的内容:配置文件(profile)和属性(properties),不同的环境(eg:生产环境、预发布环境)可以使用不同的配置文件,而属性则可以从配置文件、环境变量、命令行参数等来源获取。因此,当Environment准备好后,在整个应用的任何时候,都可以从Environment中获取资源。

  • 判断Environment是否存在,不存在就创建(如果是web项目就创建StandardServletEnvironment,否则创建StandardEnvironment)
  • 配置Environment:配置profile以及properties
  • 调用SpringApplicationRunListener的environmentPrepared()方法,通知事件监听者:应用的Environment已经准备好

3.打印banner(可以自定义)

4.根据是否是web项目,来创建不同的ApplicationContext容器

5.创建一系列FailureAnalyzer,创建流程依然是通过SpringFactoriesLoader获取到所有实现FailureAnalyzer接口的class,然后在创建对应的实例。FailureAnalyzer用于分析故障并提供相关诊断信息。

6.初始化ApplicationContext

  • 将准备好的Environment设置给ApplicationContext
  • 遍历调用所有的ApplicationContextInitializer的initialize()方法来对已经创建好的ApplicationContext进行进一步的处理
  • 调用SpringApplicationRunListener的contextPrepared()方法,通知所有的监听者:ApplicationContext已经准备完毕
  • 将所有的bean加载到容器中
  • 调用SpringApplicationRunListener的contextLoaded()方法,通知所有的监听者:ApplicationContext已经装载完毕

7.调用ApplicationContext的refresh()方法,刷新容器

  • 这里的刷新和spring中刷新原理类似,这里重点关注invokeBeanFactoryPostProcessors(beanFactory);方法,主要完成获取到所有的BeanFactoryPostProcessor来对容器做一些额外的操作,通过源可以进入到PostProcessorRegistrationDelegate类的invokeBeanFactoryPostProcessors()方法,会获取类型为BeanDefinitionRegistryPostProcessor的beanorg.springframework.context.annotation.internalConfigurationAnnotationProcessor,对应的Class为ConfigurationClassPostProcessor。ConfigurationClassPostProcessor用于解析处理各种注解,包括:@Configuration、@ComponentScan、@Import、@PropertySource、@ImportResource、@Bean。当处理@import注解的时候,就会调用EnableAutoConfigurationImportSelector.selectImports()来完成自动配置功能

8.查找当前context中是否注册有CommandLineRunner和ApplicationRunner,如果有则遍历执行它们。

页面展示

1.登录页面

输入图片说明

2.登录失败

输入图片说明

3.注册页面 输入图片说明

4.注册成功

输入图片说明

5.主页 输入图片说明

6.新增页面 输入图片说明

7.删除页面 输入图片说明

8.修改页面 输入图片说明

注意

1.MySQL 版本5.0以上与8.0以上配置有些许差异,需要调整pom文件中数据库连接器版本

2.运行项目前提前配置好MySQL数据库、IDEA maven依赖库

3.本系统仅实现简单功能,仅为更好理解SpringBoot原理,需进一步完善更多功能及健壮性

LICENSE

zjzhou

About me

一个爱学习、爱分享、爱交流的程序员;

欢迎关注个人微信公众号【Java烂笔头】,一起交流、共同进步;
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于SpringBoot框架的管理系统 简洁版 ; 实现 登录 、 注册 、 增 、 删 、 改 、 查 ; 可继续完善增加前端、校验、其他功能等; 可作为 SpringBoot框架项目 开发练习基础模型; 课程设计 、 毕业设计 开发基础; 任何复杂的框架都是简单的知识组合并延伸的,学好基础知识才是最重要的; 此项目简单,便于理解基本原理,为复杂的SpringBoot框架复杂项目的开发打下基础。 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/lxl123/ManagerSystem_SpringBoot.git
git@gitee.com:lxl123/ManagerSystem_SpringBoot.git
lxl123
ManagerSystem_SpringBoot
基于SpringBoot框架的管理系统
master

搜索帮助