目录
前言
在本次项目实训中我负责后端的开发,后端主要使用springboot和mybatis,数据库使用mysql。
由于我之前并未对springboot进行系统的学习,仅仅使用过SSM开发,所以在之前一段时间我先是学习了一遍springboot以及mybatis框架。
springboot项目的框架如下:
环境搭建
JDK1.8+ ,MAVEN3.2+,Spring Framework 5.x+,SpringBoot内嵌的Tomcat为9.0+。
引入如下依赖:
<!--继承springboot的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version> 可以通过修改这个来指定springboot的版本
</parent>
<dependencies>
<!--引入springboot的web支持,一个依赖相当于SSM中的所有org.Springframework依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
编写入口类:
//在项目中src/java下创建入口类 Application
@SpringBootApplication
public class XXXApplication {
public static void main(String[] args) {
SpringApplication.run(XXXApplication.class,args);
}
}
此时一个基础的项目就成型了。可以看出来springboot的开发步骤比SSM简单便捷的多。
切面编程
springboot是对原有项目中spring框架和springmvc的进一步封装,因此在springboot中同样支持spring框架中AOP切面编程,不过在springboot中为了快速开发仅仅提供了注解方式的切面编程。
切面编程就类似于拦截器。
首先引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
前置切面:
在SpringBoot项目中新建一个config(配置)包,包内新建一个配置类,添加附加方法:
@Aspect
@Configuration
public class MyAspect {
@Before("execution(* com.baizhi.service.*.*(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置通知");
joinPoint.getTarget();//目标对象
joinPoint.getSignature();//方法签名
joinPoint.getArgs();//方法参数
}
}
后置切面:
@Aspect
@Configuration
public class MyAspect {
@After("execution(* com.baizhi.service.*.*(..))")
public void before(JoinPoint joinPoint){
System.out.println("后置通知");
joinPoint.getTarget();//目标对象
joinPoint.getSignature();//方法签名
joinPoint.getArgs();//方法参数
}
}
环绕切面:
@Aspect
@Configuration
public class MyAspect {
@Around("execution(* com.baizhi.service.*.*(..))")
public Object before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("进入环绕通知");
proceedingJoinPoint.getTarget();//目标对象
proceedingJoinPoint.getSignature();//方法签名
proceedingJoinPoint.getArgs();//方法参数
Object proceed = proceedingJoinPoint.proceed();//放行执行目标方法
System.out.println("目标方法执行之后回到环绕通知");
return proceed;//返回目标方法返回值
}
}
环绕通知存在返回值,参数为ProceedingJoinPoint(必须加参数),如果不调用proceed方法,就不会执行目标方法,如果调用proceed方法就必须将目标方法的返回值返回,否则调用者无法接受返回数据。
拦截器
拦截器只能拦截controller,而javaweb中的过滤器还可以拦截静态资源。拦截器用到了AOP。
新建一个包叫做interceptors(以后可能有多个拦截器),把拦截器放到包里。
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
System.out.println("======1=====");
return true;//返回true 放行 返回false阻止 参数o代表请求的控制器方法对象
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("=====2=====");
}
@Override
//此方法一定会执行,无论请求是否出现异常。
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
System.out.println("=====3=====");
}
}
配置拦截器:
在SpringMVC中我们在springmvc.xml中指定拦截器,但是在SpringBoot中没有springmvc.xml,我们就需要使用SpringMVC的配置类:WebMvcConfigurer接口,新建一个类实现WebMvcConfigurer接口即可。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor("new拦截器类名()")
.addPathPatterns("拦截路径") /**代表拦截所有controller
.excludePathPatterns("排除路径") 排除某些路径:/file/**
.order("指定执行顺序")
}
}
WAR包部署
设置打包方式为war:
clean之后点击package
之后target目录会有一个war包,但是这个包不能直接用,因为项目中有内嵌服务器,但是war包部署需要一个外部服务器,我们需要先排除内嵌服务器,并下载外部服务器。
默认是jar包部署,我们也推荐使用jar包部署。
RestFul
REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移。 它首次出现在2000年Roy Fielding的博士论文中,Roy Fielding是HTTP规范的主要编写者之一。 他在论文中提到:"我这篇文章的写作目的,就是想在符合架构原理的前提下,理解和评估以网络为基础的应用软件的架构设计,得到一个功能强、性能好、适宜通信的架构。REST指的是一组架构约束条件和原则。" 如果一个架构符合REST的约束条件和原则,我们就称它为RESTful架构。
使用RESTful操作资源
【GET】 /users # 查询用户信息列表
【GET】 /users/1001 # 查看某个用户信息
【POST】 /users # 新建用户信息
【PUT】 /users/1001 # 更新用户信息(全部字段)
【PATCH】 /users/1001 # 更新用户信息(部分字段)
【DELETE】 /users/1001 # 删除用户信息