springboot的使用!

本文详细介绍了SpringBoot的使用,包括自动配置的SpringMVC特性,如静态资源处理、数据绑定和HttpMessageConverters。还讲解了如何在SpringBoot中配置拦截器、整合MyBatis进行数据库操作,以及使用Thymeleaf、SpringSecurity、Shiro、Swagger等。此外,探讨了多线程实现异步效果、邮件发送、定时任务和Redis集成等内容。
摘要由CSDN通过智能技术生成

首先了解下什么是springboot

是可以用于搭建spring应用的脚手架的框架。
我自己觉得的比较明显的好处:

  1. 配置很少就可以用了
  2. 导入一个启动器会导入很多依赖,方便省事啊

springboot对springMVC自动配置了哪些东西呢?:

官网的原文和翻译(不过版本一直在更新,可能会有更新、增加或删除)

Spring MVC Auto-configuration
// Spring Boot为Spring MVC提供了自动配置,它可以很好地与大多数应用程序一起工作。
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
// 自动配置在Spring默认设置的基础上添加了以下功能:
The auto-configuration adds the following features on top of Spring’s defaults:
// 包含视图解析器
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
// 支持静态资源文件夹的路径,以及webjars
Support for serving static resources, including support for WebJars
// 自动注册了Converter:转换器,这就是我们网页提交数据到后台自动封装成为对象的东西,比如把"1"字符串自动转换为int类型
// Formatter:【格式化器,比如页面给我们了一个2019-8-10,它会给我们自动格式化为Date对象】
Automatic registration of Converter, GenericConverter, and Formatter beans.
// HttpMessageConverters
// SpringMVC用来转换Http请求和响应的的,比如我们要把一个User对象转换为JSON字符串,可以去看官网文档解释;
Support for HttpMessageConverters (covered later in this document).
// 定义错误代码生成规则的
Automatic registration of MessageCodesResolver (covered later in this document).
// 首页定制
Static index.html support.
// 图标定制
Custom Favicon support (covered later in this document).
// 初始化数据绑定器:帮我们把请求数据绑定到JavaBean中!
Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
/*
如果您希望保留Spring Boot MVC功能,并且希望添加其他MVC配置(拦截器、格式化程序、视图控制器和其他功能),则可以添加自己的@configuration类,类型为webmvcconfiguer,但不添加@EnableWebMvc。如果希望提供
RequestMappingHandlerMapping、RequestMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义实例,则可以声明WebMVCregistrationAdapter实例来提供此类组件。
*/
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration
(interceptors, formatters, view controllers, and other features), you can add your own
@Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide
custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or
ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.
// 如果您想完全控制Spring MVC,可以添加自己的@Configuration,并用@EnableWebMvc进行注释。
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

在springboot项目里使用拦截器

首先了解下什么是拦截器(要知道我们用他干嘛?):
在这里插入图片描述
拦截器可以做登录验证之类的功能,比如没登录跳到登录页。。。
结构如下 :
在这里插入图片描述

创建拦截器:

@Component
//要实现这个接口先
public class MyInterceptor implements HandlerInterceptor {
   
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   
        System.out.println("前置方法正在执行");
        return true; //return true 是放行,使程序正常向后面执行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
   
        System.out.println("后置方法正在执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
   
        System.out.println("完成方法正在执行");
    }
}

创建配置类来配置我们的拦截器 :

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
   

    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
   
    	//注册拦截器和拦截路径, /**是多级都可以拦截, /*是只拦截一级
    	//静态资源不能被拦截,还有登录页面不能拦截,所以后面我们可以排除掉一些资源不被拦截的好像叫exclude的方法。
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}

测试一下拦截器:
在这里插入图片描述
在这里插入图片描述
拦截器使用成功!

springboot整合mybatis

连接数据库

需要导入jdbc依赖、mysql驱动和mybatis依赖

<!--不是spring官方写的,所以是mybatis在前面-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.1.3</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值